Class: Moxml::Adapter::CustomizedOx::Attribute

Inherits:
Ox::Node
  • Object
show all
Defined in:
lib/moxml/adapter/customized_ox/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_name, value, parent = nil) ⇒ Attribute

Returns a new instance of Attribute.



9
10
11
12
13
14
# File 'lib/moxml/adapter/customized_ox/attribute.rb', line 9

def initialize(attr_name, value, parent = nil)
  self.name = attr_name
  @parent = parent
  @value = value # Explicitly set @value
  super(value)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/moxml/adapter/customized_ox/attribute.rb', line 7

def name
  @name
end

#prefixObject (readonly)

Returns the value of attribute prefix.



7
8
9
# File 'lib/moxml/adapter/customized_ox/attribute.rb', line 7

def prefix
  @prefix
end

Instance Method Details

#expanded_nameObject



25
26
27
# File 'lib/moxml/adapter/customized_ox/attribute.rb', line 25

def expanded_name
  [prefix, name].compact.join(":")
end

#to_sObject

Support string conversion



48
49
50
# File 'lib/moxml/adapter/customized_ox/attribute.rb', line 48

def to_s
  to_xml
end

#to_xmlObject

Serialize the attribute to XML format with proper escaping



36
37
38
39
40
41
42
43
44
45
# File 'lib/moxml/adapter/customized_ox/attribute.rb', line 36

def to_xml
  escaped_value = @value.to_s
    .gsub("&", "&")
    .gsub("<", "&lt;")
    .gsub(">", "&gt;")
    .gsub('"', "&quot;")
    .gsub("'", "&apos;")

  "#{expanded_name}=\"#{escaped_value}\""
end

#valueObject

Expose the value stored in Ox::Node Ox stores attribute values using @value instance variable



31
32
33
# File 'lib/moxml/adapter/customized_ox/attribute.rb', line 31

def value
  @value
end