Class: Dwca::Import::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/dwca/import.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Manager

Returns a new instance of Manager



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dwca/import.rb', line 15

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_objectsObject

Returns the value of attribute available_objects



9
10
11
# File 'lib/dwca/import.rb', line 9

def available_objects
  @available_objects
end

#dataObject

Returns the value of attribute data



12
13
14
# File 'lib/dwca/import.rb', line 12

def data
  @data
end

#errorsObject

Returns the value of attribute errors



12
13
14
# File 'lib/dwca/import.rb', line 12

def errors
  @errors
end

#field_indexObject

Returns the value of attribute field_index



8
9
10
# File 'lib/dwca/import.rb', line 8

def field_index
  @field_index
end

#row_numberObject 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_objectsObject

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



63
64
65
66
67
# File 'lib/dwca/import.rb', line 63

def self.build(data)
  data.rows[0..10].each do |row|
    puts row
  end
end

Instance Method Details

#build_object(row, object) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/dwca/import.rb', line 36

def build_object(row, object)
  klass = object.class.name.underscore.to_sym
  DWC2TW[klass].keys.each do |attr|
    method = DWC2TW[klass][attr][:in]
    if object.respond_to?(method)
      object.send(method, cell(row, attr))
    end
  end
  object
end

#build_row_objects(row, row_objects) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/dwca/import.rb', line 47

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



32
33
34
# File 'lib/dwca/import.rb', line 32

def cell(row, attribute)
  row[@field_index[attribute]]
end

#relate_row_objects(row, row_objects) ⇒ Object

2nd pass



56
57
58
59
60
61
# File 'lib/dwca/import.rb', line 56

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) ⇒ Object



28
29
30
# File 'lib/dwca/import.rb', line 28

def row_id(row)
  row[0] 
end