Skip to content

Commit

Permalink
Make MyModel.all.to_yaml and MyModel.all.to_json work.
Browse files Browse the repository at this point in the history
  • Loading branch information
trogdoro authored and michaelklishin committed May 27, 2008
1 parent fb8a533 commit c431e3a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
13 changes: 12 additions & 1 deletion dm-serializer/lib/dm-serializer.rb
Expand Up @@ -105,9 +105,20 @@ def to_xml_document(opts={})
doc
end

end # module Serialize
end # module Serialize

module Resource
include Serialize
end # module Resource

class Collection
def to_yaml(opts = {})
to_a.to_yaml(opts)
end

def to_json
to_a.to_json
end
end

end # module DataMapper
31 changes: 30 additions & 1 deletion dm-serializer/spec/unit/serializer_spec.rb
@@ -1,4 +1,5 @@
require 'pathname'
require 'yaml'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'

describe DataMapper::Serialize do
Expand All @@ -11,8 +12,14 @@ class Cow
property :name, String
property :breed, String
end
end

properties = Cow.properties(:default)
properties_with_indexes = Hash[*properties.zip((0...properties.length).to_a).flatten]
@collection = DataMapper::Collection.new(DataMapper::repository(:default), Cow, properties_with_indexes)
@collection.load([1, 2, 'Betsy', 'Jersey'])
@collection.load([10, 20, 'Berta', 'Guernsey'])

end

it "should serialize a resource to YAML" do
betsy = Cow.new
Expand All @@ -29,6 +36,21 @@ class Cow
EOS
end

it "should serialize a collection to YAML" do
@collection.to_yaml.gsub(/[[:space:]]+\n/, "\n").strip.should == <<-EOS.margin
---
- :id: 1
:composite: 2
:name: Betsy
:breed: Jersey
- :id: 10
:composite: 20
:name: Berta
:breed: Guernsey
EOS

end

it "should serialize a resource to XML" do
berta = Cow.new
berta.id = 89
Expand Down Expand Up @@ -60,6 +82,13 @@ class Cow
EOS
end

it "should serialize a collection to JSON" do
@collection.to_json.gsub(/[[:space:]]+\n/, "\n").strip.should ==
'[{ "id": 1, "composite": 2, "name": "Betsy", "breed": "Jersey" },' +
'{ "id": 10, "composite": 20, "name": "Berta", "breed": "Guernsey" }]'
end


it "should serialize a resource to CSV" do
peter = Cow.new
peter.id = 44
Expand Down

0 comments on commit c431e3a

Please sign in to comment.