Class: Array

Inherits:
Object show all
Defined in:
lib/mongo_mapper/support.rb,
lib/mongo_mapper/plugins/serialization/array.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_mongo(value) ⇒ Object



8
9
10
# File 'lib/mongo_mapper/support.rb', line 8

def self.from_mongo(value)
  value || []
end

.to_mongo(value) ⇒ Object



3
4
5
6
# File 'lib/mongo_mapper/support.rb', line 3

def self.to_mongo(value)
  value = value.respond_to?(:lines) ? value.lines : value
  value.to_a
end

Instance Method Details

#to_xml(options = {}) ⇒ Object



16
17
18
19
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
49
50
51
52
53
54
# File 'lib/mongo_mapper/plugins/serialization/array.rb', line 16

def to_xml(options = {})
  #raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
  require 'builder' unless defined?(Builder)

  options = options.dup
  options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records"
  options[:children] ||= options[:root].singularize
  options[:indent] ||= 2
  options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])

  root = options.delete(:root).to_s
  children = options.delete(:children)

  if !options.has_key?(:dasherize) || options[:dasherize]
    root = root.dasherize
  end

  options[:builder].instruct! unless options.delete(:skip_instruct)

  opts = options.merge({ :root => children })

  root = root.pluralize
  
  xml = options[:builder]
  if empty?
    xml.tag!(root, options[:skip_types] ? {} : {:type => "array"})
  else
    xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) do
      yield xml if block_given?
      each do |e|
        if e.respond_to? :to_xml
          e.to_xml(opts.merge({ :skip_instruct => true }))
        else
          xml.tag!(root.singularize, e.to_s)
        end
      end
    end
  end
end