Module: Origami::StandardObject::ClassMethods

Includes:
TypeGuessing
Defined in:
lib/origami/object.rb

Overview

:nodoc:all

Instance Method Summary collapse

Methods included from TypeGuessing

#guess_type

Instance Method Details

#field(name, attributes) ⇒ Object

Define a new field with given attributes.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/origami/object.rb', line 181

def field(name, attributes)
  if attributes[:Required] && attributes.key?(:Default) && (attributes[:Type] == Name)
    signature = {}
    signature[name] = attributes[:Default]

    add_type_signature(**signature)
  end

  if @fields.key?(name)
    @fields[name].merge! attributes
  else
    @fields[name] = attributes
  end

  define_field_methods(name)
end

#fieldsObject



174
175
176
# File 'lib/origami/object.rb', line 174

def fields
  @fields
end

#hint_type(name) ⇒ Object

Returns the expected type for a field name.



213
214
215
# File 'lib/origami/object.rb', line 213

def hint_type(name)
  @fields[name][:Type] if @fields.key?(name)
end

#inherited(subclass) ⇒ Object



170
171
172
# File 'lib/origami/object.rb', line 170

def inherited(subclass)
  subclass.instance_variable_set(:@fields, @fields.map { |name, attributes| [name, attributes.clone] }.to_h)
end

#required_fieldsObject

Returns an array of required fields for the current Object.



201
202
203
204
205
206
207
208
# File 'lib/origami/object.rb', line 201

def required_fields
  fields = []
  @fields.each_pair do |name, attributes|
    fields << name if attributes[:Required] == true
  end

  fields
end