Class: Stockboy::CandidateRecord

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(attrs, map) ⇒ CandidateRecord

Initialize a new candidate record

Parameters:

  • attrs (Hash)

    Raw key-values from source data

  • map (AttributeMap)

    Mapping and translations



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_hashHash

Mapped output hash including ignored values

Returns:

  • (Hash)


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

#inputSourceRecord

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

Examples:

input = candidate.input
input["RawEmail"] # => "[email protected]  "
input.email       # => "[email protected]  "

Returns:



96
97
98
# File 'lib/stockboy/candidate_record.rb', line 96

def input
  SourceRecord.new(raw_hash, @table)
end

#outputMappedRecord

Data structure representing the record’s mapped & translated output values

Examples:

input = candidate.output
output.email       # => "[email protected]"

Returns:



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

Parameters:

  • filters (FilterChain) (defaults to: {})

    List of filters to apply

Returns:

  • (Symbol)

    Name of the matched filter



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_hashHash Also known as: raw_attributes

Return the original values mapped to attribute keys

Returns:

  • (Hash)


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_hashHash Also known as: attributes

Convert the mapped output to a hash

Returns:

  • (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

Parameters:

  • model (Class)

    ActiveModel class

Returns:

  • (Class)

    ActiveModel class



66
67
68
# File 'lib/stockboy/candidate_record.rb', line 66

def to_model(model)
  model.new(attributes)
end