Module: PriceHubble::Concern::Associations
- Extended by:
- ActiveSupport::Concern
- Included in:
- BaseEntity
- Defined in:
- lib/price_hubble/entity/concern/associations.rb
Overview
Allow simple association mappings like ActiveRecord supports (eg. has_one, has_many).
Class Method Summary collapse
-
.has_many(entity, **args) ⇒ Object
Define a simple
has_manyassociation. -
.has_one(entity, **args) ⇒ Object
Define a simple
has_oneassociation. -
.inherited_setup_associations(child_class) ⇒ Object
Initialize the associations structures on an inherited class.
Class Method Details
.has_many(entity, **args) ⇒ Object
Define a simple has_many association.
Options
-
:class_name- the entity class to use, otherwise it is guessed -
:from- take the data from this attribute -
:fallback_from- otherwise take the data from the fallback -
:persist- whenever to send the associationattributes (default: false) -
:initialize- whenever to initialize an empty array
rubocop:disable Naming/PredicatePrefix – because we mimic the
ActiveRecord API here
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/price_hubble/entity/concern/associations.rb', line 181 def has_many(entity, **args) # Sanitize options entity = entity.to_sym opts = { class_name: nil, from: entity, persist: false } .merge(args).merge(type: :has_many) # Resolve the given entity to a class name, when no explicit class # name was given via options if opts[:class_name].nil? opts[:class_name] = entity.to_s.singularize.camelcase .prepend('PriceHubble::').constantize end # Register the association associations[entity] = opts # Generate getters and setters attr_accessor entity # Add the entity to the tracked attributes if it should be persisted tracked_attr entity if opts[:persist] end |
.has_one(entity, **args) ⇒ Object
Define a simple has_one association.
Options
-
:class_name- the entity class to use, otherwise it is guessed -
:from- take the data from this attribute -
:persist- whenever to send the associationattributes (default: false) -
:initialize- whenever to initialize an empty object
rubocop:disable Naming/PredicatePrefix – because we mimic the
ActiveRecord API here
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/price_hubble/entity/concern/associations.rb', line 145 def has_one(entity, **args) # Sanitize options entity = entity.to_sym opts = { class_name: nil, from: entity, persist: false } .merge(args).merge(type: :has_one) # Resolve the given entity to a class name, when no explicit class # name was given via options if opts[:class_name].nil? opts[:class_name] = entity.to_s.camelcase.prepend('PriceHubble::').constantize end # Register the association associations[entity] = opts # Generate getters and setters attr_accessor entity # Add the entity to the tracked attributes if it should be persisted tracked_attr entity if opts[:persist] end |
.inherited_setup_associations(child_class) ⇒ Object
Initialize the associations structures on an inherited class.
127 128 129 |
# File 'lib/price_hubble/entity/concern/associations.rb', line 127 def inherited_setup_associations(child_class) child_class.associations = {} end |