Module: XmlFu

Defined in:
lib/xml-fu.rb,
lib/xml-fu/hash.rb,
lib/xml-fu/node.rb,
lib/xml-fu/array.rb,
lib/xml-fu/markup.rb,
lib/xml-fu/version.rb,
lib/xml-fu/open_struct.rb,
lib/xml-fu/configuration.rb

Defined Under Namespace

Classes: Array, Configuration, Hash, Markup, Node, OpenStruct

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.configObject



64
65
66
# File 'lib/xml-fu.rb', line 64

def self.config
  @@config ||= Configuration.new
end

.configure {|Configuration| ... } ⇒ Object

Modify configuration for library

Yields:



59
60
61
# File 'lib/xml-fu.rb', line 59

def self.configure
  yield self.config if block_given?
end

.recognized_object?(obj) ⇒ Boolean

Used to determine if the top-level #xml method recognizes the passed object

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
# File 'lib/xml-fu.rb', line 41

def self.recognized_object?(obj)
  case obj
  when ::Hash       then return true
  when ::Array      then return true
  when ::OpenStruct then return true
  else
    return true if obj.respond_to?(:to_xml)
    return false
  end
end

.xml(construct, options = {}) ⇒ Object

Convert construct into XML



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/xml-fu.rb', line 11

def self.xml(construct, options={})

  # Override options for xml_declaration if config is present
  unless self.config.include_xml_declaration.nil?
    options[:instruct] = self.config.include_xml_declaration
  end

  case construct
  when ::OpenStruct then
    OpenStruct.to_xml( construct.dup, options )
  when ::Hash then
    Hash.to_xml( construct.dup, options )
  when ::Array then
    Array.to_xml( construct.dup, options )
  else
    if construct.respond_to?(:to_xml)
      construct.to_xml
    else
      # Options have been exhausted
      if self.config.fail_on_invalid_construct
        raise ArgumentError, "Invalid construct"
      else
        nil
      end
    end
  end
end