Class: Oga::XML::Attribute
- Inherits:
-
Object
- Object
- Oga::XML::Attribute
- 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
The element this attribute belongs to.
-
#name ⇒ String
The name of the attribute.
- #namespace_name ⇒ String
Instance Method Summary collapse
-
#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)
- #to_xml ⇒ String
-
#value ⇒ String|NilClass
Returns the value of the attribute or nil if no explicit value was set.
- #value=(value) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Attribute
Returns a new instance of Attribute.
37 38 39 40 41 42 43 |
# File 'lib/oga/xml/attribute.rb', line 37 def initialize( = {}) @name = [:name] @value = [:value] @element = [:element] @namespace_name = [:namespace_name] end |
Instance Attribute Details
#element ⇒ Oga::XML::Element
The element this attribute belongs to.
16 17 18 |
# File 'lib/oga/xml/attribute.rb', line 16 def element @element end |
#name ⇒ String
The name of the attribute.
9 10 11 |
# File 'lib/oga/xml/attribute.rb', line 9 def name @name end |
#namespace_name ⇒ String
12 13 14 |
# File 'lib/oga/xml/attribute.rb', line 12 def namespace_name @namespace_name end |
Instance Method Details
#inspect ⇒ 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 |
#namespace ⇒ Oga::XML::Namespace
Returns the Namespace instance for the current namespace name.
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 |
#text ⇒ String Also known as: to_s
88 89 90 |
# File 'lib/oga/xml/attribute.rb', line 88 def text value.to_s end |
#to_xml ⇒ 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 |
#value ⇒ String|NilClass
Returns the value of the attribute or nil if no explicit value was set.
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
66 67 68 69 |
# File 'lib/oga/xml/attribute.rb', line 66 def value=(value) @value = value @decoded = false end |