Module: Morpheus::Mixins::Attributes
- Included in:
- Base
- Defined in:
- lib/morpheus/mixins/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes_without_basic_attributes ⇒ Object
- #read_attribute_for_validation(key) ⇒ Object
- #typecast(attribute, value) ⇒ Object
- #update_attribute(attribute, value) ⇒ Object
- #update_reflection(reflection, attribute, value) ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 |
# File 'lib/morpheus/mixins/attributes.rb', line 5 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#attributes ⇒ Object
47 48 49 |
# File 'lib/morpheus/mixins/attributes.rb', line 47 def attributes @attributes ||= self.class.default_attributes.dup end |
#attributes_without_basic_attributes ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/morpheus/mixins/attributes.rb', line 59 def attributes_without_basic_attributes attributes_to_reject = %w( id errors valid ) self.class.reflections.keys.each do |key| attributes_to_reject.push(key.to_s) end attributes.reject do |key, value| attributes_to_reject.include?(key) end end |
#read_attribute_for_validation(key) ⇒ Object
51 52 53 |
# File 'lib/morpheus/mixins/attributes.rb', line 51 def read_attribute_for_validation(key) attributes[key] end |
#typecast(attribute, value) ⇒ Object
55 56 57 |
# File 'lib/morpheus/mixins/attributes.rb', line 55 def typecast(attribute, value) TypeCaster.cast(value, self.class.typecast_attributes[attribute.to_sym]) end |
#update_attribute(attribute, value) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/morpheus/mixins/attributes.rb', line 69 def update_attribute(attribute, value) reflection = self.class.reflect_on_association(attribute) if reflection update_reflection(reflection, attribute, value) else attributes[attribute.to_sym] = typecast(attribute, value) end end |
#update_reflection(reflection, attribute, value) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/morpheus/mixins/attributes.rb', line 78 def update_reflection(reflection, attribute, value) return unless value if reflection.macro == :has_many # need to construct each member of array one-by-one association_object = send(attribute) value.each do |a_value| if a_value.instance_of? reflection.klass target = a_value else target = reflection.build_association(a_value) end association_object << target end elsif reflection.macro == :belongs_to if value.instance_of? reflection.klass target = value else if reflection.[:polymorphic] polymorphic_class = send("#{reflection.name}_type".to_sym) polymorphic_class = value['type'] if value.include?('type') target = polymorphic_class.constantize.new(value) else target = reflection.build_association(value) end end send("#{attribute}=", target) else if value.instance_of? reflection.klass target = value else target = reflection.build_association(value) end send("#{attribute}=", target) end end |