Class: DataMiner::Import

Inherits:
Object
  • Object
show all
Includes:
Blockenspiel::DSL
Defined in:
lib/data_miner/import.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, position_in_run, description, table_options = {}) ⇒ Import

Returns a new instance of Import.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/data_miner/import.rb', line 10

def initialize(base, position_in_run, description, table_options = {})
  table_options.symbolize_keys!

  @attributes = ActiveSupport::OrderedHash.new
  @base = base
  @position_in_run = position_in_run
  @description = description
  
  if table_options[:errata].is_a?(String)
    table_options[:errata] = Errata.new :url => table_options[:errata], :responder => resource
  end
    
  if table_options[:table].present?
    DataMiner.log_or_raise "You should specify :table or :url, but not both" if table_options[:url].present?
    @table = table_options[:table]
  else
    @table = RemoteTable.new table_options
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/data_miner/import.rb', line 5

def attributes
  @attributes
end

#baseObject

Returns the value of attribute base.



6
7
8
# File 'lib/data_miner/import.rb', line 6

def base
  @base
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/data_miner/import.rb', line 7

def description
  @description
end

#position_in_runObject

Returns the value of attribute position_in_run.



6
7
8
# File 'lib/data_miner/import.rb', line 6

def position_in_run
  @position_in_run
end

#tableObject

Returns the value of attribute table.



6
7
8
# File 'lib/data_miner/import.rb', line 6

def table
  @table
end

Instance Method Details

#inspectObject



30
31
32
# File 'lib/data_miner/import.rb', line 30

def inspect
  "Import(#{resource}) position #{position_in_run} (#{description})"
end

#key(attr_name, attr_options = {}) ⇒ Object



43
44
45
46
47
# File 'lib/data_miner/import.rb', line 43

def key(attr_name, attr_options = {})
  DataMiner.log_or_raise "You should only call store or key once for #{resource.name}##{attr_name}" if attributes.has_key? attr_name
  @key = attr_name
  store attr_name, attr_options
end

#run(run) ⇒ Object



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

def run(run)
  primary_key = resource.primary_key
  test_counter = 0

  table.each_row do |row|
    if ENV['DUMP'] == 'true'
      raise "[data_miner gem] Stopping after 5 rows because TEST=true" if test_counter > 5
      test_counter += 1
      DataMiner.log_info %{Row #{test_counter}
IN:  #{row.inspect}
OUT: #{attributes.inject(Hash.new) { |memo, v| attr_name, attr = v; memo[attr_name] = attr.value_from_row(row); memo }.inspect}
      }
    end
  
    record = resource.send "find_or_initialize_by_#{@key}", attributes[@key].value_from_row(row)
    attributes.each { |_, attr| attr.set_record_from_row record, row }
    record.save! if record.send(primary_key).present?
  end
  DataMiner.log_info "performed #{inspect}"
end

#store(attr_name, attr_options = {}) ⇒ Object



38
39
40
41
# File 'lib/data_miner/import.rb', line 38

def store(attr_name, attr_options = {})
  DataMiner.log_or_raise "You should only call store or key once for #{resource.name}##{attr_name}" if attributes.has_key? attr_name
  attributes[attr_name] = Attribute.new self, attr_name, attr_options
end

#stores?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/data_miner/import.rb', line 34

def stores?(attr_name)
  attributes.has_key? attr_name
end