Module: DwCR
- Defined in:
- lib/dwcr/schema.rb,
lib/dwcr/dynamic_models.rb,
lib/dwcr/metaschema/entity.rb,
lib/dwcr/metaschema/archive.rb,
lib/dwcr/metaschema/attribute.rb,
lib/dwcr/metaschema/metaschema.rb,
lib/dwcr/metaschema/content_file.rb,
lib/dwcr/metaschema/xml_parsable.rb
Overview
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
Defined Under Namespace
Modules: Metaschema
Constant Summary collapse
- MODELS =
DwCR.load_models archive
Class Method Summary collapse
-
.create_model(entity) ⇒ Object
Creates a Sequel::Model class for a Entity instance adds all associations given for the Entity instance.
-
.create_schema(archive, **options) ⇒ Object
Creates the database schema for the DwCA nodes options: -
type:trueorfalse-length:trueorfalseif options are given, the schema will be updated based on the DwCA files actual content, analysing each column for type and length. -
.create_schema_table(entity) ⇒ Object
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 anyattributesassociated withentity. -
.load_contents_for(archive) ⇒ Object
Loads the contents of all CSV files associated with an archive into the shema tables.
-
.load_models(archive = Metaschema::Archive.first) ⇒ Object
Loads models for all Entity instances in the Archive instance if no explicit Archive instance is given, it will load the first.
Class Method Details
.create_model(entity) ⇒ Object
Creates a Sequel::Model class for a Entity instance adds all associations given for the Entity instance
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 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 |
.create_schema(archive, **options) ⇒ Object
Creates the database schema for the DwCA nodes options:
-
type:trueorfalse -
length:trueorfalse
if options are given, the schema will be updated based on the DwCA files actual content, analysing each column for type and length
33 34 35 36 |
# File 'lib/dwcr/schema.rb', line 33 def self.create_schema(archive, **) Metaschema.update(archive, ) archive.entities.each { |entity| DwCR.create_schema_table(entity) } end |
.create_schema_table(entity) ⇒ Object
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
15 16 17 18 19 20 21 22 23 24 |
# 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 |
.load_contents_for(archive) ⇒ Object
Loads the contents of all CSV files associated with an archive into the shema tables
52 53 54 55 56 57 |
# 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 |
.load_models(archive = Metaschema::Archive.first) ⇒ Object
Loads models for all Entity instances in the Archive instance if no explicit Archive instance is given, it will load the first
40 41 42 43 44 45 46 47 48 |
# 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 |