Class: TreeStruct

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

Defined Under Namespace

Classes: Array, Attribute, Attributes

Constant Summary collapse

VERSION =
"1.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.array_classObject



27
28
29
# File 'lib/tree_struct.rb', line 27

def self.array_class
  Array
end

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



39
40
41
42
43
44
45
# File 'lib/tree_struct.rb', line 39

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

.attribute_classObject



35
36
37
# File 'lib/tree_struct.rb', line 35

def self.attribute_class
  Attribute
end

.attributesObject



47
48
49
# File 'lib/tree_struct.rb', line 47

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

.nested_classObject



31
32
33
# File 'lib/tree_struct.rb', line 31

def self.nested_class
  ::TreeStruct
end

Instance Method Details

#to_hashObject



51
52
53
# File 'lib/tree_struct.rb', line 51

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