class DwCR::Metaschema::ContentFile

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

Public Instance Methods

content_headers() click to toggle source

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

# 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_name() click to toggle source

Returns the full file name including the path

# File lib/dwcr/metaschema/content_file.rb, line 24
def file_name
  File.join(path, name)
end
load() click to toggle source

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

# 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!() click to toggle source

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!

# 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

Private Instance Methods

compact(row) click to toggle source

removes all cells from a row for which the column in all associated CSV files is empty (the associated Attribute instance has type == nil)

# File lib/dwcr/metaschema/content_file.rb, line 73
def compact(row)
  empty_cols = entity.attributes_dataset
                     .exclude(index: nil)
                     .where(type: nil)
                     .map(&:index)
  row.delete_if.with_index { |_, index| empty_cols.include?(index) }
end
core_row(foreign_key) click to toggle source

Returns the row from the DwCA core that matches the foreign key

# File lib/dwcr/metaschema/content_file.rb, line 110
def core_row(foreign_key)
  entity.core.model_get.first(entity.core.key => foreign_key)
end
delete_row(row) click to toggle source

Delets a CSV row from the DwCA table represented by the instance's parent Entity instance

# File lib/dwcr/metaschema/content_file.rb, line 83
def delete_row(row)
  row_vals = row_to_hash row
  row_vals.delete(entity.key) unless entity.is_core
  rec = entity.model_get.first(row_vals)
  rec.destroy
end
insert_into_core(row) click to toggle source

Inserts a CSV row into the DwCA schema's core table

# File lib/dwcr/metaschema/content_file.rb, line 91
def insert_into_core(row)
  return unless entity.is_core
  entity.model_get.create(row_to_hash(row))
end
insert_into_extension(row) click to toggle source

Inserts a CSV row into an extension table of the DwCA schema

# File lib/dwcr/metaschema/content_file.rb, line 97
def insert_into_extension(row)
  row_vals = row_to_hash row
  core_row(row_vals.delete(entity.key)).send(add_related, row_vals)
end
insert_row(row) click to toggle source

Inserts a CSV row into the DwCA table represented by the instance's parent Entity instance

# File lib/dwcr/metaschema/content_file.rb, line 104
def insert_row(row)
  rec = insert_into_core(row) || insert_into_extension(row)
  entity.send(add_related, rec)
end
row_to_hash(row) click to toggle source

Creates a hash from a headerless CSV row Entity instance's :attributes colum names are keys the CSV row cells are values

# File lib/dwcr/metaschema/content_file.rb, line 123
def row_to_hash(row)
  keys = content_headers
  vals = compact row
  keys.zip(vals).to_h.select { |_k, v| v && !v.empty? }
end