Class: Assimilate::Extender
- Inherits:
-
Object
- Object
- Assimilate::Extender
- Defined in:
- lib/assimilate/extender.rb
Instance Attribute Summary collapse
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#idfield ⇒ Object
readonly
Returns the value of attribute idfield.
-
#keyfield ⇒ Object
readonly
Returns the value of attribute keyfield.
Instance Method Summary collapse
- #<<(record) ⇒ Object
-
#apply_inserts ⇒ Object
an “insert” here means a record for which we have extended data but does not appear in the current catalog, so we need to create a stub entry.
-
#apply_updates ⇒ Object
“update” means store the extended data in the record (which must exist).
-
#commit ⇒ Object
write all the changes to the catalog.
-
#initialize(args) ⇒ Extender
constructor
A new instance of Extender.
- #load_baseline ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(args) ⇒ Extender
Returns a new instance of Extender.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/assimilate/extender.rb', line 4 def initialize(args) @catalog = args[:catalog] @domainkey = @catalog.config[:domain] @domain = args[:domain] @idfield = args[:idfield] @filename = args[:filename] @keyfield = args[:key] load_baseline @noops = [] @changes = [] @adds = [] @deletes = [] end |
Instance Attribute Details
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
2 3 4 |
# File 'lib/assimilate/extender.rb', line 2 def domain @domain end |
#idfield ⇒ Object (readonly)
Returns the value of attribute idfield.
2 3 4 |
# File 'lib/assimilate/extender.rb', line 2 def idfield @idfield end |
#keyfield ⇒ Object (readonly)
Returns the value of attribute keyfield.
2 3 4 |
# File 'lib/assimilate/extender.rb', line 2 def keyfield @keyfield end |
Instance Method Details
#<<(record) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/assimilate/extender.rb', line 32 def <<(record) @seen ||= Hash.new(0) hash = record.to_hash key = hash[@idfield] data = hash.reject {|k,v| k == idfield} # @seen[key] = data current_record = @baseline[key] if current_record if current_record[@keyfield] == data @noops << key @seen[key] = {} else @changes << key @seen[key] = data end else @adds << key @seen[key] = data end end |
#apply_inserts ⇒ Object
an “insert” here means a record for which we have extended data but does not appear in the current catalog, so we need to create a stub entry.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/assimilate/extender.rb', line 76 def apply_inserts @adds.each do |key| data = @seen[key] @catalog.catalog.insert( @domainkey => domain, idfield => key, keyfield => data ) end end |
#apply_updates ⇒ Object
“update” means store the extended data in the record (which must exist)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/assimilate/extender.rb', line 88 def apply_updates @changes.each do |key| data = @seen[key] @catalog.catalog.update( { @domainkey => domain, idfield => key }, {"$set" => { keyfield => data } } ) end end |
#commit ⇒ Object
write all the changes to the catalog
68 69 70 71 |
# File 'lib/assimilate/extender.rb', line 68 def commit apply_inserts apply_updates end |
#load_baseline ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/assimilate/extender.rb', line 21 def load_baseline stored_records = @catalog.catalog.find(@domainkey => @domain).to_a @baseline = stored_records.each_with_object({}) do |rec, h| key = rec[@idfield] if h.include?(key) raise Assimilate::CorruptDataError, "Duplicate records for key [#{key}] in #{@domainkey} [#{@domain}]" end h[key] = rec end end |
#stats ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/assimilate/extender.rb', line 54 def stats { :baseline_count => @baseline.size, :final_count => @baseline.size + @adds.count, :distinct_ids => @seen.size, :adds_count => @adds.count, :new_ids => @adds, :updates_count => @changes.count, :updated_fields => @seen.each_with_object(Hash.new(0)) {|(k,hash),memo| hash.each {|k,v| memo[k] += 1}}, :unchanged_count => @noops.count } end |