class DwCR::Metaschema::Entity
This class represents core or extension nodes in DarwinCoreArchive
-
name
: the name for the node default: pluralized term without namespace in underscore (snake case) form -
term
: the full term (a uri), including namespace for the node see rs.tdwg.org/dwc/terms/index.htm -
is_core
: true if the node is the core node of the DarwinCoreArchive, false otherwise -
key_column
: the column in the node representing the primary key of the core node or the foreign key in am extension node -
fields_enclosed_by
: directive to formCSV
in :content_files default: '"' -
fields_terminated_by
: fieldsTerminatedBy=“,” -
lines_terminated_by
: linesTerminatedBy=“rn” -
#archive: the
Archive
instance theEntity
instance belongs to -
#content_files:
ContentFile
instances associated with theEntity
-
#core: if the
Entity
instance is an extension returns theEntity
instance representing the core node nil otherwise -
#extensions: if the
Entity
instance is the core node returns anyEntity
instances representing extension nodes
Public Instance Methods
Creates a Attribute
instance from an xml node (field) given that the instance has not been previously defined if an instance has been previously defined, it will be updated
# File lib/dwcr/metaschema/entity.rb, line 157 def add_attribute_from(xml) attribute = attributes_dataset.first(term: term_from(xml)) attribute ||= add_attribute(values_from(xml, :term, :index, :default)) attribute.update_from(xml, :index, :default) end
Creates a ContentFile
instance from an xml node (file) a path
can be given to add files from arbitrary directories name
is parsed from the location node
# File lib/dwcr/metaschema/entity.rb, line 166 def add_files_from(xml, path: nil) files_from(xml).each { |file| add_content_file(name: file, path: path) } end
Returns the last component of the term
# File lib/dwcr/metaschema/entity.rb, line 69 def baseterm term.split('/').last end
Returns a string with Entity
instance's singularized name in camelcase this is the name of the Sequel::Model in the DarwinCoreArchive schema
# File lib/dwcr/metaschema/entity.rb, line 75 def class_name name.classify end
Returns an array of full filenames with path for all associated ContentFile
instances
# File lib/dwcr/metaschema/entity.rb, line 81 def files content_files.map(&:file_name) end
Returns a symbol based on the Entity
instance's foreign key name
# File lib/dwcr/metaschema/entity.rb, line 94 def foreign_key class_name.foreign_key.to_sym end
Returns true if all content_files have been loaded, false otherwise
# File lib/dwcr/metaschema/entity.rb, line 87 def loaded? loaded_files = content_files_dataset.where(is_loaded: true) return true if loaded_files.count == content_files.size loaded_files.empty? ? false : loaded_files.map(&:file_name) end
Returns an array of parameters for all associations of the Sequel::Model in the DarwinCoreArchive schema that the Entity
represents each set of parameters is an array [association_type, association_name, options]
that can be splattet as arguments into Sequel::Model::Associations::ClassMethods#associate
# File lib/dwcr/metaschema/entity.rb, line 111 def model_associations # add the assoc to Entity here meta_assoc = [:many_to_one, :entity, { class: Entity }] if is_core a = extensions.map { |extension| association_with(extension) } a.unshift meta_assoc else [meta_assoc, association_with(core)] end end
Returns the constant with module name for the Entity
instance this is the constant of the Sequel::Model in the DarwinCoreArchive schema
# File lib/dwcr/metaschema/entity.rb, line 125 def model_get modelname = 'DwCR::' + class_name modelname.constantize end
Returns a symbol for the pluralzid name
that is the name of the table in the DarwinCoreArchive schema
# File lib/dwcr/metaschema/entity.rb, line 132 def table_name name.tableize.to_sym end
Analyzes the Entity
instance's content_files for any parameters given as modifiers and updates the asssociated attributes with the new values
-
:length or _'length'_ will update the
Attribute
instances'max_content_length
-
:type or _'type'_ will update the
Attribute
instances'type
attributes
# File lib/dwcr/metaschema/entity.rb, line 143 def update_attributes!(*modifiers) DwCAContentAnalyzer::FileSet.new(files, modifiers).columns.each do |cp| column = attributes_dataset.first(index: cp[:index]) modifiers.each { |m| column.send(m.to_s + '=', cp[m]) } column.save end end
Private Instance Methods
Returns an array that can be splattet as arguments into the Sequel::Model::Associations::ClassMethods#associate method: [association_type, association_name, options]
# File lib/dwcr/metaschema/entity.rb, line 183 def association_with(entity) options = { class: entity.class_name, class_namespace: 'DwCR' } if is_core options[:key] = foreign_key [:one_to_many, entity.table_name, options] else options[:key] = entity.foreign_key [:many_to_one, entity.name.singularize.to_sym, options] end end
Sequel Model hook that creates a default name
from the term
# File lib/dwcr/metaschema/entity.rb, line 173 def before_create e = 'Entity instances need to belong to a Archive' raise ArgumentError, e unless archive self.name ||= term&.split('/')&.last&.underscore super end