Class: Oga::XML::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/oga/xml/attribute.rb

Overview

Class for storing information about a single XML attribute.

Constant Summary collapse

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

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)


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

def initialize(options = {})
  @name    = options[:name]
  @value   = options[:value]
  @element = options[:element]

  @namespace_name = options[:namespace_name]
end

Instance Attribute Details

#elementOga::XML::Element

The element this attribute belongs to.

Returns:



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

def element
  @element
end

#nameString

The name of the attribute.

Returns:

  • (String)


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

def name
  @name
end

#namespace_nameString

Returns:

  • (String)


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

def namespace_name
  @namespace_name
end

Instance Method Details

#inspectString

Returns:

  • (String)


112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/oga/xml/attribute.rb', line 112

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:



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

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)


88
89
90
# File 'lib/oga/xml/attribute.rb', line 88

def text
  value.to_s
end

#to_xmlString

Returns:

  • (String)


97
98
99
100
101
102
103
104
105
106
107
# File 'lib/oga/xml/attribute.rb', line 97

def to_xml
  if namespace_name
    full_name = "#{namespace_name}:#{name}"
  else
    full_name = name
  end

  enc_value = value ? Entities.encode_attribute(value) : nil

  %Q(#{full_name}="#{enc_value}")
end

#valueString|NilClass

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

Returns:

  • (String|NilClass)


76
77
78
79
80
81
82
83
# File 'lib/oga/xml/attribute.rb', line 76

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

  @value
end

#value=(value) ⇒ Object

Parameters:

  • value (String)


66
67
68
69
# File 'lib/oga/xml/attribute.rb', line 66

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