Class: Assimilate::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/assimilate/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/assimilate/command.rb', line 6

def initialize
  @options = {}
  @parser = OptionParser.new do |opts|
    opts.banner = "Usage: assimilate [command] [options]"

    opts.on("--config FILENAME", String, "Catalog database configuration file") do |f|
      @options[:config] = f
    end

    opts.on("--id FIELDNAME", String, "Field name to be used for record identifier") do |f|
      @options[:idfield] = f
    end

    opts.on("--commit", "Commit changes to database") do
      @options[:commit] = true
    end

    opts.on("--key FIELDNAME", String, "(*extend* only) Hash key to store extended attributes under") do |f|
      @options[:key] = f
    end

    opts.on("--datestamp DATESTRING", String, "(*load* only) Datestamp to record for file batch operation") do |s|
      @options[:datestamp] = s
    end

    opts.on("--domain STRING", String, "Domain value to apply to each record") do |s|
      @options[:domain] = s
    end
  end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



4
5
6
# File 'lib/assimilate/command.rb', line 4

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/assimilate/command.rb', line 4

def options
  @options
end

Instance Method Details

#execute(command, options, filenames = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/assimilate/command.rb', line 50

def execute(command, options, filenames = nil)
  filename = filenames.first

  case command
  when 'load'
    raise OptionParser::MissingArgument, "missing datestamp" unless options[:datestamp]

    results = Assimilate.load(filename, options)
    logmessage(command, options, results)

  when 'extend'
    raise OptionParser::MissingArgument, "missing keyfield" unless options[:key]

    results = Assimilate.extend_data(filename, options)
    logmessage(command, options, results)


  else
    raise "unknown command #{command}"
  end
end

#logmessage(command, options, results) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/assimilate/command.rb', line 72

def logmessage(command, options, results)
  $stderr.puts <<-EOT
* assimilate #{command} (#{options.keys.join(', ')})
EOT

  case command
  when 'load'
    $stderr.puts <<-EOT
  Original record count: #{results[:baseline_count]}
     Final record count: #{results[:final_count]}
      Unchanged records: #{results[:unchanged_count]}
            New records: #{results[:adds_count]} (#{results[:new_ids].take(10).join(',')})
                Deletes: #{results[:deletes_count]} (#{results[:deleted_ids].take(10).join(',')})
                Updates: #{results[:updates_count]}
EOT
    if results[:updated_fields].any?
      $stderr.puts <<-EOT
          Counts by field:
EOT
      results[:updated_fields].each do |k,v|
        $stderr.puts <<-EOT
                      #{k}: #{v}
EOT
      end
    end
  when 'extend'
    $stderr.puts <<-EOT
  Original record count: #{results[:baseline_count]}
     Final record count: #{results[:final_count]}
        New identifiers: #{results[:adds_count]} #{options[:idfield]} (#{results[:new_ids].take(10).join(',')})
           Distinct ids: #{results[:distinct_ids]}
      Unchanged records: #{results[:unchanged_count]}
                Updates: #{results[:updates_count]}
EOT
    if results[:updated_fields].any?
      results[:updated_fields].each do |k,v|
        $stderr.puts <<-EOT
                      #{options[:key]}.#{k}: #{v}
EOT
      end
    end
  else
    $stderr.puts results.inspect
  end
end

#parse(argv = ARGV) ⇒ Object

Raises:

  • (OptionParser::MissingArgument)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/assimilate/command.rb', line 37

def parse(argv = ARGV)
  @command = argv.shift
  filenames = @parser.parse(argv)

  raise OptionParser::MissingArgument, "missing config" unless options[:config]
  raise OptionParser::MissingArgument, "missing idfield" unless options[:idfield]
  raise OptionParser::MissingArgument, "missing domain" unless options[:domain]
  raise "missing filename" unless filenames.any?

  # argv remnants are filenames
  [@command, @options, filenames]
end