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



67
68
69
70
71
# File 'lib/leptris/xml/attr.rb', line 67

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

#inspectObject



73
74
75
# File 'lib/leptris/xml/attr.rb', line 73

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

#namespaceObject



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

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.



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

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
33
# File 'lib/leptris/xml/attr.rb', line 30

def prefix
  i = @name.index(":")
  i ? @name[0, i] : nil
end

#removeObject



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

def remove
  @element.remove_attribute(@name)
end

#to_sObject



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

def to_s; @value; end

#to_strObject



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

def to_str; @value; end

#to_xmlObject



63
64
65
# File 'lib/leptris/xml/attr.rb', line 63

def to_xml
  "#{@name}=\"#{@value.to_s.gsub(/[&<>"']/, ESCAPE_TABLE)}\""
end