Module: PriceHubble::EntityConcern::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

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 association

    attributes (default: false)
    
  • :initialize - whenever to initialize an empty array

Parameters:

  • entity (String, Symbol)

    the attribute/entity name

  • args (Hash{Symbol => Mixed})

    additional options



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/price_hubble/entity/concern/associations.rb', line 176

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 association

    attributes (default: false)
    
  • :initialize - whenever to initialize an empty object

Parameters:

  • entity (String, Symbol)

    the attribute/entity name

  • args (Hash{Symbol => Mixed})

    additional options



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/price_hubble/entity/concern/associations.rb', line 144

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.

Parameters:

  • child_class (Class)

    the child class which inherits us



129
130
131
# File 'lib/price_hubble/entity/concern/associations.rb', line 129

def inherited_setup_associations(child_class)
  child_class.associations = {}
end