module DwCR::Metaschema::XMLParsable
Public Class Methods
Loads the meta.xml file from path if path is a directory, will try to locate the meta.xml file in path wil default to working directory if no path is given
# File lib/dwcr/metaschema/xml_parsable.rb, line 31 def self.load_meta(path = nil) path ||= Dir.pwd meta = File.directory?(path) ? File.join(path, 'meta.xml') : path xml = File.open(meta) { |f| Nokogiri::XML(f) } XMLParsable.validate_meta xml end
Validates the meta.xml file will raise errors if the file is not valid currently only covers validation against multiple core instances
# File lib/dwcr/metaschema/xml_parsable.rb, line 12 def self.validate_meta(xml) raise ArgumentError, 'Root is not archive' unless xml.root.name == 'archive' xml_elements = xml.root.elements xml_core = xml_elements.css 'core' raise ArgumentError, 'Missing core node' if xml_core.empty? raise ArgumentError, 'Multiple core nodes' if xml_core.count > 1 xml_elements -= xml_core xml_xtns = xml_elements.css 'extension' xml_elements -= xml_xtns raise ArgumentError, 'Invalid node' unless xml_elements.empty? xml end
Public Instance Methods
Parses the default value from an xml node applies to field nodes
# File lib/dwcr/metaschema/xml_parsable.rb, line 40 def default_from(xml) xml.attributes['default']&.value end
Returns an array with the names for any files associated with a child node of archive (core or extension) applies to child nodes of archive (entities)
# File lib/dwcr/metaschema/xml_parsable.rb, line 79 def files_from(xml) xml.css('files').css('location').map(&:text) end
Returns the index of a field node or the coreid of an extension node applies to field and extension nodes
# File lib/dwcr/metaschema/xml_parsable.rb, line 61 def index_from(xml) key_index = xml.css('coreid')&.first return xml.attributes['index']&.value&.to_i unless key_index key_index.attributes['index'].value.to_i end
Returns true
id the xml node represenst the core false
otherwise applies to child nodes of archive (entities)
# File lib/dwcr/metaschema/xml_parsable.rb, line 47 def is_core_from(xml) case xml.name when 'core' true when 'extension' false else raise ArgumentError, "invalid node name: '#{xml.name}'" end end
Returns the index of the key column in the core (id) or an extension (coreid) applies to child nodes of archive (entities)
# File lib/dwcr/metaschema/xml_parsable.rb, line 71 def key_column_from(xml) key_tag = is_core_from(xml) ? 'id' : 'coreid' xml.css(key_tag).first.attributes['index'].value.to_i end
Returns the XMLParsable
method that corresponds to the method name of the class the mixin is included to
# File lib/dwcr/metaschema/xml_parsable.rb, line 100 def method(method_name) method_name.to_s + '_from' end
Returns the term for an entity or attribute applies to field nodes and child nodes of archive (entities)
# File lib/dwcr/metaschema/xml_parsable.rb, line 85 def term_from(xml) term = xml.attributes['rowType'] || xml.attributes['term'] term&.value end
Updates an instance of the model class the mixin is included in with values parsed from xml applies to field nodes
# File lib/dwcr/metaschema/xml_parsable.rb, line 93 def update_from(xml, *fields) update values_from(xml, *fields) save end
Returns a hash with model attributes as keys, values parsed from xml as values applies to field nodes
# File lib/dwcr/metaschema/xml_parsable.rb, line 107 def values_from(xml, *attrs) values = attrs.map { |attr| send(method(attr), xml) } attrs.zip(values).to_h.compact end