Class: DwCR::Metaschema::Entity

Inherits:
Sequel::Model
  • Object
show all
Includes:
XMLParsable
Defined in:
lib/dwcr/metaschema/entity.rb

Overview

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 form CSV in :content_files default: ‘"’

  • fields_terminated_by: fieldsTerminatedBy=“,”

  • lines_terminated_by: linesTerminatedBy=“rn”

  • #archive: the Archive instance the Entity instance belongs to

  • #attributes: Attribute instances associated with the Entity

  • #content_files: ContentFile instances associated with the Entity

  • #core: if the Entity instance is an extension returns the Entity instance representing the core node nil otherwise

  • #extensions: if the Entity instance is the core node returns any Entity instances representing extension nodes

Instance Method Summary collapse

Methods included from XMLParsable

#default_from, #files_from, #index_from, #is_core_from, #key_column_from, load_meta, #method, #term_from, #update_from, validate_meta, #values_from

Instance Method Details

#add_attribute_from(xml) ⇒ Object

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



157
158
159
160
161
# 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) ⇒ Object

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



166
167
168
# 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

#basetermObject

Returns the last component of the term



69
70
71
# File 'lib/dwcr/metaschema/entity.rb', line 69

def baseterm
  term.split('/').last
end

#class_nameObject

Returns a string with Entity instance’s singularized name in camelcase this is the name of the Sequel::Model in the DarwinCoreArchive schema



75
76
77
# File 'lib/dwcr/metaschema/entity.rb', line 75

def class_name
  name.classify
end

#filesObject

Returns an array of full filenames with path for all associated ContentFile instances



81
82
83
# File 'lib/dwcr/metaschema/entity.rb', line 81

def files
  content_files.map(&:file_name)
end

#foreign_keyObject

Returns a symbol based on the Entity instance’s foreign key name



94
95
96
# File 'lib/dwcr/metaschema/entity.rb', line 94

def foreign_key
  class_name.foreign_key.to_sym
end

#keyObject

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



101
102
103
# File 'lib/dwcr/metaschema/entity.rb', line 101

def key
  attributes_dataset.first(index: key_column).name.to_sym
end

#loaded?Boolean

Returns true if all content_files have been loaded, false otherwise

Returns:

  • (Boolean)


87
88
89
90
91
# 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_associationsObject

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



111
112
113
114
115
116
117
118
119
120
# 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_getObject

Returns the constant with module name for the Entity instance this is the constant of the Sequel::Model in the DarwinCoreArchive schema



125
126
127
128
# File 'lib/dwcr/metaschema/entity.rb', line 125

def model_get
  modelname = 'DwCR::' + class_name
  modelname.constantize
end

#table_nameObject

Returns a symbol for the pluralzid name that is the name of the table in the DarwinCoreArchive schema



132
133
134
# File 'lib/dwcr/metaschema/entity.rb', line 132

def table_name
  name.tableize.to_sym
end

#update_attributes!(*modifiers) ⇒ Object

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



143
144
145
146
147
148
149
# 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