Module: PriceHubble::Concern::Attributes
- Extended by:
- ActiveSupport::Concern
- Includes:
- DateArray, Enum, Range, StringInquirer
- Included in:
- BaseEntity
- Defined in:
- lib/price_hubble/entity/concern/attributes.rb,
lib/price_hubble/entity/concern/attributes/enum.rb,
lib/price_hubble/entity/concern/attributes/range.rb,
lib/price_hubble/entity/concern/attributes/date_array.rb,
lib/price_hubble/entity/concern/attributes/string_inquirer.rb
Overview
An ActiveRecord-like attribute management feature, with the exception that the attributes are not generated through a schema file, but are defined inline the entity class.
Defined Under Namespace
Modules: DateArray, Enum, Range, StringInquirer
Class Method Summary collapse
-
.inherited_setup_attributes(child_class) ⇒ Object
Initialize the attributes structures on an inherited class.
-
.tracked_attr(*args) ⇒ Object
Register tracked attributes of the entity.
-
.typed_attr(name, type, **args) ⇒ Object
(Re-)Register attributes with strict type casts.
Instance Method Summary collapse
-
#assign_attributes(struct = {}) ⇒ Mixed
rubocop:enable Metrics/MethodLength A wrapper for the ActiveModel#assign_attributes method with support for unmapped attributes.
-
#attributes(sanitize: false) ⇒ Hash{String => Mixed}
Export all attributes as data hash, as requested by the ActiveModel API.
Methods included from StringInquirer
Methods included from Range
Methods included from Enum
Methods included from DateArray
Class Method Details
.inherited_setup_attributes(child_class) ⇒ Object
Initialize the attributes structures on an inherited class.
140 141 142 |
# File 'lib/price_hubble/entity/concern/attributes.rb', line 140 def inherited_setup_attributes(child_class) child_class.attribute_names = [] end |
.tracked_attr(*args) ⇒ Object
Register tracked attributes of the entity. This adds the attributes to the Class.attribute_names collection, generates getters and setters as well sets up the dirty-tracking of these attributes.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/price_hubble/entity/concern/attributes.rb', line 149 def tracked_attr(*args) # Register the attribute names, for easy access self.attribute_names += args # Define getters/setters attr_reader(*args) args.each do |arg| class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{arg}=(value) #{arg}_will_change! @#{arg} = value end RUBY end # Register the attributes for ActiveModel define_attribute_methods(*args) end |
.typed_attr(name, type, **args) ⇒ Object
(Re-)Register attributes with strict type casts. This adds additional reader methods as well as a writer with casted type.
173 174 175 |
# File 'lib/price_hubble/entity/concern/attributes.rb', line 173 def typed_attr(name, type, **args) send("typed_attr_#{type}", name, **args) end |
Instance Method Details
#assign_attributes(struct = {}) ⇒ Mixed
rubocop:enable Metrics/MethodLength A wrapper for the ActiveModel#assign_attributes method with support for unmapped attributes. These attributes are put into the _unmapped struct and all the known attributes are assigned like normal. This allows the client to be forward compatible with changing APIs.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/price_hubble/entity/concern/attributes.rb', line 57 def assign_attributes(struct = {}) # Build a RecursiveOpenStruct and a simple hash from the given data struct, hash = sanitize_data(struct) # Initialize associations and map them accordingly struct, hash = initialize_associations(struct, hash) # Initialize attributes and map unknown ones and pass back the known known = initialize_attributes(struct, hash) # Mass assign the known attributes via ActiveModel super(known) end |
#attributes(sanitize: false) ⇒ Hash{String => Mixed}
Export all attributes as data hash, as requested by the ActiveModel API.
rubocop:disable Metrics/MethodLength – because of the key/value
sanitization handling
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/price_hubble/entity/concern/attributes.rb', line 30 def attributes(sanitize: false) attribute_names.each_with_object({}) do |key, memo| reader = key if sanitize sanitizer = :"sanitize_attr_#{key}" reader = methods.include?(sanitizer) ? sanitizer : key key_sanitizer = :"sanitize_attr_key_#{key}" key = send(key_sanitizer) if methods.include?(key_sanitizer) end result = resolve_attributes(send(reader)) memo[key.to_s] = result end end |