class Oga::XML::Attribute

Class for storing information about a single XML attribute.

Constants

DEFAULT_NAMESPACE

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

@return [Oga::XML::Namespace]

Attributes

element[RW]

The element this attribute belongs to. @return [Oga::XML::Element]

name[RW]

The name of the attribute. @return [String]

namespace_name[RW]

@return [String]

parent[RW]

The element this attribute belongs to. @return [Oga::XML::Element]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options

@option options [String] :name @option options [String] :namespace_name @option options [String] :value @option options [Oga::XML::Element] :element

# 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

Public Instance Methods

each_ancestor() { |element| ... } click to toggle source

@see [Oga::XML::Node#each_ancestor]

# 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
inspect() click to toggle source

@return [String]

# 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
namespace() click to toggle source

Returns the {Oga::XML::Namespace} instance for the current namespace name.

@return [Oga::XML::Namespace]

# 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
text() click to toggle source

@return [String]

# File lib/oga/xml/attribute.rb, line 80
def text
  value.to_s
end
Also aliased as: to_s
to_s()
Alias for: text
value() click to toggle source

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

@return [String|NilClass]

# 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) click to toggle source

@param [String] value

# File lib/oga/xml/attribute.rb, line 62
def value=(value)
  @value   = value
  @decoded = false
end

Private Instance Methods

html?() click to toggle source

@return [TrueClass|FalseClass]

# File lib/oga/xml/attribute.rb, line 115
def html?
  !!@element && @element.html?
end