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
-
#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 |
# File 'lib/stockboy/candidate_record.rb', line 18 def initialize(attrs, map) @map = map @table = use_frozen_keys(attrs, map) @tr_table = Hash.new freeze end |
Instance Method Details
#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
82 83 84 |
# File 'lib/stockboy/candidate_record.rb', line 82 def input SourceRecord.new(self.raw_hash, @table) end |
#output ⇒ MappedRecord
Data structure representing the record’s mapped & translated output values
93 94 95 |
# File 'lib/stockboy/candidate_record.rb', line 93 def output MappedRecord.new(self.to_hash) end |
#partition(filters = {}) ⇒ Symbol
Find the filter key that captures this record
61 62 63 64 65 66 67 68 69 |
# File 'lib/stockboy/candidate_record.rb', line 61 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
40 41 42 43 44 |
# File 'lib/stockboy/candidate_record.rb', line 40 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
29 30 31 32 33 |
# File 'lib/stockboy/candidate_record.rb', line 29 def to_hash Hash.new.tap do |out| @map.each { |col| out[col.to] = translate(col) } end end |
#to_model(model) ⇒ Class
Wrap the mapped attributes in a new ActiveModel or ActiveRecord object
52 53 54 |
# File 'lib/stockboy/candidate_record.rb', line 52 def to_model(model) model.new(attributes) end |