class BerkeleyLibrary::Util::ODS::XML::ElementNode
Attributes
doc[R]
@return [Nokogiri::XML::Document] the document containing this element
element_name[R]
@return [String] the name of this element
namespace[R]
@return [Namespace] the namespace for this element
Public Class Methods
new(namespace, element_name, doc:)
click to toggle source
@param namespace [String, Symbol, Namespace] the element namespace @param element_name
[String] the element name @param doc [Nokogiri::XML::Document] the document containing this element
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 24 def initialize(namespace, element_name, doc:) @namespace = ensure_namespace(namespace) @element_name = element_name @doc = doc end
Public Instance Methods
add_child(child)
click to toggle source
rubocop:enable Style/OptionalArguments
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 57 def add_child(child) raise ArgumentError, "Not text or an element: #{child.inspect}" unless child.is_a?(ElementNode) || child.is_a?(String) child.tap { |c| children << c } end
clear_attribute(namespace = prefix, name)
click to toggle source
rubocop:disable Style/OptionalArguments
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 51 def clear_attribute(namespace = prefix, name) attr_name = prefixed_attr_name(namespace, name) attributes.delete(attr_name) end
element()
click to toggle source
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 34 def element ensure_element! end
ensure_element!()
click to toggle source
Finalize this XML
element and prepare for output.
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 39 def ensure_element! @element ||= create_element end
prefix()
click to toggle source
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 30 def prefix namespace.prefix end
set_attribute(namespace = prefix, name, value)
click to toggle source
rubocop:disable Style/OptionalArguments
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 44 def set_attribute(namespace = prefix, name, value) attr_name = prefixed_attr_name(namespace, name) attributes[attr_name] = value.to_s end
Protected Instance Methods
attributes()
click to toggle source
@return [Hash<String, String>] the attributes, as a map from name to value
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 83 def attributes # noinspection RubyYardReturnMatch @attributes ||= {} end
children()
click to toggle source
@return [Array<ElementNode>] the child elements TODO: replace this with :each_child and a protected default array
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 90 def children @children ||= [] end
create_element()
click to toggle source
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 71 def create_element doc.create_element("#{prefix}:#{element_name}", attributes).tap do |element| children.each do |child| next element.add_child(child.element) if child.is_a?(ElementNode) text_node = doc.create_text_node(child.to_s) element.add_child(text_node) end end end
prefixed_attr_name(ns, name)
click to toggle source
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 65 def prefixed_attr_name(ns, name) return "xmlns:#{name}" if ns.to_s == 'xmlns' "#{ensure_namespace(ns).prefix}:#{name}" end
Private Instance Methods
ensure_namespace(ns)
click to toggle source
# File lib/berkeley_library/util/ods/xml/element_node.rb, line 96 def ensure_namespace(ns) return ns if ns.is_a?(Namespace) raise ArgumentError, "Not a recognized namespace: #{ns.inspect}" unless (ns_for_prefix = Namespace.for_prefix(ns.to_s.downcase)) ns_for_prefix end