Class: XMLNode

Inherits:
String
  • Object
show all
Defined in:
lib/apricoteatsgorilla/xml_node.rb

Overview

XMLNode

Representation of an XML node. Inherits from String and includes some useful XML-specific methods for namespaces, attributes, node content etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, namespaces = {}) ⇒ XMLNode

Initializer + expects the node name and an optional Hash of namespaces.



14
15
16
17
# File 'lib/apricoteatsgorilla/xml_node.rb', line 14

def initialize(name, namespaces = {})
  super(name)
  set_namespace(namespaces)
end

Instance Attribute Details

#attributes=(value) ⇒ Object

Hash of attributes.



8
9
10
# File 'lib/apricoteatsgorilla/xml_node.rb', line 8

def attributes=(value)
  @attributes = value
end

#body=(value) ⇒ Object

Node body content.



11
12
13
# File 'lib/apricoteatsgorilla/xml_node.rb', line 11

def body=(value)
  @body = value
end

Instance Method Details

#strip_namespace!Object

Strips the namespace from this node.



20
21
22
# File 'lib/apricoteatsgorilla/xml_node.rb', line 20

def strip_namespace!
  sub!(/.+:(.+)/, '\1')
end

#to_lower_camel_case!Object

Converts this node to lowerCamelCase.



32
33
34
# File 'lib/apricoteatsgorilla/xml_node.rb', line 32

def to_lower_camel_case!
  gsub!(/_(.)/) { $1.upcase }
end

#to_snake_case!Object

Converts this node to snake_case.



25
26
27
28
29
# File 'lib/apricoteatsgorilla/xml_node.rb', line 25

def to_snake_case!
  gsub!(/[A-Z]/, '_\0')
  gsub!(/^_/, '')
  downcase!
end

#to_tagObject

Returns this node as an XML tag including a namespace, attributes and a body in case these values were supplied.



38
39
40
41
42
43
44
# File 'lib/apricoteatsgorilla/xml_node.rb', line 38

def to_tag
  if @body
    "<#{namespace}#{self}#{attributes}>#{body}</#{namespace}#{self}>"
  else
    "<#{namespace}#{self}#{attributes} />"
  end
end