Class: Dwca::Import::Manager
- Inherits:
-
Object
- Object
- Dwca::Import::Manager
- Defined in:
- lib/dwca/import.rb
Instance Attribute Summary collapse
-
#available_objects ⇒ Object
Returns the value of attribute available_objects.
-
#data ⇒ Object
Returns the value of attribute data.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#field_index ⇒ Object
Returns the value of attribute field_index.
-
#row_number ⇒ Object
(also: #i)
Returns the value of attribute row_number.
-
#tw_objects ⇒ Object
Returns the value of attribute tw_objects.
Class Method Summary collapse
Instance Method Summary collapse
- #build_object(row, object) ⇒ Object
- #build_row_objects(row, row_objects) ⇒ Object
- #cell(row, attribute) ⇒ Object
-
#initialize(opts = {}) ⇒ Manager
constructor
A new instance of Manager.
-
#referenced_models(core_fields) ⇒ Array
protected
Given the core_fields what TW objects are possible.
-
#relate_row_objects(row, row_objects) ⇒ None
2nd pass.
- #row_id(row) ⇒ Integer
Constructor Details
#initialize(opts = {}) ⇒ Manager
Returns a new instance of Manager.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dwca/import.rb', line 16 def initialize(opts = {}) opts = { data: [], # dwc.core.read[0] errors: [], # dwc.core.read[1] core_fields: {}, # dwc.core.fields row_number: 1 # default starting row index }.merge!(opts) @data, @errors, @row_number = opts[:data], opts[:errors], opts[:row_number] @available_objects = referenced_models(opts[:core_fields]) @tw_objects = TwObjects.new() @field_index = opts[:core_fields].inject({}){|hsh, a| hsh.merge(a[:term] => (a[:index]) )} end |
Instance Attribute Details
#available_objects ⇒ Object
Returns the value of attribute available_objects.
9 10 11 |
# File 'lib/dwca/import.rb', line 9 def available_objects @available_objects end |
#data ⇒ Object
Returns the value of attribute data.
12 13 14 |
# File 'lib/dwca/import.rb', line 12 def data @data end |
#errors ⇒ Object
Returns the value of attribute errors.
12 13 14 |
# File 'lib/dwca/import.rb', line 12 def errors @errors end |
#field_index ⇒ Object
Returns the value of attribute field_index.
8 9 10 |
# File 'lib/dwca/import.rb', line 8 def field_index @field_index end |
#row_number ⇒ Object Also known as: i
Returns the value of attribute row_number.
10 11 12 |
# File 'lib/dwca/import.rb', line 10 def row_number @row_number end |
#tw_objects ⇒ Object
Returns the value of attribute tw_objects.
13 14 15 |
# File 'lib/dwca/import.rb', line 13 def tw_objects @tw_objects end |
Class Method Details
.build(data) ⇒ Object
78 79 80 81 82 |
# File 'lib/dwca/import.rb', line 78 def self.build(data) data.rows[0..10].each do |row| puts row end end |
Instance Method Details
#build_object(row, object) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dwca/import.rb', line 44 def build_object(row, object) klass = object.class.name.underscore.to_sym DWC2TW[klass].each_key do |attr| method = DWC2TW[klass][attr][:in] if method && object.respond_to?(method) object.send(method, cell(row, attr)) end end object end |
#build_row_objects(row, row_objects) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/dwca/import.rb', line 58 def build_row_objects(row, row_objects) result = row_objects.inject({}){|hsh, a| hsh.merge(a => nil)} # might need to be a hash row_objects.each do |r| result[r] = build_object(row, r.to_s.classify.safe_constantize.new) end result end |
#cell(row, attribute) ⇒ Object
37 38 39 |
# File 'lib/dwca/import.rb', line 37 def cell(row, attribute) row[@field_index[attribute]] end |
#referenced_models(core_fields) ⇒ Array (protected)
Given the core_fields what TW objects are possible
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dwca/import.rb', line 89 def referenced_models(core_fields) core_fields = core_fields.collect{|i| i[:term]} models = [] DWC2TW.each_key do |k| if (core_fields & DWC2TW[k].keys) != [] models.push k end end models.sort end |
#relate_row_objects(row, row_objects) ⇒ None
2nd pass
70 71 72 73 74 75 |
# File 'lib/dwca/import.rb', line 70 def relate_row_objects(row, row_objects) # specimen -> identifier, collecting_event, biocuration_classification, taxon_determination, type_specimen # otu -> taxon_name, biological_association # collecting_event -> georeference # taxon_name -> taxon_name end |
#row_id(row) ⇒ Integer
31 32 33 |
# File 'lib/dwca/import.rb', line 31 def row_id(row) row[0] end |