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. Mutation goes through the parent element via #[].

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, element) ⇒ Attr

Returns a new instance of Attr.



10
11
12
13
14
# File 'lib/leptris/xml/attr.rb', line 10

def initialize(name, value, element)
  @name = name
  @value = value
  @element = element
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



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

def element
  @element
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



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

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

#inspectObject



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

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

#namespaceObject



21
22
23
# File 'lib/leptris/xml/attr.rb', line 21

def namespace
  nil  # upstream gap: per-attribute namespace not exposed
end

#removeObject



25
26
27
# File 'lib/leptris/xml/attr.rb', line 25

def remove
  @element.remove_attribute(@name)
end

#to_sObject



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

def to_s; @value; end

#to_strObject



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

def to_str; @value; end

#to_xmlObject

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



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

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