Module: DwCR::Metaschema::XMLParsable
Class Method Summary collapse
-
.load_meta(path = nil) ⇒ Object
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.
-
.validate_meta(xml) ⇒ Object
Validates the meta.xml file will raise errors if the file is not valid currently only covers validation against multiple core instances.
Instance Method Summary collapse
-
#default_from(xml) ⇒ Object
Parses the default value from an xml node applies to field nodes.
-
#files_from(xml) ⇒ Object
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).
-
#index_from(xml) ⇒ Object
Returns the index of a field node or the coreid of an extension node applies to field and extension nodes.
-
#is_core_from(xml) ⇒ Object
Returns
true
id the xml node represenst the corefalse
otherwise applies to child nodes of archive (entities). -
#key_column_from(xml) ⇒ Object
Returns the index of the key column in the core (id) or an extension (coreid) applies to child nodes of archive (entities).
-
#method(method_name) ⇒ Object
Returns the XMLParsable method that corresponds to the method name of the class the mixin is included to.
-
#term_from(xml) ⇒ Object
Returns the term for an entity or attribute applies to field nodes and child nodes of archive (entities).
-
#update_from(xml, *fields) ⇒ Object
Updates an instance of the model class the mixin is included in with values parsed from xml applies to field nodes.
-
#values_from(xml, *attrs) ⇒ Object
Returns a hash with model attributes as keys, values parsed from xml as values applies to field nodes.
Class Method Details
.load_meta(path = nil) ⇒ Object
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
31 32 33 34 35 36 |
# File 'lib/dwcr/metaschema/xml_parsable.rb', line 31 def self.(path = nil) path ||= Dir.pwd = File.directory?(path) ? File.join(path, 'meta.xml') : path xml = File.open() { |f| Nokogiri::XML(f) } XMLParsable. xml end |
.validate_meta(xml) ⇒ Object
Validates the meta.xml file will raise errors if the file is not valid currently only covers validation against multiple core instances
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/dwcr/metaschema/xml_parsable.rb', line 12 def self.(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 |
Instance Method Details
#default_from(xml) ⇒ Object
Parses the default value from an xml node applies to field nodes
40 41 42 |
# File 'lib/dwcr/metaschema/xml_parsable.rb', line 40 def default_from(xml) xml.attributes['default']&.value end |
#files_from(xml) ⇒ Object
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)
79 80 81 |
# File 'lib/dwcr/metaschema/xml_parsable.rb', line 79 def files_from(xml) xml.css('files').css('location').map(&:text) end |
#index_from(xml) ⇒ Object
Returns the index of a field node or the coreid of an extension node applies to field and extension nodes
61 62 63 64 65 |
# 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 |
#is_core_from(xml) ⇒ Object
Returns true
id the xml node represenst the core false
otherwise applies to child nodes of archive (entities)
47 48 49 50 51 52 53 54 55 56 |
# 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 |
#key_column_from(xml) ⇒ Object
Returns the index of the key column in the core (id) or an extension (coreid) applies to child nodes of archive (entities)
71 72 73 74 |
# 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 |
#method(method_name) ⇒ Object
Returns the XMLParsable method that corresponds to the method name of the class the mixin is included to
100 101 102 |
# File 'lib/dwcr/metaschema/xml_parsable.rb', line 100 def method(method_name) method_name.to_s + '_from' end |
#term_from(xml) ⇒ Object
Returns the term for an entity or attribute applies to field nodes and child nodes of archive (entities)
85 86 87 88 |
# File 'lib/dwcr/metaschema/xml_parsable.rb', line 85 def term_from(xml) term = xml.attributes['rowType'] || xml.attributes['term'] term&.value end |
#update_from(xml, *fields) ⇒ Object
Updates an instance of the model class the mixin is included in with values parsed from xml applies to field nodes
93 94 95 96 |
# File 'lib/dwcr/metaschema/xml_parsable.rb', line 93 def update_from(xml, *fields) update values_from(xml, *fields) save end |
#values_from(xml, *attrs) ⇒ Object
Returns a hash with model attributes as keys, values parsed from xml as values applies to field nodes
107 108 109 110 |
# 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 |