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
-
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 totrue
when theContentFile
instance's contents have been loaded from theCSV
file into the database -
#entity: the
Entity
instance theContentFile
instance belongs to
Public Instance Methods
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
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
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
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
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
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
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
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