Class: DataMiner::Import
- Inherits:
-
Object
- Object
- DataMiner::Import
- Includes:
- Blockenspiel::DSL
- Defined in:
- lib/data_miner/import.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#base ⇒ Object
Returns the value of attribute base.
-
#description ⇒ Object
Returns the value of attribute description.
-
#position_in_run ⇒ Object
Returns the value of attribute position_in_run.
-
#table ⇒ Object
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(base, position_in_run, description, table_options = {}) ⇒ Import
constructor
A new instance of Import.
- #inspect ⇒ Object
- #key(attr_name, attr_options = {}) ⇒ Object
- #run(run) ⇒ Object
- #store(attr_name, attr_options = {}) ⇒ Object
- #stores?(attr_name) ⇒ Boolean
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, = {}) .symbolize_keys! @attributes = ActiveSupport::OrderedHash.new @base = base @position_in_run = position_in_run @description = description if [:errata].is_a?(String) [:errata] = Errata.new :url => [:errata], :responder => resource end if [:table].present? DataMiner.log_or_raise "You should specify :table or :url, but not both" if [:url].present? @table = [:table] else @table = RemoteTable.new end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/data_miner/import.rb', line 5 def attributes @attributes end |
#base ⇒ Object
Returns the value of attribute base.
6 7 8 |
# File 'lib/data_miner/import.rb', line 6 def base @base end |
#description ⇒ Object
Returns the value of attribute description.
7 8 9 |
# File 'lib/data_miner/import.rb', line 7 def description @description end |
#position_in_run ⇒ Object
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 |
#table ⇒ Object
Returns the value of attribute table.
6 7 8 |
# File 'lib/data_miner/import.rb', line 6 def table @table end |
Instance Method Details
#inspect ⇒ Object
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, = {}) 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, 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, = {}) 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, end |
#stores?(attr_name) ⇒ Boolean
34 35 36 |
# File 'lib/data_miner/import.rb', line 34 def stores?(attr_name) attributes.has_key? attr_name end |