Class: Moxml::Attribute

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

Constant Summary

Constants inherited from Node

Node::TYPES

Instance Attribute Summary

Attributes inherited from Node

#context, #native, #parent_node

Instance Method Summary collapse

Methods inherited from Node

adapter, #add_child, #add_next_sibling, #add_previous_sibling, #after, #ancestors, #at_xpath, #before, #blank?, #children, #descendants, #document, #dup, #each, #each_node, #find, #find_all, #first_child, #following_siblings, #has_children?, #initialize, #invalidate_namespace_cache!, #last_child, #line_number, #materialize, #namespaces, #next_sibling, node_type_map, #outer_xml, #parent, #path, #preceding_siblings, #previous_sibling, #refresh_native!, #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_entity_reference_name, #validate_pi_target, #validate_prefix, #validate_uri

Constructor Details

This class inherits a constructor from Moxml::Node

Instance Method Details

#==(other) ⇒ Object



67
68
69
70
71
# File 'lib/moxml/attribute.rb', line 67

def ==(other)
  return false unless other.is_a?(Attribute)

  name == other.name && value == other.value && namespace == other.namespace
end

#attribute?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/moxml/attribute.rb', line 81

def attribute?
  true
end

#elementObject



54
55
56
57
# File 'lib/moxml/attribute.rb', line 54

def element
  native_elem = adapter.attribute_element(@native)
  native_elem && Moxml::Node.wrap(native_elem, context)
end

#identifierString

Returns the primary identifier for this attribute (its name)

Returns:

  • (String)

    the attribute name



18
19
20
# File 'lib/moxml/attribute.rb', line 18

def identifier
  name
end

#nameObject



5
6
7
# File 'lib/moxml/attribute.rb', line 5

def name
  adapter.attribute_name(@native)
end

#name=(new_name) ⇒ Object



9
10
11
12
13
14
# File 'lib/moxml/attribute.rb', line 9

def name=(new_name)
  # Mutation return contract (Adapter::Base): returns the native
  # to keep tracking — same object for in-place adapters, fresh
  # object for value-object adapters (leptris).
  @native = adapter.set_attribute_name(@native, new_name)
end

#namespaceObject



44
45
46
47
# File 'lib/moxml/attribute.rb', line 44

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

#namespace=(ns) ⇒ Object



49
50
51
52
# File 'lib/moxml/attribute.rb', line 49

def namespace=(ns)
  # See name= for the mutation return contract.
  @native = adapter.set_namespace(@native, ns&.native)
end

#raw_valueObject

Returns raw native value without entity marker restoration.



30
31
32
# File 'lib/moxml/attribute.rb', line 30

def raw_value
  @native.value
end

#removeObject



59
60
61
62
63
64
65
# File 'lib/moxml/attribute.rb', line 59

def remove
  adapter.remove_attribute_native(@native)
  if @parent_node.is_a?(Moxml::Element)
    @parent_node.invalidate_attribute_cache!
  end
  self
end

#textObject

XPath conversion compatibility - attributes need .text method that returns their value for XPath comparisons



40
41
42
# File 'lib/moxml/attribute.rb', line 40

def text
  value
end

#to_sObject



73
74
75
76
77
78
79
# File 'lib/moxml/attribute.rb', line 73

def to_s
  if namespace&.prefix
    "#{namespace.prefix}:#{name}=\"#{value}\""
  else
    "#{name}=\"#{value}\""
  end
end

#valueObject Also known as: content



22
23
24
25
# File 'lib/moxml/attribute.rb', line 22

def value
  val = @native.value.to_s
  adapter.restore_entities(val)
end

#value=(new_value) ⇒ Object



34
35
36
# File 'lib/moxml/attribute.rb', line 34

def value=(new_value)
  adapter.set_attribute_value(@native, new_value)
end