Class: Oga::XML::Attribute

Inherits:
Object
  • Object
show all
Includes:
ExpandedName, ToXML
Defined in:
lib/oga/xml/attribute.rb

Overview

Class for storing information about a single XML attribute.

Constant Summary

DEFAULT_NAMESPACE =

The default namespace available to all attributes. This namespace can not be modified.

Returns:

Namespace.new(
  :name => 'xml',
  :uri  => XML::DEFAULT_NAMESPACE.uri
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ToXML

#to_xml

Methods included from ExpandedName

#expanded_name

Constructor Details

#initialize(options = {}) ⇒ Attribute

Returns a new instance of Attribute

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :name (String)
  • :namespace_name (String)
  • :value (String)
  • :element (Oga::XML::Element)


36
37
38
39
40
41
42
43
# File 'lib/oga/xml/attribute.rb', line 36

def initialize(options = {})
  @name           = options[:name]
  @value          = options[:value]
  @element        = options[:element]
  @decoded        = false
  @namespace      = nil
  @namespace_name = options[:namespace_name]
end

Instance Attribute Details

#elementOga::XML::Element Also known as: parent

The element this attribute belongs to.

Returns:



17
18
19
# File 'lib/oga/xml/attribute.rb', line 17

def element
  @element
end

#nameString

The name of the attribute.

Returns:

  • (String)


10
11
12
# File 'lib/oga/xml/attribute.rb', line 10

def name
  @name
end

#namespace_nameString

Returns:

  • (String)


13
14
15
# File 'lib/oga/xml/attribute.rb', line 13

def namespace_name
  @namespace_name
end

Instance Method Details

#each_ancestor {|element| ... } ⇒ Object

Yields:

See Also:

  • Oga::XML::Attribute.[Oga[Oga::XML[Oga::XML::Node[Oga::XML::Node#each_ancestor]


102
103
104
105
106
107
108
109
110
# File 'lib/oga/xml/attribute.rb', line 102

def each_ancestor
  return to_enum(:each_ancestor) unless block_given?

  return unless element

  yield element

  element.each_ancestor { |ancestor| yield ancestor }
end

#inspectString

Returns:

  • (String)


87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/oga/xml/attribute.rb', line 87

def inspect
  segments = []

  [:name, :namespace, :value].each do |attr|
    value = send(attr)

    if value
      segments << "#{attr}: #{value.inspect}"
    end
  end

  "Attribute(#{segments.join(' ')})"
end

#namespaceOga::XML::Namespace

Returns the Namespace instance for the current namespace name.

Returns:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/oga/xml/attribute.rb', line 49

def namespace
  unless @namespace
    if namespace_name == DEFAULT_NAMESPACE.name
      @namespace = DEFAULT_NAMESPACE
    else
      @namespace = element.available_namespaces[namespace_name]
    end
  end

  @namespace
end

#textString Also known as: to_s

Returns:

  • (String)


80
81
82
# File 'lib/oga/xml/attribute.rb', line 80

def text
  value.to_s
end

#valueString|NilClass

Returns the value of the attribute or nil if no explicit value was set.

Returns:

  • (String|NilClass)


70
71
72
73
74
75
76
77
# File 'lib/oga/xml/attribute.rb', line 70

def value
  if !@decoded and @value
    @value   = EntityDecoder.try_decode(@value, html?)
    @decoded = true
  end

  @value
end

#value=(value) ⇒ Object

Parameters:

  • value (String)


62
63
64
65
# File 'lib/oga/xml/attribute.rb', line 62

def value=(value)
  @value   = value
  @decoded = false
end