Class: BerkeleyLibrary::Util::ODS::XML::ElementNode

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/berkeley_library/util/ods/xml/element_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, element_name, doc:) ⇒ ElementNode

Returns a new instance of ElementNode.

Parameters:

  • namespace (String, Symbol, Namespace)

    the element namespace

  • element_name (String)

    the element name

  • doc (Nokogiri::XML::Document)

    the document containing this element



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

#docNokogiri::XML::Document (readonly)

Returns the document containing this element.

Returns:

  • (Nokogiri::XML::Document)

    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_nameString (readonly)

Returns the name of this element.

Returns:

  • (String)

    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

#namespaceNamespace (readonly)

Returns the namespace for this element.

Returns:

  • (Namespace)

    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

Raises:

  • (ArgumentError)


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

#attributesHash<String, String> (protected)

Returns the attributes, as a map from name to value.

Returns:

  • (Hash<String, String>)

    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

#childrenArray<ElementNode> (protected)

TODO: replace this with :each_child and a protected default array

Returns:



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_elementObject (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

#elementObject



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

#prefixObject



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