Module: Assimilate

Defined in:
lib/assimilate.rb,
lib/assimilate/version.rb

Defined Under Namespace

Classes: Batch, Catalog, Command, CorruptDataError, DuplicateImportError, Extender, InvalidConfiguration

Constant Summary collapse

VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.extend_data(filename, opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/assimilate.rb', line 40

def self.extend_data(filename, opts = {})
  begin
    catalog = Catalog.new(:config => opts[:config])
    extender = catalog.extend_data(opts)
    slurp(filename) do |rec|
      extender << rec
    end
    if opts[:commit]
      extender.commit
    else
      $stderr.puts "(suppressing data commit)"

      if ENV['ASSIM_VERBOSE']
        $stderr.puts "UPDATES:"
        extender.changes.each do |recid|
          $stderr.puts "#{recid}: #{extender.seen[recid]}"
        end
      end
    end
    extender.stats
  end
end

.load(filename, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/assimilate.rb', line 14

def self.load(filename, opts = {})
  begin
    catalog = Catalog.new(:config => opts[:config])
    batcher = catalog.start_batch(opts.merge(:filename => filename))

    headers = nil
    slurp(filename) do |rec|
      if opts[:subset] && !headers
        headers = rec.keys
        batcher.prime(headers)
      end
      batcher << rec
    end
    if opts[:commit]
      batcher.commit
    else
      $stderr.puts "(suppressing data commit)"
    end
    batcher.stats
  # TODO explicit handling for Assimilate exceptions - when code is stable
  # rescue Assimilate::DuplicateImportError => e
  #   $stderr.puts e.message
  #   exit 1
  end
end

.slurp(filename) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/assimilate.rb', line 63

def self.slurp(filename)
  headers = nil
  CSV.read(filename).each do |row|
    if !headers
      headers = row.to_a
    else
      raise "Row count mismatch: #{row} vs #{headers}" if row.count > headers.count
      hash = {}
      row.zip(headers) do |v,k|
        hash[k] = v.strip unless v.blank?
      end
      yield hash
    end
  end
end