module DwCR
This module provides functionality to create a SQLite database from a DarwinCoreArchive and provides an ORM layer using sequel.jeremyevans.net Sequel::Model instances are created from the DwCA's meta.xml file
Public Class Methods
Creates a Sequel::Model class for a Entity instance adds all associations given for the Entity instance
# File lib/dwcr/dynamic_models.rb, line 7 def self.create_model(entity) model_class = Class.new(Sequel::Model(entity.table_name)) do include DynamicModelQueryable @entity = entity entity.model_associations.each do |association| associate(*association) next if association[0] == :many_to_one plugin :association_dependencies add_association_dependencies(association[1] => :destroy) end define_singleton_method(:finalize) do @entity = nil instance_methods(false).each { |method| remove_method(method) } Module.nesting.last.send(:remove_const, entity.class_name) end end const_set entity.class_name, model_class model_class end
Creates the database schema for the DwCA nodes options:
-
type:
true
orfalse
-
length:
true
orfalse
if options are given, the schema will be updated based on the DwCA files actual content, analysing each column for type and length
# File lib/dwcr/schema.rb, line 33 def self.create_schema(archive, **options) Metaschema.update(archive, options) archive.entities.each { |entity| DwCR.create_schema_table(entity) } end
Creates the table for entity
(a Entity instanc) inserts foreign key for entities skips the coreid field declared in extensions in the DwCA meta.xml (this field is redundant, because relationships are re-established upon import using SQL primary and foreign keys) inserts the proper SQL foreign key into extensions adds columns for any attributes
associated with entity
# File lib/dwcr/schema.rb, line 15 def self.create_schema_table(entity) DB.create_table? entity.table_name do primary_key :id foreign_key :entity_id, :entities foreign_key entity.core.foreign_key, entity.core.table_name if entity.core entity.attributes.each do |a| column(*a.to_table_column) unless a.foreign_key? end end end
Loads the contents of all CSV
files associated with an archive into the shema tables
# File lib/dwcr/schema.rb, line 52 def self.load_contents_for(archive) archive.core.content_files.each(&:load) archive.extensions.each do |extension| extension.content_files.each(&:load) end end
Loads models for all Entity instances in the Archive instance if no explicit Archive instance is given, it will load the first
# File lib/dwcr/schema.rb, line 40 def self.load_models(archive = Metaschema::Archive.first) archive.entities.map do |entity| entity_model = DwCR.create_model(entity) Metaschema::Entity.associate(:one_to_many, entity.table_name, class: entity_model) entity_model end end