Class: XmlFu::Array

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

Overview

Convert Array to XML String

Defined Under Namespace

Classes: MissingKeyException

Class Method Summary collapse

Class Method Details

.each_with_xml(arr, opts = {}) ⇒ Object

Convenience function to iterate over array items as well as providing a single location for logic

Parameters:

  • arr (Array)

    Array to iterate over

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

    Hash of options to pass to the iteration



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/xml-fu/array.rb', line 70

def self.each_with_xml(arr, opts={})
  xml = Builder::XmlMarkup.new
  xml.instruct! if opts[:instruct] == true
  opts.delete(:instruct)


  arr.each do |item|
    key = opts.fetch(:key, "")
    item_content = item

    # Attributes reuires duplicate or child elements will 
    # contain attributes of their siblings.
    attributes = (opts[:attributes] ? opts[:attributes].dup : {})

    if item.respond_to?(:keys)
      filtered = Hash.filter(item)
      item_content, attributes = filtered.first, filtered.last
    end

    item_name = ( Symbol === key ?
                 XmlFu::Node.symbol_conversion_algorithm.call(key) :
                 key.to_s )

    yield xml, item_name, item_content, attributes
  end

  xml.target!
end

.infer_node(item, attributes = {}) ⇒ Object

TODO:

Add node inferrance functionality

Note:

Do not use if you want stable functionality

Future Functionality - VERY ALPHA STAGE!!!

Parameters:

  • item

    Simple Value

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

    Hash of attributes to assign to inferred node.



55
56
57
58
59
60
61
62
63
64
# File 'lib/xml-fu/array.rb', line 55

def self.infer_node(item, attributes={})
  node_name = case item.class
              when "TrueClass"
              when "FalseClass"
                "Boolean"
              else
                item.class
              end
  Node.new(node_name, item, attributes).to_xml
end

.to_xml(array, options = {}) ⇒ Object

Convert Array to XML String of sibling XML nodes

Parameters:

  • array (Array)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :content_type (String)

    Either “collection” or “content”. (defaults to “content”)

  • :key (String, Symbol)

    Name of node.

  • :attributes (Hash)

    Possible hash of attributes to assign to nodes in array.

Returns:

  • String



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/xml-fu/array.rb', line 20

def self.to_xml(array, options={})
  each_with_xml(array, options) do |xml, key, item, attributes|
    case options[:content_type].to_s
    when "collection"
      raise(MissingKeyException, "Key name missing for collection") if key.empty?
      
      case 
      when ::Array === item
        xml << Array.to_xml(item.flatten,options)
      else
        xml << Node.new(key, item, attributes).to_xml
      end
    else
      # Array is content of node rather than collection of node elements
      case
      when ::Hash === item   
        xml << Hash.to_xml(item, options)
      when ::Array === item  
        xml << Array.to_xml(item, options)
      when XmlFu.infer_simple_value_nodes == true
        xml << infer_node(item, attributes)
      when item.respond_to?(:to_xml)
        xml << item.to_xml
      else
        # unknown xml tranfromation process
      end
    end
  end
end