Class: XmlFu::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/xml-fu/node.rb

Overview

Class to contain logic for converting a key/value pair into an XML node

Defined Under Namespace

Classes: InvalidAttributesException

Constant Summary collapse

XS_DATETIME_FORMAT =

xs:dateTime format.

"%Y-%m-%dT%H:%M:%SZ"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, attributes = {}) ⇒ Node

Create XmlFu::Node object

Parameters:

  • name (String, Symbol)

    Name of node

  • value

    Simple Value or nil

  • attributes (Hash) (defaults to: {})

    Optional hash of attributes to apply to XML Node



26
27
28
29
30
31
32
33
# File 'lib/xml-fu/node.rb', line 26

def initialize(name, value, attributes={})
  @escape_xml = true
  @self_closing = false
  @content_type = "container"
  self.attributes = attributes
  self.value = value
  self.name = name
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



36
37
38
# File 'lib/xml-fu/node.rb', line 36

def attributes
  @attributes
end

#content_typeObject

Returns the value of attribute content_type.



20
21
22
# File 'lib/xml-fu/node.rb', line 20

def content_type
  @content_type
end

#escape_xmlObject

Returns the value of attribute escape_xml.



18
19
20
# File 'lib/xml-fu/node.rb', line 18

def escape_xml
  @escape_xml
end

#nameObject

Returns the value of attribute name.



46
47
48
# File 'lib/xml-fu/node.rb', line 46

def name
  @name
end

#self_closingObject

Returns the value of attribute self_closing.



19
20
21
# File 'lib/xml-fu/node.rb', line 19

def self_closing
  @self_closing
end

Instance Method Details

#name_parse_special_characters(val) ⇒ Object

Converts name into proper XML node name

Parameters:

  • val (String, Symbol)

    Raw name



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
# File 'lib/xml-fu/node.rb', line 66

def name_parse_special_characters(val)
  use_this = val.dup

  # Ensure that we don't have special characters at end of name
  while ["!","/","*"].include?(use_this.to_s[-1,1]) do
    # Will this node contain escaped XML?
    if use_this.to_s[-1,1] == '!'
      @escape_xml = false
      use_this.chop!
    end

    # Will this be a self closing node?
    if use_this.to_s[-1,1] == '/'
      @self_closing = true
      use_this.chop!
    end

    # Will this node contain a collection of sibling nodes?
    if use_this.to_s[-1,1] == '*'
      @content_type = "collection"
      use_this.chop!
    end
  end

  return use_this
end

#to_xmlObject

Create XML String from XmlFu::Node object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/xml-fu/node.rb', line 129

def to_xml
  xml = Builder::XmlMarkup.new
  case
  when @self_closing && @content_type == 'container'
    xml.tag!(@name, @attributes)
  when @value.nil?
    xml.tag!(@name, @attributes.merge!("xsi:nil" => "true"))
  when ::Hash === @value
    xml.tag!(@name, @attributes) { xml << XmlFu::Hash.to_xml(@value) }
  when ::Array === @value
    case @content_type
    when "collection"
      xml << XmlFu::Array.to_xml(@value.flatten, {
        :key => (@self_closing ? "#{@name}/" : @name),
        :attributes => @attributes,
        :content_type => "collection"
      })
    when "container"
      xml.tag!(@name, @attributes) { xml << XmlFu::Array.to_xml(@value) }
    else
      # Shouldn't be anything else
    end
  else
    xml.tag!(@name, @attributes) { xml << self.value.to_s }
  end
  xml.target!
end

#valueString?

Value can be nil, else it should return a String value.

Returns:



122
123
124
125
# File 'lib/xml-fu/node.rb', line 122

def value
  return CGI.escapeHTML(@value) if String === @value && @escape_xml
  return @value
end

#value=(val) ⇒ Object

Custom Setter for @value instance method



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/xml-fu/node.rb', line 95

def value=(val)
  case val
  when ::String     then @value = val.to_s
  when ::Hash       then @value = val
  when ::Array      then @value = val
  when ::OpenStruct then @value = val
  when ::DateTime   then @value = val.strftime XS_DATETIME_FORMAT
  when ::Time       then @value = val.strftime XS_DATETIME_FORMAT
  when ::Date       then @value = val.strftime XS_DATETIME_FORMAT
  else
    if val.respond_to?(:to_datetime)
      @value = val.to_datetime
    elsif val.respond_to?(:call)
      @value = val.call
    elsif val.nil?
      @value = nil
    else
      @value = val.to_s
    end
  end
rescue => e
  @value = val.to_s
end