Class: Stockboy::CandidateRecord
- Inherits:
-
Object
- Object
- Stockboy::CandidateRecord
- Defined in:
- lib/stockboy/candidate_record.rb
Overview
Joins the raw data values to an attribute mapping to allow comparison of input/output values, conversion, and filtering
Instance Method Summary collapse
-
#bulk_hash ⇒ Hash
Mapped output hash including ignored values.
-
#initialize(attrs, map) ⇒ CandidateRecord
constructor
Initialize a new candidate record.
-
#input ⇒ SourceRecord
Data structure representing the record’s raw input values.
-
#output ⇒ MappedRecord
Data structure representing the record’s mapped & translated output values.
-
#partition(filters = {}) ⇒ Symbol
Find the filter key that captures this record.
-
#raw_hash ⇒ Hash
(also: #raw_attributes)
Return the original values mapped to attribute keys.
-
#to_hash ⇒ Hash
(also: #attributes)
Convert the mapped output to a hash.
-
#to_model(model) ⇒ Class
Wrap the mapped attributes in a new ActiveModel or ActiveRecord object.
Constructor Details
#initialize(attrs, map) ⇒ CandidateRecord
Initialize a new candidate record
18 19 20 21 22 23 24 |
# File 'lib/stockboy/candidate_record.rb', line 18 def initialize(attrs, map) @map = map @table = reuse_frozen_hash_keys(attrs, map) @tr_table = Hash.new @ignored_fields = [] freeze end |
Instance Method Details
#bulk_hash ⇒ Hash
Mapped output hash including ignored values
44 45 46 47 48 |
# File 'lib/stockboy/candidate_record.rb', line 44 def bulk_hash Hash.new.tap do |out| @map.each { |col| out[col.to] = translate(col) } end end |
#input ⇒ SourceRecord
Data structure representing the record’s raw input values
Values can be accessed like hash keys, or attribute names that correspond to a :from
attribute mapping option
96 97 98 |
# File 'lib/stockboy/candidate_record.rb', line 96 def input SourceRecord.new(raw_hash, @table) end |
#output ⇒ MappedRecord
Data structure representing the record’s mapped & translated output values
107 108 109 |
# File 'lib/stockboy/candidate_record.rb', line 107 def output MappedRecord.new(bulk_hash) end |
#partition(filters = {}) ⇒ Symbol
Find the filter key that captures this record
75 76 77 78 79 80 81 82 83 |
# File 'lib/stockboy/candidate_record.rb', line 75 def partition(filters={}) input, output = self.input, self.output filters.each_pair do |filter_key, f| if f.call(input, output) return filter_key end end nil end |
#raw_hash ⇒ Hash Also known as: raw_attributes
Return the original values mapped to attribute keys
54 55 56 57 58 |
# File 'lib/stockboy/candidate_record.rb', line 54 def raw_hash Hash.new.tap do |out| @map.each { |col| out[col.to] = @table[col.from] } end end |
#to_hash ⇒ Hash Also known as: attributes
Convert the mapped output to a hash
30 31 32 33 34 35 36 37 |
# File 'lib/stockboy/candidate_record.rb', line 30 def to_hash bulk_hash.tap do |out| tmp_context = SourceRecord.new(out, @table) @map.each_with_object(out) do |col| out.delete(col.to) if ignore?(col, tmp_context) end end end |
#to_model(model) ⇒ Class
Wrap the mapped attributes in a new ActiveModel or ActiveRecord object
66 67 68 |
# File 'lib/stockboy/candidate_record.rb', line 66 def to_model(model) model.new(attributes) end |