class Chronicle::ETL::Models::Base
Represents a record that's been transformed by a Transformer
and ready to be loaded. Loosely based on ActiveModel.
Constants
- ASSOCIATIONS
- ATTRIBUTES
Attributes
dedupe_on[RW]
id[RW]
Public Class Methods
new(attributes = {})
click to toggle source
# File lib/chronicle/etl/models/base.rb, line 14 def initialize(attributes = {}) assign_attributes(attributes) if attributes @dedupe_on = [] end
Public Instance Methods
associations()
click to toggle source
All of this record's associations
# File lib/chronicle/etl/models/base.rb, line 66 def associations association_list = ASSOCIATIONS + self.class::ASSOCIATIONS attributes = {} association_list.each do |attribute| instance_variable = "@#{attribute.to_s}" association = self.instance_variable_get(instance_variable) attributes[attribute] = association if association end attributes.compact end
associations_hash()
click to toggle source
# File lib/chronicle/etl/models/base.rb, line 77 def associations_hash Hash[associations.map do |k, v| [k, v.to_h] end] end
attribute_list()
click to toggle source
Set of attribute names that this model has is Base's shared attributes combined with the child class's
# File lib/chronicle/etl/models/base.rb, line 51 def attribute_list (ATTRIBUTES + self.class::ATTRIBUTES).uniq end
attributes()
click to toggle source
All of this record's attributes
# File lib/chronicle/etl/models/base.rb, line 56 def attributes attributes = {} attribute_list.each do |attribute| instance_variable = "@#{attribute.to_s}" attributes[attribute] = self.instance_variable_get(instance_variable) end attributes.compact end
generate_lid(fields)
click to toggle source
For a given set of fields of this model, generate a unique local id by hashing the field values
# File lib/chronicle/etl/models/base.rb, line 38 def generate_lid fields values = fields.sort.map do |field| instance_variable = "@#{field.to_s}" self.instance_variable_get(instance_variable) end return if values.any? { |e| e.nil? } Digest::SHA256.hexdigest(values.join(",")) end
identifier_hash()
click to toggle source
A unique identifier for this model is formed from a type and either an id or lids.
# File lib/chronicle/etl/models/base.rb, line 21 def identifier_hash { type: self.class::TYPE, id: @id, lids: lids }.compact end
lids()
click to toggle source
Array of local ids that uniquely identify this record
# File lib/chronicle/etl/models/base.rb, line 30 def lids @dedupe_on.map do |fields| generate_lid(fields) end.compact.uniq end
to_h()
click to toggle source
# File lib/chronicle/etl/models/base.rb, line 88 def to_h identifier_hash.merge(attributes).merge(associations_hash) end
to_h_flattened()
click to toggle source
FIXME: move this to a Utils
module
# File lib/chronicle/etl/models/base.rb, line 84 def to_h_flattened Chronicle::ETL::Utils::HashUtilities.flatten_hash(to_h) end
Private Instance Methods
assign_attributes(attributes)
click to toggle source
# File lib/chronicle/etl/models/base.rb, line 94 def assign_attributes attributes attributes.each do |k, v| setter = :"#{k}=" public_send(setter, v) if respond_to? setter end end