Class: DataMapper::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-serializer/to_csv.rb,
lib/dm-serializer/to_xml.rb,
lib/dm-serializer/to_json.rb,
lib/dm-serializer/to_yaml.rb

Instance Method Summary collapse

Instance Method Details

#to_csvObject



31
32
33
34
35
36
37
# File 'lib/dm-serializer/to_csv.rb', line 31

def to_csv
  result = ''
  each do |item|
    result << item.to_csv + "\n"
  end
  result
end

#to_json(*args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dm-serializer/to_json.rb', line 66

def to_json(*args)
  opts = args.first || {}

  options = opts.merge(:to_json => false)
  collection = map { |e| e.to_json(options) }

  # default to making JSON
  if opts.fetch(:to_json, true)
    collection.to_json
  else
    collection
  end
end

#to_xml(opts = {}) ⇒ Object



51
52
53
# File 'lib/dm-serializer/to_xml.rb', line 51

def to_xml(opts = {})
  to_xml_document(opts).to_s
end

#to_yaml(opts_or_emitter = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/dm-serializer/to_yaml.rb', line 38

def to_yaml(opts_or_emitter = {})
  if !opts_or_emitter.is_a?(Hash)
    to_a.to_yaml(opts_or_emitter)
  else
    # FIXME: Don't double handle the YAML (remove the YAML.load)
    to_a.collect {|x| YAML.load(x.to_yaml(opts_or_emitter)) }.to_yaml
  end
end