class DwCR::Metaschema::Entity

This class represents core or extension nodes in DarwinCoreArchive

Public Instance Methods

add_attribute_from(xml) click to toggle source

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
add_files_from(xml, path: nil) click to toggle source

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

Returns the last component of the term

# File lib/dwcr/metaschema/entity.rb, line 69
def baseterm
  term.split('/').last
end
class_name() click to toggle source

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

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

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

Returns a symbol for the name of the associated Attribute instance that is the key_column in the DarwinCoreArchive node the Entity represents

# File lib/dwcr/metaschema/entity.rb, line 101
def key
  attributes_dataset.first(index: key_column).name.to_sym
end
loaded?() click to toggle source

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

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

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

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
update_attributes!(*modifiers) click to toggle source

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

association_with(entity) click to toggle source

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

Sequel Model hook that creates a default name from the term

Calls superclass method
# 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