dm-serializer 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README +4 -0
- data/Rakefile +65 -0
- data/TODO +8 -0
- data/lib/dm-serializer.rb +160 -0
- data/spec/fixtures/cow.rb +8 -0
- data/spec/fixtures/planet.rb +18 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +37 -0
- data/spec/unit/serializer_spec.rb +8 -0
- data/spec/unit/to_csv_spec.rb +34 -0
- data/spec/unit/to_json_spec.rb +109 -0
- data/spec/unit/to_xml_spec.rb +42 -0
- data/spec/unit/to_yaml_spec.rb +62 -0
- metadata +76 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Guy van den Berg
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require 'spec/rake/spectask'
|
6
|
+
require 'pathname'
|
7
|
+
|
8
|
+
CLEAN.include '{log,pkg}/'
|
9
|
+
|
10
|
+
spec = Gem::Specification.new do |s|
|
11
|
+
s.name = 'dm-serializer'
|
12
|
+
s.version = '0.9.2'
|
13
|
+
s.platform = Gem::Platform::RUBY
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.extra_rdoc_files = %w[ README LICENSE TODO ]
|
16
|
+
s.summary = 'DataMapper plugin for serializing DataMapper objects'
|
17
|
+
s.description = s.summary
|
18
|
+
s.author = 'Guy van den Berg'
|
19
|
+
s.email = 'vandenberg.guy@gmail.com'
|
20
|
+
s.homepage = 'http://github.com/sam/dm-more/tree/master/dm-serializer'
|
21
|
+
s.require_path = 'lib'
|
22
|
+
s.files = FileList[ '{lib,spec}/**/*.rb', 'spec/spec.opts', 'Rakefile', *s.extra_rdoc_files ]
|
23
|
+
s.add_dependency('dm-core', "=#{s.version}")
|
24
|
+
end
|
25
|
+
|
26
|
+
task :default => [ :spec ]
|
27
|
+
|
28
|
+
WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
|
29
|
+
SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
|
30
|
+
|
31
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
32
|
+
pkg.gem_spec = spec
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Install #{spec.name} #{spec.version} (default ruby)"
|
36
|
+
task :install => [ :package ] do
|
37
|
+
sh "#{SUDO} gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources", :verbose => false
|
38
|
+
end
|
39
|
+
|
40
|
+
desc "Uninstall #{spec.name} #{spec.version} (default ruby)"
|
41
|
+
task :uninstall => [ :clobber ] do
|
42
|
+
sh "#{SUDO} gem uninstall #{spec.name} -v#{spec.version} -I -x", :verbose => false
|
43
|
+
end
|
44
|
+
|
45
|
+
namespace :jruby do
|
46
|
+
desc "Install #{spec.name} #{spec.version} with JRuby"
|
47
|
+
task :install => [ :package ] do
|
48
|
+
sh %{#{SUDO} jruby -S gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources}, :verbose => false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
desc 'Run specifications'
|
53
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
54
|
+
t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
|
55
|
+
t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
|
56
|
+
|
57
|
+
begin
|
58
|
+
t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
|
59
|
+
t.rcov_opts << '--exclude' << 'spec'
|
60
|
+
t.rcov_opts << '--text-summary'
|
61
|
+
t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
|
62
|
+
rescue Exception
|
63
|
+
# rcov not installed
|
64
|
+
end
|
65
|
+
end
|
data/TODO
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
require Pathname('rexml/document')
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'faster_csv'
|
5
|
+
rescue LoadError
|
6
|
+
nil
|
7
|
+
end
|
8
|
+
|
9
|
+
begin
|
10
|
+
require Pathname('json/ext')
|
11
|
+
rescue LoadError
|
12
|
+
require Pathname('json/pure')
|
13
|
+
end
|
14
|
+
|
15
|
+
module DataMapper
|
16
|
+
module Serialize
|
17
|
+
|
18
|
+
# Serialize a Resource to JavaScript Object Notation (JSON; RFC 4627)
|
19
|
+
#
|
20
|
+
# @return <String> a JSON representation of the Resource
|
21
|
+
def to_json(options = {})
|
22
|
+
result = '{ '
|
23
|
+
fields = []
|
24
|
+
|
25
|
+
# FIXME: this should go into bunch of protected methods shared with other serialization methods
|
26
|
+
only_properties = options[:only] || []
|
27
|
+
excluded_properties = options[:exclude] || []
|
28
|
+
exclude_read_only = options[:without_read_only_attributes] || false
|
29
|
+
|
30
|
+
propset = self.class.properties(repository.name)
|
31
|
+
|
32
|
+
# FIXME: this ugly condition is here because PropertySet does not support reject/select yet.
|
33
|
+
unless only_properties.empty?
|
34
|
+
propset.each do |property|
|
35
|
+
fields << "#{property.name.to_json}: #{send(property.getter).to_json}" if only_properties.include?(property.name.to_sym)
|
36
|
+
end
|
37
|
+
else
|
38
|
+
propset.each do |property|
|
39
|
+
fields << "#{property.name.to_json}: #{send(property.getter).to_json}" unless excluded_properties.include?(property.name.to_sym)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
if self.class.respond_to?(:read_only_attributes) && exclude_read_only
|
44
|
+
self.class.read_only_attributes.each do |property|
|
45
|
+
fields << "#{property.to_json}: #{send(property).to_json}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# add methods
|
50
|
+
(options[:methods] || []).each do |meth|
|
51
|
+
if self.respond_to?(meth)
|
52
|
+
fields << "#{meth.to_json}: #{send(meth).to_json}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
result << fields.join(', ')
|
57
|
+
result << ' }'
|
58
|
+
result
|
59
|
+
end
|
60
|
+
|
61
|
+
# Serialize a Resource to comma-separated values (CSV).
|
62
|
+
#
|
63
|
+
# @return <String> a CSV representation of the Resource
|
64
|
+
def to_csv(writer = '')
|
65
|
+
FasterCSV.generate(writer) do |csv|
|
66
|
+
row = []
|
67
|
+
self.class.properties(repository.name).each do |property|
|
68
|
+
row << send(property.name).to_s
|
69
|
+
end
|
70
|
+
csv << row
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Serialize a Resource to XML
|
75
|
+
#
|
76
|
+
# @return <REXML::Document> an XML representation of this Resource
|
77
|
+
def to_xml(opts = {})
|
78
|
+
to_xml_document(opts).to_s
|
79
|
+
end
|
80
|
+
|
81
|
+
# Serialize a Resource to YAML
|
82
|
+
#
|
83
|
+
# @return <YAML> a YAML representation of this Resource
|
84
|
+
def to_yaml(opts = {})
|
85
|
+
YAML::quick_emit(object_id,opts) do |out|
|
86
|
+
out.map(nil,to_yaml_style) do |map|
|
87
|
+
self.class.properties(repository.name).each do |property|
|
88
|
+
value = send(property.name.to_sym)
|
89
|
+
map.add(property.name, value.is_a?(Class) ? value.to_s : value)
|
90
|
+
end
|
91
|
+
(instance_variable_get("@yaml_addes") || []).each do |k,v|
|
92
|
+
map.add(k.to_s,v)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
protected
|
99
|
+
|
100
|
+
# Return the name of this Resource - to be used as the root element name.
|
101
|
+
# This can be overloaded.
|
102
|
+
#
|
103
|
+
# @return <String> name of this Resource
|
104
|
+
def xml_element_name
|
105
|
+
Extlib::Inflection.underscore(self.class.name)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Return a REXML::Document representing this Resource
|
109
|
+
#
|
110
|
+
# @return <REXML::Document> an XML representation of this Resource
|
111
|
+
def to_xml_document(opts={})
|
112
|
+
doc = REXML::Document.new
|
113
|
+
root = doc.add_element(xml_element_name)
|
114
|
+
|
115
|
+
#TODO old code base was converting single quote to double quote on attribs
|
116
|
+
|
117
|
+
self.class.properties(repository.name).each do |property|
|
118
|
+
value = send(property.name)
|
119
|
+
node = root.add_element(property.name.to_s)
|
120
|
+
if property.key?
|
121
|
+
node.attributes["type"] = property.type.to_s.downcase
|
122
|
+
end
|
123
|
+
node << REXML::Text.new(value.to_s) unless value.nil?
|
124
|
+
end
|
125
|
+
doc
|
126
|
+
end
|
127
|
+
|
128
|
+
end # module Serialize
|
129
|
+
|
130
|
+
module Resource
|
131
|
+
include Serialize
|
132
|
+
end # module Resource
|
133
|
+
|
134
|
+
class Collection
|
135
|
+
def to_yaml(opts = {})
|
136
|
+
to_a.to_yaml(opts)
|
137
|
+
end
|
138
|
+
|
139
|
+
def to_json(opts = {})
|
140
|
+
"[" << map {|e| e.to_json(opts)}.join(",") << "]"
|
141
|
+
end
|
142
|
+
|
143
|
+
def to_xml
|
144
|
+
result = ""
|
145
|
+
each do |item|
|
146
|
+
result << item.to_xml + "\n"
|
147
|
+
end
|
148
|
+
result
|
149
|
+
end
|
150
|
+
|
151
|
+
def to_csv
|
152
|
+
result = ""
|
153
|
+
each do |item|
|
154
|
+
result << item.to_csv + "\n"
|
155
|
+
end
|
156
|
+
result
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
end # module DataMapper
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Planet
|
2
|
+
include DataMapper::Resource
|
3
|
+
|
4
|
+
property :name, String, :key => true
|
5
|
+
property :aphelion, Float
|
6
|
+
|
7
|
+
def category
|
8
|
+
case self.name.downcase
|
9
|
+
when "mercury", "venus", "earth", "mars" then "terrestrial"
|
10
|
+
when "jupiter", "saturn", "uranus", "neptune" then "gas giants"
|
11
|
+
when "pluto" then "dwarf planets"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def has_known_form_of_life?
|
16
|
+
self.name.downcase == "earth"
|
17
|
+
end
|
18
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
gem 'dm-core', '=0.9.2'
|
5
|
+
require 'dm-core'
|
6
|
+
|
7
|
+
spec_dir_path = Pathname(__FILE__).dirname.expand_path
|
8
|
+
require spec_dir_path.parent + 'lib/dm-serializer'
|
9
|
+
|
10
|
+
def load_driver(name, default_uri)
|
11
|
+
return false if ENV['ADAPTER'] != name.to_s
|
12
|
+
|
13
|
+
lib = "do_#{name}"
|
14
|
+
|
15
|
+
begin
|
16
|
+
gem lib, '=0.9.2'
|
17
|
+
require lib
|
18
|
+
DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
|
19
|
+
DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
|
20
|
+
true
|
21
|
+
rescue Gem::LoadError => e
|
22
|
+
warn "Could not load #{lib}: #{e}"
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
ENV['ADAPTER'] ||= 'sqlite3'
|
28
|
+
|
29
|
+
HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
|
30
|
+
HAS_MYSQL = load_driver(:mysql, 'mysql://localhost/dm_core_test')
|
31
|
+
HAS_POSTGRES = load_driver(:postgres, 'postgres://postgres@localhost/dm_core_test')
|
32
|
+
|
33
|
+
|
34
|
+
# require fixture resources
|
35
|
+
Dir[spec_dir_path + "fixtures/*.rb"].each do |fixture_file|
|
36
|
+
require fixture_file
|
37
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
3
|
+
|
4
|
+
describe DataMapper::Serialize, '#to_csv' do
|
5
|
+
#
|
6
|
+
# ==== blah, it's CSV
|
7
|
+
#
|
8
|
+
|
9
|
+
before(:all) do
|
10
|
+
query = DataMapper::Query.new(DataMapper::repository(:default), Cow)
|
11
|
+
|
12
|
+
@collection = DataMapper::Collection.new(query) do |c|
|
13
|
+
c.load([1, 2, 'Betsy', 'Jersey'])
|
14
|
+
c.load([10, 20, 'Berta', 'Guernsey'])
|
15
|
+
end
|
16
|
+
|
17
|
+
@empty_collection = DataMapper::Collection.new(query) {}
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should serialize a resource to CSV" do
|
21
|
+
peter = Cow.new
|
22
|
+
peter.id = 44
|
23
|
+
peter.composite = 344
|
24
|
+
peter.name = 'Peter'
|
25
|
+
peter.breed = 'Long Horn'
|
26
|
+
peter.to_csv.chomp.should == '44,344,Peter,Long Horn'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should serialize a collection to CSV" do
|
30
|
+
@collection.to_csv.gsub(/[[:space:]]+\n/, "\n").should ==
|
31
|
+
"1,2,Betsy,Jersey\n" +
|
32
|
+
"10,20,Berta,Guernsey\n"
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
3
|
+
|
4
|
+
describe DataMapper::Serialize, '#to_json' do
|
5
|
+
#
|
6
|
+
# ==== ajaxy JSON
|
7
|
+
#
|
8
|
+
|
9
|
+
before(:all) do
|
10
|
+
query = DataMapper::Query.new(DataMapper::repository(:default), Cow)
|
11
|
+
|
12
|
+
@collection = DataMapper::Collection.new(query) do |c|
|
13
|
+
c.load([1, 2, 'Betsy', 'Jersey'])
|
14
|
+
c.load([10, 20, 'Berta', 'Guernsey'])
|
15
|
+
end
|
16
|
+
|
17
|
+
@empty_collection = DataMapper::Collection.new(query) {}
|
18
|
+
end
|
19
|
+
|
20
|
+
it "serializes resource to JSON" do
|
21
|
+
deserialized_hash = JSON.parse(Cow.new(:id => 1, :composite => 322, :name => "Harry", :breed => "Angus").to_json)
|
22
|
+
|
23
|
+
deserialized_hash["id"].should == 1
|
24
|
+
deserialized_hash["composite"].should == 322
|
25
|
+
deserialized_hash["name"].should == "Harry"
|
26
|
+
deserialized_hash["breed"].should == "Angus"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "excludes nil attributes" do
|
30
|
+
deserialized_hash = JSON.parse(Cow.new(:id => 1, :name => "Harry", :breed => "Angus").to_json)
|
31
|
+
|
32
|
+
deserialized_hash["id"].should == 1
|
33
|
+
deserialized_hash["composite"].should be(nil)
|
34
|
+
deserialized_hash["name"].should == "Harry"
|
35
|
+
deserialized_hash["breed"].should == "Angus"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "serializes collections to JSON by serializing each member" do
|
39
|
+
deserialized_collection = JSON.parse(@collection.to_json)
|
40
|
+
betsy = deserialized_collection.first
|
41
|
+
berta = deserialized_collection.last
|
42
|
+
|
43
|
+
betsy["id"].should == 1
|
44
|
+
betsy["composite"].should == 2
|
45
|
+
betsy["name"].should == "Betsy"
|
46
|
+
betsy["breed"].should == "Jersey"
|
47
|
+
|
48
|
+
berta["id"].should == 10
|
49
|
+
berta["composite"].should == 20
|
50
|
+
berta["name"].should == "Berta"
|
51
|
+
berta["breed"].should == "Guernsey"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "handles empty collections just fine" do
|
55
|
+
deserialized_collection = JSON.parse(@empty_collection.to_json)
|
56
|
+
deserialized_collection.should be_empty
|
57
|
+
end
|
58
|
+
|
59
|
+
it "handles options given to a collection properly" do
|
60
|
+
deserialized_collection = JSON.parse(@collection.to_json(:only => [:composite]))
|
61
|
+
betsy = deserialized_collection.first
|
62
|
+
berta = deserialized_collection.last
|
63
|
+
|
64
|
+
betsy["id"].should be_nil
|
65
|
+
betsy["composite"].should == 2
|
66
|
+
betsy["name"].should be_nil
|
67
|
+
betsy["breed"].should be_nil
|
68
|
+
|
69
|
+
berta["id"].should be_nil
|
70
|
+
berta["composite"].should == 20
|
71
|
+
berta["name"].should be_nil
|
72
|
+
berta["breed"].should be_nil
|
73
|
+
end
|
74
|
+
|
75
|
+
it "serializes values returned by methods given to :methods option" do
|
76
|
+
deserialized_hash = JSON.parse(Planet.new(:name => "Mars", :aphelion => 249_209_300.4).to_json(:methods => [:category, :has_known_form_of_life?]))
|
77
|
+
|
78
|
+
deserialized_hash["category"].should == "terrestrial"
|
79
|
+
deserialized_hash["has_known_form_of_life?"].should be(false)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "only includes properties given to :only option" do
|
83
|
+
deserialized_hash = JSON.parse(Planet.new(:name => "Mars", :aphelion => 249_209_300.4).to_json(:only => [:name]))
|
84
|
+
|
85
|
+
deserialized_hash["name"].should == "Mars"
|
86
|
+
deserialized_hash["aphelion"].should be(nil)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "only includes properties given to :only option" do
|
90
|
+
deserialized_hash = JSON.parse(Planet.new(:name => "Mars", :aphelion => 249_209_300.4).to_json(:exclude => [:aphelion]))
|
91
|
+
|
92
|
+
deserialized_hash["name"].should == "Mars"
|
93
|
+
deserialized_hash["aphelion"].should be(nil)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "has higher presedence for :only option" do
|
97
|
+
deserialized_hash = JSON.parse(Planet.new(:name => "Mars", :aphelion => 249_209_300.4).to_json(:only => [:aphelion], :exclude => [:aphelion]))
|
98
|
+
|
99
|
+
deserialized_hash["name"].should be(nil)
|
100
|
+
deserialized_hash["aphelion"].should == 249_209_300.4
|
101
|
+
end
|
102
|
+
|
103
|
+
it "supports :include option for one level depth"
|
104
|
+
|
105
|
+
it "supports :include option for more than one level depth"
|
106
|
+
|
107
|
+
it "has :repository option to override used repository"
|
108
|
+
|
109
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
3
|
+
|
4
|
+
describe DataMapper::Serialize, '#to_xml' do
|
5
|
+
#
|
6
|
+
# ==== enterprisey XML
|
7
|
+
#
|
8
|
+
|
9
|
+
before(:all) do
|
10
|
+
query = DataMapper::Query.new(DataMapper::repository(:default), Cow)
|
11
|
+
|
12
|
+
@collection = DataMapper::Collection.new(query) do |c|
|
13
|
+
c.load([1, 2, 'Betsy', 'Jersey'])
|
14
|
+
c.load([10, 20, 'Berta', 'Guernsey'])
|
15
|
+
end
|
16
|
+
|
17
|
+
@empty_collection = DataMapper::Collection.new(query) {}
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should serialize a resource to XML" do
|
21
|
+
berta = Cow.new
|
22
|
+
berta.id = 89
|
23
|
+
berta.composite = 34
|
24
|
+
berta.name = 'Berta'
|
25
|
+
berta.breed = 'Guernsey'
|
26
|
+
|
27
|
+
berta.to_xml.should == <<-EOS.compress_lines(false)
|
28
|
+
<cow>
|
29
|
+
<id type='integer'>89</id>
|
30
|
+
<composite type='integer'>34</composite>
|
31
|
+
<name>Berta</name>
|
32
|
+
<breed>Guernsey</breed>
|
33
|
+
</cow>
|
34
|
+
EOS
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should serialize a collection to XML" do
|
38
|
+
@collection.to_xml.gsub(/[[:space:]]+\n/, "\n").should ==
|
39
|
+
"<cow><id type='integer'>1</id><composite type='integer'>2</composite><name>Betsy</name><breed>Jersey</breed></cow>\n" +
|
40
|
+
"<cow><id type='integer'>10</id><composite type='integer'>20</composite><name>Berta</name><breed>Guernsey</breed></cow>\n"
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'yaml'
|
3
|
+
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
4
|
+
|
5
|
+
describe DataMapper::Serialize, '#to_yaml' do
|
6
|
+
#
|
7
|
+
# ==== yummy YAML
|
8
|
+
#
|
9
|
+
|
10
|
+
before(:all) do
|
11
|
+
query = DataMapper::Query.new(DataMapper::repository(:default), Cow)
|
12
|
+
|
13
|
+
@collection = DataMapper::Collection.new(query) do |c|
|
14
|
+
c.load([1, 2, 'Betsy', 'Jersey'])
|
15
|
+
c.load([10, 20, 'Berta', 'Guernsey'])
|
16
|
+
end
|
17
|
+
|
18
|
+
@empty_collection = DataMapper::Collection.new(query) {}
|
19
|
+
end
|
20
|
+
|
21
|
+
it "serializes single resource to YAML" do
|
22
|
+
betsy = Cow.new(:id => 230, :composite => 22, :name => "Betsy", :breed => "Jersey")
|
23
|
+
deserialized_hash = YAML.load(betsy.to_yaml)
|
24
|
+
|
25
|
+
deserialized_hash[:id].should == 230
|
26
|
+
deserialized_hash[:name].should == "Betsy"
|
27
|
+
deserialized_hash[:composite].should == 22
|
28
|
+
deserialized_hash[:breed].should == "Jersey"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "leaves out nil properties" do
|
32
|
+
betsy = Cow.new(:id => 230, :name => "Betsy", :breed => "Jersey")
|
33
|
+
deserialized_hash = YAML.load(betsy.to_yaml)
|
34
|
+
|
35
|
+
deserialized_hash[:id].should == 230
|
36
|
+
deserialized_hash[:name].should == "Betsy"
|
37
|
+
deserialized_hash[:composite].should be(nil)
|
38
|
+
deserialized_hash[:breed].should == "Jersey"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "serializes a collection to YAML" do
|
42
|
+
deserialized_collection = YAML.load(@collection.to_yaml)
|
43
|
+
|
44
|
+
betsy = deserialized_collection.first
|
45
|
+
berta = deserialized_collection.last
|
46
|
+
|
47
|
+
betsy[:id].should == 1
|
48
|
+
betsy[:name].should == "Betsy"
|
49
|
+
betsy[:composite].should == 2
|
50
|
+
betsy[:breed].should == "Jersey"
|
51
|
+
|
52
|
+
berta[:id].should == 10
|
53
|
+
berta[:name].should == "Berta"
|
54
|
+
berta[:composite].should == 20
|
55
|
+
berta[:breed].should == "Guernsey"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "handles empty collections just fine" do
|
59
|
+
YAML.load(@empty_collection.to_yaml).should be_empty
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Guy van den Berg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-25 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: dm-core
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - "="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.9.2
|
23
|
+
version:
|
24
|
+
description: DataMapper plugin for serializing DataMapper objects
|
25
|
+
email: vandenberg.guy@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
- LICENSE
|
33
|
+
- TODO
|
34
|
+
files:
|
35
|
+
- lib/dm-serializer.rb
|
36
|
+
- spec/fixtures/cow.rb
|
37
|
+
- spec/fixtures/planet.rb
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
- spec/unit/serializer_spec.rb
|
40
|
+
- spec/unit/to_csv_spec.rb
|
41
|
+
- spec/unit/to_json_spec.rb
|
42
|
+
- spec/unit/to_xml_spec.rb
|
43
|
+
- spec/unit/to_yaml_spec.rb
|
44
|
+
- spec/spec.opts
|
45
|
+
- Rakefile
|
46
|
+
- README
|
47
|
+
- LICENSE
|
48
|
+
- TODO
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/sam/dm-more/tree/master/dm-serializer
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.0.1
|
72
|
+
signing_key:
|
73
|
+
specification_version: 2
|
74
|
+
summary: DataMapper plugin for serializing DataMapper objects
|
75
|
+
test_files: []
|
76
|
+
|