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
36
37
38
39
# 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; optional) 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

    opts.on("--compare FIELDNAME", String, "(*extend* only) Optional field to check whether an update is current (e.g. timestamp)") do |f|
      @options[:compare] = f
    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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/assimilate/command.rb', line 54

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'
    # keyfield is an *optional* parameter

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


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

#idsummary(ids) ⇒ Object



76
77
78
79
80
# File 'lib/assimilate/command.rb', line 76

def idsummary(ids)
  if ids.any?
    "(#{ids.take(10).join(',')})"
  end
end

#logmessage(command, options, results) ⇒ Object



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
117
118
119
120
121
122
123
124
125
126
# File 'lib/assimilate/command.rb', line 82

def logmessage(command, options, results)
  $stderr.puts <<-EOT
assimilate #{command} for #{options[:domain]} using #{options[:idfield]}
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]} #{idsummary(results[:new_ids])}
                Deletes: #{results[:deletes_count]} #{idsummary(results[:deleted_ids])}
                Updates: #{results[:updates_count]} #{idsummary(results[:updated_ids])}
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] && options[:key] + '.'}#{k}: #{v}
EOT
      end
    end
  else
    $stderr.puts results.inspect
  end
end

#parse(argv = ARGV) ⇒ Object

Raises:

  • (OptionParser::MissingArgument)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/assimilate/command.rb', line 41

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