Class: PriceHubble::BaseEntity

Inherits:
Object
  • Object
show all
Extended by:
Utils::Bangers
Includes:
ActiveModel::Dirty, ActiveModel::Model, EntityConcern::Associations, EntityConcern::Attributes, EntityConcern::Callbacks, EntityConcern::Client, EntityConcern::Persistence, Utils::Bangers
Defined in:
lib/pricehubble/entity/base_entity.rb

Overview

The base entity, with a lot of known ActiveRecord/ActiveModel features.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Bangers

bangers

Methods included from EntityConcern::Persistence

#destroyed?, #mark_as_destroyed, #new_record?, #persisted?

Methods included from EntityConcern::Client

#client, client

Methods included from EntityConcern::Associations

has_many, has_one, inherited_setup_associations

Methods included from EntityConcern::Attributes

#assign_attributes, #attributes, inherited_setup_attributes, tracked_attr, typed_attr

Methods included from EntityConcern::Attributes::StringInquirer

typed_attr_string_inquirer

Methods included from EntityConcern::Attributes::Range

typed_attr_range

Methods included from EntityConcern::Attributes::Enum

typed_attr_enum

Methods included from EntityConcern::Attributes::DateArray

typed_attr_date_array

Constructor Details

#initialize(struct = {}) {|PriceHubble::BaseEntity| ... } ⇒ PriceHubble::BaseEntity

Create a new instance of an entity with a lot of known ActiveRecord/ActiveModel features.

Parameters:

  • struct (Hash{Mixed => Mixed}, RecursiveOpenStruct) (defaults to: {})

    the initial data

Yields:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pricehubble/entity/base_entity.rb', line 34

def initialize(struct = {})
  # Set the initial unmapped struct
  self._unmapped = RecursiveOpenStruct.new
  # 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)
  # Follow the ActiveRecord API
  yield self if block_given?
  # Run the initializer callbacks
  _run_initialize_callbacks
end

Instance Attribute Details

#_unmappedObject

We collect all unknown attributes instead of raising while creating a new instance. The unknown attributes are wrapped inside a RecursiveOpenStruct to ease the accessibility. This also allows us to handle responses in a forward-compatible way.



26
27
28
# File 'lib/pricehubble/entity/base_entity.rb', line 26

def _unmapped
  @_unmapped
end

Class Method Details

.inherited(child_class) ⇒ Object

Initialize the class we were inherited to. We trigger all our methods which start with inherited_setup_ to allow per-concern/feature based initialization after BaseEntity inheritance.

Parameters:

  • child_class (Class)

    the child class which inherits us



57
58
59
60
61
62
# File 'lib/pricehubble/entity/base_entity.rb', line 57

def inherited(child_class)
  super
  match = ->(sym) { sym.to_s.start_with? 'inherited_setup_' }
  trigger = ->(sym) { send(sym, child_class) }
  methods.select(&match).each(&trigger)
end