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

Parameters:

  • args (Hash)


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

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

Parameters:

  • data (CSV)


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

- (Object) build_object(row, object)

Parameters:

  • row (CSV::Row)
  • object (Object)

Returns:

  • (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

- (Object) build_row_objects(row, row_objects)

Parameters:

  • row (CSV::Row)
  • row_objects (Array)

Returns:

  • (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

- (Object) cell(row, attribute)

Parameters:

  • row (CSV::Row)
  • attribute (CSV::Row::Cell)


37
38
39
# File 'lib/dwca/import.rb', line 37

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

- (Array) referenced_models(core_fields) (protected)

Given the core_fields what TW objects are possible

Parameters:

  • core_fields (Array)

Returns:

  • (Array)


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

- (None) relate_row_objects(row, row_objects)

2nd pass

Parameters:

  • row (CSV::Row)
  • row_objects (Array)

Returns:

  • (None)


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

- (Integer) row_id(row)

Parameters:

  • row (CSV::Row)

Returns:

  • (Integer)


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

def row_id(row)
  row[0]
end