Module: NestedStruct::Interface::ClassMethods

Defined in:
lib/nested_struct/interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#field_namesObject (readonly)

Returns the value of attribute field_names.



8
9
10
# File 'lib/nested_struct/interface.rb', line 8

def field_names
  @field_names
end

#fieldsObject (readonly)

Returns the value of attribute fields.



8
9
10
# File 'lib/nested_struct/interface.rb', line 8

def fields
  @fields
end

Instance Method Details

#field(name, coercer_expression = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nested_struct/interface.rb', line 10

def field(name, coercer_expression = nil)
  if superclass.ancestors.include?(NestedStruct::Interface)
    @field_names ||= superclass.field_names.dup || []
    @fields ||= superclass.fields.dup || []
  else
    @field_names ||= []
    @fields ||= []
  end

  if !field_names.include?(name)
    @field_names << name
    @fields << Field.new(name, coercer_expression)
    attr_accessor(name)
  end
end