Class: DwCR::Metaschema::ContentFile

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/dwcr/metaschema/content_file.rb

Overview

This class represents location nodes (children of the files nodes) in DarwinCoreArchive, which represent csv files associated with a core or extension node

  • name: the basename of the file with extension (normally .csv)

  • path: the directory path where the file is located set this attribute to load content files from arbitrary directories

  • is_loaded: a flag that is set to true when the ContentFile instance’s contents have been loaded from the CSV file into the database

  • #entity: the Entity instance the ContentFile instance belongs to

Instance Method Summary collapse

Instance Method Details

#content_headersObject

Returns an array of symbols for column names for each column in the (headerless) CSV file specified in the file attribute the array is mapped from the ContentFile instance’s parent Entity instance’s Attribute instances that have an index The array is sorted by the index



33
34
35
36
37
38
39
# File 'lib/dwcr/metaschema/content_file.rb', line 33

def content_headers
  entity.attributes_dataset
        .exclude(index: nil)
        .exclude(type: nil)
        .order(:index)
        .map(&:column_name)
end

#file_nameObject

Returns the full file name including the path



24
25
26
# File 'lib/dwcr/metaschema/content_file.rb', line 24

def file_name
  File.join(path, name)
end

#loadObject

Inserts all rows of the CSV file belonging to the ContentFile instance into the table of the DwCA represented by the instance’s parent Entity instance Will raise an error for extension instances if the core instance is not loaded



46
47
48
49
50
51
52
53
54
# File 'lib/dwcr/metaschema/content_file.rb', line 46

def load
  return if is_loaded
  load_error = 'core needs to be loaded before extension files'
  raise load_error unless entity.is_core || entity.core.loaded?
  CSV.foreach(file_name) { |row| insert_row(row) }
  self.is_loaded = true
  save
  is_loaded
end

#unload!Object

Deletes all rows of the CSV file belonging to the ContentFile instance from the table of the DwCA represented by the instance’s parent Entity instance Warning: If this is called on a core instance, this will also destroy any dependant extension records!



61
62
63
64
65
66
# File 'lib/dwcr/metaschema/content_file.rb', line 61

def unload!
  return unless is_loaded
  CSV.foreach(file_name) { |row| delete_row(row) }
  self.is_loaded = false
  save
end