Module: Her::Model::Attributes
Overview
This module handles all methods related to model attributes
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#assign_attributes(new_attributes) ⇒ Object
(also: #attributes=)
Assign new attributes to a resource.
- #attributes ⇒ Object
-
#id ⇒ Object
Return the value of the model ‘primary_key` attribute.
-
#initialize(attributes = {}) {|_self| ... } ⇒ Object
Initialize a new object with data.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
Handles missing methods
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/her/model/attributes.rb', line 41 def method_missing(method, *args, &blk) if method.to_s =~ /[?=]$/ || @_her_attributes.include?(method) # Extract the attribute attribute = method.to_s.sub(/[?=]$/, '') # Create a new `attribute` methods set self.class.attributes(*attribute) # Resend the method! send(method, *args, &blk) else super end end |
Instance Method Details
#assign_attributes(new_attributes) ⇒ Object Also known as: attributes=
Assign new attributes to a resource
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/her/model/attributes.rb', line 71 def assign_attributes(new_attributes) if !new_attributes.respond_to?(:to_hash) raise ArgumentError, "When assigning attributes, you must pass a hash as an argument." end # Coerce new_attributes to hash in case of strong parameters new_attributes = new_attributes.to_hash @_her_attributes ||= attributes # Use setter methods first unset_attributes = self.class.use_setter_methods(self, new_attributes) # Then translate attributes of associations into association instances associations = self.class.parse_associations(unset_attributes) # Then merge the associations into @_her_attributes. @_her_attributes.merge!(associations) end |
#attributes ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/her/model/attributes.rb', line 92 def attributes # The natural choice of instance variable naming here would be # `@attributes`. Unfortunately that causes a naming clash when # used with `ActiveModel` version >= 5.2.0. # As of v5.2.0 `ActiveModel` checks to see if `ActiveRecord` # attributes exist, and assumes that if the instance variable # `@attributes` exists on the instance, it is because they are # `ActiveRecord` attributes. @_her_attributes ||= HashWithIndifferentAccess.new end |
#id ⇒ Object
Return the value of the model ‘primary_key` attribute
119 120 121 |
# File 'lib/her/model/attributes.rb', line 119 def id @_her_attributes[self.class.primary_key] end |
#initialize(attributes = {}) {|_self| ... } ⇒ Object
Initialize a new object with data
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/her/model/attributes.rb', line 26 def initialize(attributes = {}) attributes ||= {} @metadata = attributes.delete(:_metadata) || {} @response_errors = attributes.delete(:_errors) || {} @destroyed = attributes.delete(:_destroyed) || false attributes = self.class.default_scope.apply_to(attributes) assign_attributes(attributes) yield self if block_given? run_callbacks :initialize end |