Class: BerkeleyLibrary::Util::ODS::XML::ElementNode
- Inherits:
-
Object
- Object
- BerkeleyLibrary::Util::ODS::XML::ElementNode
- Includes:
- Logging
- Defined in:
- lib/berkeley_library/util/ods/xml/element_node.rb
Direct Known Subclasses
LOExt::TableProtection, Manifest::FileEntry, Manifest::Manifest, Office::AutomaticStyles, Office::Body, Office::DocumentContent, Office::DocumentStyles, Office::FontFaceDecls, Office::Scripts, Office::Spreadsheet, Office::Styles, Style::DefaultStyle, Style::FontFace, Style::ParagraphProperties, Style::Style, Style::TableCellProperties, Style::TableColumnProperties, Style::TableProperties, Style::TableRowProperties, Style::TextProperties, Table::NamedExpressions, Table::Repeatable, Table::Table, Text::LineBreak, Text::P, Text::S, Text::Tab
Instance Attribute Summary collapse
-
#doc ⇒ Nokogiri::XML::Document
readonly
The document containing this element.
-
#element_name ⇒ String
readonly
The name of this element.
-
#namespace ⇒ Namespace
readonly
The namespace for this element.
Instance Method Summary collapse
-
#add_child(child) ⇒ Object
rubocop:enable Style/OptionalArguments.
-
#attributes ⇒ Hash<String, String>
protected
The attributes, as a map from name to value.
-
#children ⇒ Array<ElementNode>
protected
TODO: replace this with :each_child and a protected default array.
-
#clear_attribute(namespace = prefix, name) ⇒ Object
rubocop:disable Style/OptionalArguments.
- #create_element ⇒ Object protected
- #element ⇒ Object
-
#ensure_element! ⇒ Object
Finalize this XML element and prepare for output.
-
#initialize(namespace, element_name, doc:) ⇒ ElementNode
constructor
A new instance of ElementNode.
- #prefix ⇒ Object
- #prefixed_attr_name(ns, name) ⇒ Object protected
-
#set_attribute(namespace = prefix, name, value) ⇒ Object
rubocop:disable Style/OptionalArguments.
Constructor Details
#initialize(namespace, element_name, doc:) ⇒ ElementNode
Returns a new instance of ElementNode.
24 25 26 27 28 |
# 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 |
Instance Attribute Details
#doc ⇒ Nokogiri::XML::Document (readonly)
Returns the document containing this element.
13 14 15 |
# File 'lib/berkeley_library/util/ods/xml/element_node.rb', line 13 def doc @doc end |
#element_name ⇒ String (readonly)
Returns the name of this element.
19 20 21 |
# File 'lib/berkeley_library/util/ods/xml/element_node.rb', line 19 def element_name @element_name end |
#namespace ⇒ Namespace (readonly)
Returns the namespace for this element.
16 17 18 |
# File 'lib/berkeley_library/util/ods/xml/element_node.rb', line 16 def namespace @namespace end |
Instance Method Details
#add_child(child) ⇒ Object
rubocop:enable Style/OptionalArguments
57 58 59 60 61 |
# 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 |
#attributes ⇒ Hash<String, String> (protected)
Returns the attributes, as a map from name to value.
83 84 85 86 |
# File 'lib/berkeley_library/util/ods/xml/element_node.rb', line 83 def attributes # noinspection RubyYardReturnMatch @attributes ||= {} end |
#children ⇒ Array<ElementNode> (protected)
TODO: replace this with :each_child and a protected default array
90 91 92 |
# File 'lib/berkeley_library/util/ods/xml/element_node.rb', line 90 def children @children ||= [] end |
#clear_attribute(namespace = prefix, name) ⇒ Object
rubocop:disable Style/OptionalArguments
51 52 53 54 |
# 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 |
#create_element ⇒ Object (protected)
71 72 73 74 75 76 77 78 79 80 |
# 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 |
#element ⇒ Object
34 35 36 |
# File 'lib/berkeley_library/util/ods/xml/element_node.rb', line 34 def element ensure_element! end |
#ensure_element! ⇒ Object
Finalize this XML element and prepare for output.
39 40 41 |
# File 'lib/berkeley_library/util/ods/xml/element_node.rb', line 39 def ensure_element! @element ||= create_element end |
#prefix ⇒ Object
30 31 32 |
# File 'lib/berkeley_library/util/ods/xml/element_node.rb', line 30 def prefix namespace.prefix end |
#prefixed_attr_name(ns, name) ⇒ Object (protected)
65 66 67 68 69 |
# 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 |
#set_attribute(namespace = prefix, name, value) ⇒ Object
rubocop:disable Style/OptionalArguments
44 45 46 47 |
# 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 |