# File lib/form_obj/struct.rb, line 68 def primary_key send(self.class.primary_key) end
class FormObj::Struct
Public Class Methods
array_class()
click to toggle source
# File lib/form_obj/struct.rb, line 20 def array_class Array end
attribute(name, opts = {}, &block)
click to toggle source
# File lib/form_obj/struct.rb, line 32 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_class()
click to toggle source
# File lib/form_obj/struct.rb, line 28 def attribute_class Attribute end
attributes()
click to toggle source
# File lib/form_obj/struct.rb, line 40 def attributes _attributes.map(&:name) end
inspect()
click to toggle source
# File lib/form_obj/struct.rb, line 44 def inspect "#{name}#{attributes.size > 0 ? "(#{attributes.join(', ')})" : ''}" end
nested_class()
click to toggle source
# File lib/form_obj/struct.rb, line 24 def nested_class ::FormObj::Struct end
new(*args)
click to toggle source
Calls superclass method
# File lib/form_obj/struct.rb, line 63 def initialize(*args) super() update_attributes(*args) if args.size > 0 && args[0] end
Private Class Methods
_define_attribute_getter(attribute)
click to toggle source
# File lib/form_obj/struct.rb, line 50 def _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/form_obj/struct.rb, line 56 def _define_attribute_setter(attribute) define_method "#{attribute.name}=" do |value| _set_attribute_value(attribute, value) end end
Public Instance Methods
inspect()
click to toggle source
# File lib/form_obj/struct.rb, line 98 def inspect "#<#{inner_inspect}>" end
primary_key()
click to toggle source
primary_key=(val)
click to toggle source
# File lib/form_obj/struct.rb, line 72 def primary_key=(val) send("#{self.class.primary_key}=", val) end
to_hash()
click to toggle source
# File lib/form_obj/struct.rb, line 94 def to_hash Hash[self.class._attributes.map { |attribute| [attribute.name, attribute.subform? ? read_attribute(attribute).to_hash : read_attribute(attribute)] }] end
update_attributes(attrs, raise_if_not_found: true)
click to toggle source
# File lib/form_obj/struct.rb, line 76 def update_attributes(attrs, raise_if_not_found: true) attrs = HashWithIndifferentAccess.new(attrs) unless attrs.is_a? HashWithIndifferentAccess attrs.each_pair do |attr_name, attr_value| attr = self.class._attributes.find(attr_name) if attr.nil? raise UnknownAttributeError.new(attr_name) if raise_if_not_found else if attr.subform? && !attr_value.is_a?(FormObj::Struct) read_attribute(attr).update_attributes(attr_value, raise_if_not_found: raise_if_not_found) else update_attribute(attr, attr_value) end end end self end
Private Instance Methods
_get_attribute_value(attribute)
click to toggle source
# File lib/form_obj/struct.rb, line 121 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/form_obj/struct.rb, line 127 def _set_attribute_value(attribute, value) attribute.validate_value!(value) instance_variable_set("@#{attribute.name}", value) end
inner_inspect()
click to toggle source
# File lib/form_obj/struct.rb, line 104 def inner_inspect attributes = self.class._attributes.map { |attribute| "#{attribute.name}: #{read_attribute(attribute).inspect}"}.join(', ') "#{self.class.name}#{attributes.size > 0 ? " #{attributes}" : ''}" end
read_attribute(attribute)
click to toggle source
# File lib/form_obj/struct.rb, line 113 def read_attribute(attribute) send(attribute.name) end
update_attribute(attribute, new_value)
click to toggle source
# File lib/form_obj/struct.rb, line 109 def update_attribute(attribute, new_value) write_attribute(attribute, new_value) end
write_attribute(attribute, value)
click to toggle source
# File lib/form_obj/struct.rb, line 117 def write_attribute(attribute, value) send("#{attribute.name}=", value) end