Module: EntityStore::Attributes::ClassMethods
- Defined in:
- lib/entity_store/attributes.rb
Instance Method Summary collapse
- #_eval_entity_value_setter(value, klass) ⇒ Object
- #entity_value_array_attribute(name, klass) ⇒ Object
- #entity_value_attribute(name, klass) ⇒ Object
- #entity_value_dictionary_attribute(name, klass) ⇒ Object
Instance Method Details
#_eval_entity_value_setter(value, klass) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/entity_store/attributes.rb', line 17 def _eval_entity_value_setter value, klass case value when Array klass.new(Hash[*value.flatten]) when Hash klass.new(value) else value end end |
#entity_value_array_attribute(name, klass) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/entity_store/attributes.rb', line 28 def entity_value_array_attribute name, klass variable_name = "@_#{name}".to_sym define_method(name) do instance_variable_get(variable_name) || instance_variable_set(variable_name, []) end define_method("#{name}=") do |values| mapped_values = (values || []).map do |value| case value when Hash klass.new(value) when klass value else raise ArgumentError.new("#{value.class} not supported. Expecting #{klass.name}") end end instance_variable_set(variable_name, mapped_values) end end |
#entity_value_attribute(name, klass) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/entity_store/attributes.rb', line 10 def entity_value_attribute name, klass define_method(name) { instance_variable_get("@#{name}") } define_method("#{name}=") do |value| instance_variable_set("@#{name}", self.class._eval_entity_value_setter(value, klass)) end end |
#entity_value_dictionary_attribute(name, klass) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/entity_store/attributes.rb', line 51 def entity_value_dictionary_attribute name, klass define_method("#{name}_dictionary") { instance_variable_get("@_#{name}_dictionary") || instance_variable_set("@_#{name}_dictionary", {}) } define_method("#{name}_dictionary=") do |value| value.each_pair do |key, item| case item when Hash send("#{name}_dictionary")[key] = klass.new(item) when klass send("#{name}_dictionary")[key] = item else raise ArgumentError.new("#{item.class.name} not supported. Expecting #{klass.name}") end end end end |