Class: Moxml::Element

Inherits:
Node
  • Object
show all
Defined in:
lib/moxml/element.rb

Constant Summary

Constants inherited from Node

Node::TYPES

Instance Attribute Summary

Attributes inherited from Node

#context, #native

Instance Method Summary collapse

Methods inherited from Node

#==, #add_child, #add_next_sibling, #add_previous_sibling, #at_xpath, #children, #document, #initialize, #next_sibling, #parent, #previous_sibling, #remove, #replace, #to_xml, wrap, #xpath

Methods included from XmlUtils

#encode_entities, #normalize_xml_value, #validate_comment_content, #validate_declaration_encoding, #validate_declaration_standalone, #validate_declaration_version, #validate_element_name, #validate_pi_target, #validate_prefix, #validate_uri

Constructor Details

This class inherits a constructor from Moxml::Node

Instance Method Details

#[](name) ⇒ Object



20
21
22
# File 'lib/moxml/element.rb', line 20

def [](name)
  adapter.get_attribute_value(@native, name)
end

#[]=(name, value) ⇒ Object



16
17
18
# File 'lib/moxml/element.rb', line 16

def []=(name, value)
  adapter.set_attribute(@native, name, normalize_xml_value(value))
end

#add_namespace(prefix, uri) ⇒ Object Also known as: add_namespace_definition



40
41
42
43
44
45
46
# File 'lib/moxml/element.rb', line 40

def add_namespace(prefix, uri)
  validate_uri(uri)
  adapter.create_native_namespace(@native, prefix, uri)
  self
rescue ValidationError => e
  raise Moxml::NamespaceError, e.message
end

#attribute(name) ⇒ Object



24
25
26
27
# File 'lib/moxml/element.rb', line 24

def attribute(name)
  native_attr = adapter.get_attribute(@native, name)
  native_attr && Attribute.new(native_attr, context)
end

#attributesObject



29
30
31
32
33
# File 'lib/moxml/element.rb', line 29

def attributes
  adapter.attributes(@native).map do |attr|
    Attribute.new(attr, context)
  end
end

#inner_textObject



83
84
85
# File 'lib/moxml/element.rb', line 83

def inner_text
  adapter.inner_text(@native)
end

#inner_xmlObject



87
88
89
# File 'lib/moxml/element.rb', line 87

def inner_xml
  adapter.inner_xml(@native)
end

#inner_xml=(xml) ⇒ Object



91
92
93
94
# File 'lib/moxml/element.rb', line 91

def inner_xml=(xml)
  doc = context.parse("<root>#{xml}</root>")
  adapter.replace_children(@native, doc.root.children.map(&:native))
end

#nameObject



8
9
10
# File 'lib/moxml/element.rb', line 8

def name
  adapter.node_name(@native)
end

#name=(value) ⇒ Object



12
13
14
# File 'lib/moxml/element.rb', line 12

def name=(value)
  adapter.set_node_name(@native, value)
end

#namespaceObject

it’s NOT the same as namespaces.first



50
51
52
53
# File 'lib/moxml/element.rb', line 50

def namespace
  ns = adapter.namespace(@native)
  ns && Namespace.new(ns, context)
end

#namespace=(ns_or_hash) ⇒ Object

add the prefix to the element name and add the namespace to the list of namespace definitions



57
58
59
60
61
62
63
64
65
66
# File 'lib/moxml/element.rb', line 57

def namespace=(ns_or_hash)
  if ns_or_hash.is_a?(Hash)
    adapter.set_namespace(
      @native,
      adapter.create_namespace(@native, *ns_or_hash.to_a.first)
    )
  else
    adapter.set_namespace(@native, ns_or_hash&.native)
  end
end

#namespacesObject Also known as: namespace_definitions



68
69
70
71
72
# File 'lib/moxml/element.rb', line 68

def namespaces
  adapter.namespace_definitions(@native).map do |ns|
    Namespace.new(ns, context)
  end
end

#remove_attribute(name) ⇒ Object



35
36
37
38
# File 'lib/moxml/element.rb', line 35

def remove_attribute(name)
  adapter.remove_attribute(@native, name)
  self
end

#textObject



75
76
77
# File 'lib/moxml/element.rb', line 75

def text
  adapter.text_content(@native)
end

#text=(content) ⇒ Object



79
80
81
# File 'lib/moxml/element.rb', line 79

def text=(content)
  adapter.set_text_content(@native, normalize_xml_value(content))
end

#with_attribute(name, value) ⇒ Object

Fluent interface methods



97
98
99
100
# File 'lib/moxml/element.rb', line 97

def with_attribute(name, value)
  self[name] = value
  self
end

#with_namespace(prefix, uri) ⇒ Object



102
103
104
105
# File 'lib/moxml/element.rb', line 102

def with_namespace(prefix, uri)
  add_namespace(prefix, uri)
  self
end

#with_text(content) ⇒ Object



107
108
109
110
# File 'lib/moxml/element.rb', line 107

def with_text(content)
  self.text = content
  self
end