Class: Leptris::XML::Attr

Inherits:
Object
  • Object
show all
Defined in:
lib/leptris/xml/attr.rb

Overview

Lightweight attribute wrapper: a (name, value, parent_element) triple materialized from the attribute-iteration face, carrying the C attribute handle for the per-attribute namespace accessors. Mutation goes through the parent element via #[].

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, element, c_handle: nil) ⇒ Attr

c_handle is the C LeptrisAttribute pointer from the owning element's iteration face — document-owned and borrowed. Absent only when an Attr is constructed by hand.



14
15
16
17
18
19
# File 'lib/leptris/xml/attr.rb', line 14

def initialize(name, value, element, c_handle: nil)
  @name = name
  @value = value
  @element = element
  @c_handle = c_handle
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



9
10
11
# File 'lib/leptris/xml/attr.rb', line 9

def element
  @element
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/leptris/xml/attr.rb', line 9

def name
  @name
end

#valueObject

Returns the value of attribute value.



9
10
11
# File 'lib/leptris/xml/attr.rb', line 9

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



65
66
67
68
69
# File 'lib/leptris/xml/attr.rb', line 65

def ==(other)
  other.is_a?(Leptris::XML::Attr) &&
    @name == other.name && @value == other.value &&
    @element == other.element
end

#inspectObject



71
72
73
# File 'lib/leptris/xml/attr.rb', line 71

def inspect
  "#<#{self.class.name} name=#{@name.inspect} value=#{@value.inspect}>"
end

#namespaceObject



43
44
45
# File 'lib/leptris/xml/attr.rb', line 43

def namespace
  namespace_uri
end

#namespace_uriObject

The attribute's namespace URI, resolved through the OWNING element's in-scope declarations at read time (libleptris 1.8.0). nil for a no-namespace attribute or an undeclared prefix; xml is prebound to http://www.w3.org/XML/1998/namespace.



38
39
40
41
# File 'lib/leptris/xml/attr.rb', line 38

def namespace_uri
  return nil if @c_handle.nil?
  Leptris::XML::FFI.leptris_attribute_namespace_uri(@c_handle)
end

#prefixObject

The prefix as written in the qualified name (bytes before the colon), nil for a no-namespace attribute. Name-derived and immutable — the same semantics as leptris_attribute_prefix, so no declaration lookup is involved.



30
31
32
# File 'lib/leptris/xml/attr.rb', line 30

def prefix
  @name.include?(":") ? @name.split(":", 2).first : nil
end

#removeObject



47
48
49
# File 'lib/leptris/xml/attr.rb', line 47

def remove
  @element.remove_attribute(@name)
end

#to_sObject



51
# File 'lib/leptris/xml/attr.rb', line 51

def to_s; @value; end

#to_strObject



52
# File 'lib/leptris/xml/attr.rb', line 52

def to_str; @value; end

#to_xmlObject

Serialized attribute form: name="value" with the five XML special characters escaped in the value.



56
57
58
59
60
61
62
63
# File 'lib/leptris/xml/attr.rb', line 56

def to_xml
  escaped = @value.to_s.gsub("&", "&amp;")
                     .gsub("<", "&lt;")
                     .gsub(">", "&gt;")
                     .gsub('"', "&quot;")
                     .gsub("'", "&apos;")
  "#{@name}=\"#{escaped}\""
end