Class: FormObj::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/form_obj/struct.rb,
lib/form_obj/struct/array.rb,
lib/form_obj/struct/attribute.rb,
lib/form_obj/struct/attributes.rb

Direct Known Subclasses

Form

Defined Under Namespace

Classes: Array, Attribute, Attributes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.array_classObject



12
13
14
# File 'lib/form_obj/struct.rb', line 12

def array_class
  Array
end

.attribute(name, opts = {}, &block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/form_obj/struct.rb', line 24

def attribute(name, opts = {}, &block)
  attr = attribute_class.new(name, opts.merge(parent: self), &block)
  self._attributes = _attributes.add(attr)
  _define_attribute_getter(attr)
  _define_attribute_setter(attr)
  self
end

.attribute_classObject



20
21
22
# File 'lib/form_obj/struct.rb', line 20

def attribute_class
  Attribute
end

.attributesObject



32
33
34
# File 'lib/form_obj/struct.rb', line 32

def attributes
  self._attributes.map(&:name)
end

.inspectObject



36
37
38
# File 'lib/form_obj/struct.rb', line 36

def inspect
  "#{name}#{attributes.size > 0 ? "(#{attributes.join(', ')})" : ''}"
end

.nested_classObject



16
17
18
# File 'lib/form_obj/struct.rb', line 16

def nested_class
  ::FormObj::Struct
end

Instance Method Details

#inspectObject



59
60
61
# File 'lib/form_obj/struct.rb', line 59

def inspect
  "#<#{self.class.name} #{self.class._attributes.map { |attribute| "#{attribute.name}: #{send(attribute.name).inspect}"}.join(', ')}>"
end

#to_hashObject



55
56
57
# File 'lib/form_obj/struct.rb', line 55

def to_hash
  Hash[self.class._attributes.map { |attribute| [attribute.name, attribute.subform? ? send(attribute.name).to_hash : send(attribute.name)] }]
end