Class: Oga::XML::Attribute
- Inherits:
-
Object
- Object
- Oga::XML::Attribute
- Includes:
- ExpandedName, ToXML
- 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.
Namespace.new( :name => 'xml', :uri => XML::DEFAULT_NAMESPACE.uri ).freeze
Instance Attribute Summary collapse
-
#element ⇒ Oga::XML::Element
(also: #parent)
The element this attribute belongs to.
-
#name ⇒ String
The name of the attribute.
-
#namespace_name ⇒ String
Instance Method Summary collapse
-
#each_ancestor {|element| ... } ⇒ Object
-
#initialize(options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
-
#inspect ⇒ String
-
#namespace ⇒ Oga::XML::Namespace
Returns the Namespace instance for the current namespace name.
-
#text ⇒ String
(also: #to_s)
-
#value ⇒ String|NilClass
Returns the value of the attribute or nil if no explicit value was set.
-
#value=(value) ⇒ Object
Methods included from ToXML
Methods included from ExpandedName
Constructor Details
#initialize(options = {}) ⇒ Attribute
Returns a new instance of Attribute
36 37 38 39 40 41 42 43 |
# File 'lib/oga/xml/attribute.rb', line 36 def initialize( = {}) @name = [:name] @value = [:value] @element = [:element] @decoded = false @namespace = nil @namespace_name = [:namespace_name] end |
Instance Attribute Details
#element ⇒ Oga::XML::Element Also known as: parent
The element this attribute belongs to.
17 18 19 |
# File 'lib/oga/xml/attribute.rb', line 17 def element @element end |
#name ⇒ String
The name of the attribute.
10 11 12 |
# File 'lib/oga/xml/attribute.rb', line 10 def name @name end |
#namespace_name ⇒ 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
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 |
#inspect ⇒ 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 |
#namespace ⇒ Oga::XML::Namespace
Returns the Namespace instance for the current namespace name.
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 |
#text ⇒ String Also known as: to_s
80 81 82 |
# File 'lib/oga/xml/attribute.rb', line 80 def text value.to_s end |
#value ⇒ String|NilClass
Returns the value of the attribute or nil if no explicit value was set.
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
62 63 64 65 |
# File 'lib/oga/xml/attribute.rb', line 62 def value=(value) @value = value @decoded = false end |