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
40
41
42
43
44
45
46
47
# 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("--subset", "Only consider the fields in the input file") do
      @options[:subset] = true
    end

    opts.on("--nodeletes", "Do NOT delete existing records that are not present in the input") do
      @options[:nodeletes] = 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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/assimilate/command.rb', line 64

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

  case command
  when 'version'
    puts "Assimilate #{Assimilate::VERSION}"

  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



89
90
91
92
93
# File 'lib/assimilate/command.rb', line 89

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

#logmessage(command, options, results) ⇒ Object



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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/assimilate/command.rb', line 95

def logmessage(command, options, results)
  $stderr.puts "assimilate \#{command} for \#{options[:domain]} using \#{options[:idfield]}\n"

  case command
  when 'load'
    $stderr.puts "  Original record count: \#{results[:baseline_count]}\n     Final record count: \#{results[:final_count]}\n      Unchanged records: \#{results[:unchanged_count]}\n            New records: \#{results[:adds_count]} \#{idsummary(results[:new_ids])}\n                Deletes: \#{results[:deletes_count]} \#{idsummary(results[:deleted_ids])}\n"
    if options[:nodeletes]
      warn "\t\t\tNO DELETIONS"
    end

    warn "                Updates: \#{results[:updates_count]} \#{idsummary(results[:updated_ids])}\n"
    if results[:updated_fields].any?
      $stderr.puts " Update counts by field:\n"
      results[:updated_fields].sort.each do |k,v|
        $stderr.puts "              \#{k.ljust(30, '.')}\#{\"%6d\" % v}\n"
      end
    end
  when 'extend'
    $stderr.puts "  Original record count: \#{results[:baseline_count]}\n     Final record count: \#{results[:final_count]}\n        New identifiers: \#{results[:adds_count]} \#{options[:idfield]} (\#{results[:new_ids].take(10).join(',')})\n           Distinct ids: \#{results[:distinct_ids]}\n      Unchanged records: \#{results[:unchanged_count]}\n                Updates: \#{results[:updates_count]}\n"
    if results[:updated_fields].any?
      results[:updated_fields].each do |k,v|
        $stderr.puts "                      \#{options[:key] && options[:key] + '.'}\#{k}: \#{v}\n"
      end
    end
  else
    $stderr.puts results.inspect
  end
end

#parse(argv = ARGV) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/assimilate/command.rb', line 49

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

  unless command == 'version'
    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?
  end

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