class TreeStruct

Constants

VERSION

Public Class Methods

array_class() click to toggle source
# File lib/tree_struct.rb, line 27
def self.array_class
  Array
end
attribute(name, opts = {}, &block) click to toggle source
# 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_class() click to toggle source
# File lib/tree_struct.rb, line 35
def self.attribute_class
  Attribute
end
attributes() click to toggle source
# File lib/tree_struct.rb, line 47
def self.attributes
  self._attributes.map(&:name)
end
nested_class() click to toggle source
# File lib/tree_struct.rb, line 31
def self.nested_class
  ::TreeStruct
end

Private Class Methods

_define_attribute_getter(attribute) click to toggle source
# File lib/tree_struct.rb, line 13
def self._define_attribute_getter(attribute)
  define_method attribute.name do
    _get_attribute_value(attribute)
  end
end
_define_attribute_setter(attribute) click to toggle source
# File lib/tree_struct.rb, line 19
def self._define_attribute_setter(attribute)
  define_method "#{attribute.name}=" do |value|
    _set_attribute_value(attribute, value)
  end
end

Public Instance Methods

to_hash() click to toggle source
# 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

Private Instance Methods

_get_attribute_value(attribute) click to toggle source
# File lib/tree_struct.rb, line 57
def _get_attribute_value(attribute)
  value = instance_variable_get("@#{attribute.name}")
  value = instance_variable_set("@#{attribute.name}", attribute.default_value) if value.nil?
  value
end
_set_attribute_value(attribute, value) click to toggle source
# File lib/tree_struct.rb, line 63
def _set_attribute_value(attribute, value)
  attribute.validate_value!(value)
  instance_variable_set("@#{attribute.name}", value)
end