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

- (Manager) initialize(opts = {})

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

- (Object) available_objects

Returns the value of attribute available_objects



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

def available_objects
  @available_objects
end

- (Object) data

Returns the value of attribute data



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

def data
  @data
end

- (Object) errors

Returns the value of attribute errors



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

def errors
  @errors
end

- (Object) field_index

Returns the value of attribute field_index



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

def field_index
  @field_index
end

- (Object) row_number 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

- (Object) tw_objects

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

+ (Object) build(data)



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

- (Object) build_object(row, 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 method && object.respond_to?(method)
      object.send(method, cell(row, attr))
    end
  end
  object
end

- (Object) build_row_objects(row, row_objects)



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

- (Object) cell(row, attribute)



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

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

- (Object) referenced_models(core_fields) (protected)

Given the core_fields what TW objects are possible



72
73
74
75
76
77
78
79
80
81
# File 'lib/dwca/import.rb', line 72

def referenced_models(core_fields)
  core_fields = core_fields.collect{|i| i[:term]}
  models = []
  DWC2TW.keys.each do |k|
    if (core_fields & DWC2TW[k].keys) != []
      models.push k 
    end
  end
  models.sort
end

- (Object) relate_row_objects(row, row_objects)

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

- (Object) row_id(row)



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

def row_id(row)
  row[0] 
end