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  => 'http://www.w3.org/XML/1998/namespace'
).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)

43
44
45
46
47
48
49
# File 'lib/oga/xml/attribute.rb', line 43

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:


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/oga/xml/attribute.rb', line 21

class Attribute
  attr_accessor :name, :namespace_name, :element, :value

  ##
  # The default namespace available to all attributes. This namespace can
  # not be modified.
  #
  # @return [Oga::XML::Namespace]
  #
  DEFAULT_NAMESPACE = Namespace.new(
    :name => 'xml',
    :uri  => 'http://www.w3.org/XML/1998/namespace'
  ).freeze

  ##
  # @param [Hash] options
  #
  # @option options [String] :name
  # @option options [String] :namespace_name
  # @option options [String] :value
  # @option options [Oga::XML::Element] :element
  #
  def initialize(options = {})
    @name    = options[:name]
    @value   = options[:value]
    @element = options[:element]

    @namespace_name = options[:namespace_name]
  end

  ##
  # Returns the {Oga::XML::Namespace} instance for the current namespace
  # name.
  #
  # @return [Oga::XML::Namespace]
  #
  def namespace
    unless @namespace
      if namespace_name == DEFAULT_NAMESPACE.name
        @namespace = DEFAULT_NAMESPACE
      else
        @namespace = element.available_namespaces[namespace_name]
      end
    end

    return @namespace
  end

  ##
  # Returns the value of the attribute.
  #
  # @return [String]
  #
  def text
    return value.to_s
  end

  alias_method :to_s, :text

  ##
  # @return [String]
  #
  def to_xml
    if namespace_name
      full_name = "#{namespace_name}:#{name}"
    else
      full_name = name
    end

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

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

  ##
  # @return [String]
  #
  def inspect
    segments = []

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

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

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

#nameString

The name of the attribute.

Returns:

  • (String)

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/oga/xml/attribute.rb', line 21

class Attribute
  attr_accessor :name, :namespace_name, :element, :value

  ##
  # The default namespace available to all attributes. This namespace can
  # not be modified.
  #
  # @return [Oga::XML::Namespace]
  #
  DEFAULT_NAMESPACE = Namespace.new(
    :name => 'xml',
    :uri  => 'http://www.w3.org/XML/1998/namespace'
  ).freeze

  ##
  # @param [Hash] options
  #
  # @option options [String] :name
  # @option options [String] :namespace_name
  # @option options [String] :value
  # @option options [Oga::XML::Element] :element
  #
  def initialize(options = {})
    @name    = options[:name]
    @value   = options[:value]
    @element = options[:element]

    @namespace_name = options[:namespace_name]
  end

  ##
  # Returns the {Oga::XML::Namespace} instance for the current namespace
  # name.
  #
  # @return [Oga::XML::Namespace]
  #
  def namespace
    unless @namespace
      if namespace_name == DEFAULT_NAMESPACE.name
        @namespace = DEFAULT_NAMESPACE
      else
        @namespace = element.available_namespaces[namespace_name]
      end
    end

    return @namespace
  end

  ##
  # Returns the value of the attribute.
  #
  # @return [String]
  #
  def text
    return value.to_s
  end

  alias_method :to_s, :text

  ##
  # @return [String]
  #
  def to_xml
    if namespace_name
      full_name = "#{namespace_name}:#{name}"
    else
      full_name = name
    end

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

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

  ##
  # @return [String]
  #
  def inspect
    segments = []

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

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

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

#namespace_nameString

Returns:

  • (String)

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/oga/xml/attribute.rb', line 21

class Attribute
  attr_accessor :name, :namespace_name, :element, :value

  ##
  # The default namespace available to all attributes. This namespace can
  # not be modified.
  #
  # @return [Oga::XML::Namespace]
  #
  DEFAULT_NAMESPACE = Namespace.new(
    :name => 'xml',
    :uri  => 'http://www.w3.org/XML/1998/namespace'
  ).freeze

  ##
  # @param [Hash] options
  #
  # @option options [String] :name
  # @option options [String] :namespace_name
  # @option options [String] :value
  # @option options [Oga::XML::Element] :element
  #
  def initialize(options = {})
    @name    = options[:name]
    @value   = options[:value]
    @element = options[:element]

    @namespace_name = options[:namespace_name]
  end

  ##
  # Returns the {Oga::XML::Namespace} instance for the current namespace
  # name.
  #
  # @return [Oga::XML::Namespace]
  #
  def namespace
    unless @namespace
      if namespace_name == DEFAULT_NAMESPACE.name
        @namespace = DEFAULT_NAMESPACE
      else
        @namespace = element.available_namespaces[namespace_name]
      end
    end

    return @namespace
  end

  ##
  # Returns the value of the attribute.
  #
  # @return [String]
  #
  def text
    return value.to_s
  end

  alias_method :to_s, :text

  ##
  # @return [String]
  #
  def to_xml
    if namespace_name
      full_name = "#{namespace_name}:#{name}"
    else
      full_name = name
    end

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

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

  ##
  # @return [String]
  #
  def inspect
    segments = []

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

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

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

#valueString

The value of the attribute.

Returns:

  • (String)

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/oga/xml/attribute.rb', line 21

class Attribute
  attr_accessor :name, :namespace_name, :element, :value

  ##
  # The default namespace available to all attributes. This namespace can
  # not be modified.
  #
  # @return [Oga::XML::Namespace]
  #
  DEFAULT_NAMESPACE = Namespace.new(
    :name => 'xml',
    :uri  => 'http://www.w3.org/XML/1998/namespace'
  ).freeze

  ##
  # @param [Hash] options
  #
  # @option options [String] :name
  # @option options [String] :namespace_name
  # @option options [String] :value
  # @option options [Oga::XML::Element] :element
  #
  def initialize(options = {})
    @name    = options[:name]
    @value   = options[:value]
    @element = options[:element]

    @namespace_name = options[:namespace_name]
  end

  ##
  # Returns the {Oga::XML::Namespace} instance for the current namespace
  # name.
  #
  # @return [Oga::XML::Namespace]
  #
  def namespace
    unless @namespace
      if namespace_name == DEFAULT_NAMESPACE.name
        @namespace = DEFAULT_NAMESPACE
      else
        @namespace = element.available_namespaces[namespace_name]
      end
    end

    return @namespace
  end

  ##
  # Returns the value of the attribute.
  #
  # @return [String]
  #
  def text
    return value.to_s
  end

  alias_method :to_s, :text

  ##
  # @return [String]
  #
  def to_xml
    if namespace_name
      full_name = "#{namespace_name}:#{name}"
    else
      full_name = name
    end

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

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

  ##
  # @return [String]
  #
  def inspect
    segments = []

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

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

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

Instance Method Details

#inspectString

Returns:

  • (String)

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

def inspect
  segments = []

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

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

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

#namespaceOga::XML::Namespace

Returns the Namespace instance for the current namespace name.

Returns:


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/oga/xml/attribute.rb', line 57

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

  return @namespace
end

#textString Also known as: to_s

Returns the value of the attribute.

Returns:

  • (String)

74
75
76
# File 'lib/oga/xml/attribute.rb', line 74

def text
  return value.to_s
end

#to_xmlString

Returns:

  • (String)

83
84
85
86
87
88
89
90
91
92
93
# File 'lib/oga/xml/attribute.rb', line 83

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

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

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