ardm-serializer 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMapper::Serialize do
4
+ it "is included into DataMapper::Resource" do
5
+ Cow.new.should be_kind_of(DataMapper::Serialize)
6
+ end
7
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ if defined?(::CSV)
4
+ describe DataMapper::Serialize, '#to_csv' do
5
+ #
6
+ # ==== blah, it's CSV
7
+ #
8
+
9
+ before(:all) do
10
+ DataMapper.finalize
11
+ query = DataMapper::Query.new(DataMapper::repository(:default), Cow)
12
+
13
+ resources = [
14
+ {:id => 1, :composite => 2, :name => 'Betsy', :breed => 'Jersey'},
15
+ {:id => 10, :composite => 20, :name => 'Berta', :breed => 'Guernsey'}
16
+ ]
17
+
18
+ @collection = DataMapper::Collection.new(query, resources)
19
+
20
+ @empty_collection = DataMapper::Collection.new(query)
21
+ end
22
+
23
+ it "should serialize a resource to CSV" do
24
+ peter = Cow.new
25
+ peter.id = 44
26
+ peter.composite = 344
27
+ peter.name = 'Peter'
28
+ peter.breed = 'Long Horn'
29
+
30
+ peter.to_csv.chomp.split(',')[0..3].should == ['44','344','Peter','Long Horn']
31
+ end
32
+
33
+ it "should serialize a collection to CSV" do
34
+ result = @collection.to_csv.gsub(/[[:space:]]+\n/, "\n")
35
+ result.split("\n")[0].split(',')[0..3].should == ['1','2','Betsy','Jersey']
36
+ result.split("\n")[1].split(',')[0..3].should == ['10','20','Berta','Guernsey']
37
+ end
38
+
39
+ it 'should integrate with dm-validations by providing one line per error' do
40
+ planet = Planet.create(:name => 'a')
41
+ result = planet.errors.to_csv.gsub(/[[:space:]]+\n/, "\n").split("\n")
42
+ result.should include("name,#{planet.errors[:name][0]}")
43
+ result.should include("solar_system_id,#{planet.errors[:solar_system_id][0]}")
44
+ result.length.should == 2
45
+ end
46
+
47
+ with_alternate_adapter do
48
+
49
+ describe "multiple repositories" do
50
+ before(:all) do
51
+ [ :default, :alternate ].each do |repository_name|
52
+ DataMapper.repository(repository_name) do
53
+ DataMapper.finalize
54
+ QuanTum::Cat.auto_migrate!
55
+ QuanTum::Cat.destroy!
56
+ end
57
+ end
58
+ end
59
+
60
+ it "should use the repsoitory for the model" do
61
+ gerry = QuanTum::Cat.create(:name => "gerry")
62
+ george = DataMapper.repository(:alternate){ QuanTum::Cat.create(:name => "george", :is_dead => false) }
63
+ gerry.to_csv.should_not match(/false/)
64
+ george.to_csv.should match(/false/)
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+ else
71
+ warn "[WARNING] Cannot require 'faster_csv' or 'csv', not running #to_csv specs"
72
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMapper::Serializer, '#to_json' do
4
+ #
5
+ # ==== ajaxy JSON
6
+ #
7
+
8
+ before(:all) do
9
+ DataMapper.finalize
10
+ DataMapper.auto_migrate!
11
+ query = DataMapper::Query.new(DataMapper::repository(:default), Cow)
12
+
13
+ keys = %w[ id composite name breed ]
14
+
15
+ resources = [
16
+ Hash[ keys.zip([ 1, 2, 'Betsy', 'Jersey' ]) ],
17
+ Hash[ keys.zip([ 10, 20, 'Berta', 'Guernsey' ]) ],
18
+ ]
19
+
20
+ @collection = DataMapper::Collection.new(query, query.model.load(resources, query))
21
+
22
+ @harness = Class.new(SerializerTestHarness) do
23
+ def method_name
24
+ :to_json
25
+ end
26
+
27
+ protected
28
+
29
+ def deserialize(result)
30
+ JSON.parse(result)
31
+ end
32
+ end.new
33
+ end
34
+
35
+ it_should_behave_like "A serialization method"
36
+ it_should_behave_like 'A serialization method that also serializes core classes'
37
+
38
+ it "handles options given to a collection properly" do
39
+ deserialized_collection = JSON.parse(@collection.to_json(:only => [:composite]))
40
+ betsy = deserialized_collection.first
41
+ berta = deserialized_collection.last
42
+
43
+ betsy["id"].should be_nil
44
+ betsy["composite"].should == 2
45
+ betsy["name"].should be_nil
46
+ betsy["breed"].should be_nil
47
+
48
+ berta["id"].should be_nil
49
+ berta["composite"].should == 20
50
+ berta["name"].should be_nil
51
+ berta["breed"].should be_nil
52
+ end
53
+
54
+ it "supports :include option for one level depth"
55
+
56
+ it "supports :include option for more than one level depth"
57
+
58
+ it "has :repository option to override used repository"
59
+
60
+ it "can be serialized within a Hash" do
61
+ hash = { 'cows' => Cow.all }
62
+ JSON.parse(hash.to_json).should == hash
63
+ end
64
+
65
+ end
66
+
67
+ describe DataMapper::Serializer, '#as_json' do
68
+ it "handles nil for options" do
69
+ expect { Cow.new.as_json(nil) }.to_not raise_error
70
+ end
71
+
72
+ it "serializes Discriminator types as strings" do
73
+ Motorcycle.new.as_json[:type].should == "Motorcycle"
74
+ end
75
+ end
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+ require 'rexml/document'
3
+
4
+ [:rexml, :libxml, :nokogiri].each do |lib|
5
+ begin
6
+ DataMapper::Serializer::XML.serializer = lib
7
+ rescue LoadError => e
8
+ warn "[WARNING] #{e.message}"
9
+ warn "[WARNING] Not running #to_xml specs for #{lib}"
10
+ next
11
+ end
12
+
13
+ describe DataMapper::Serializer, "#to_xml using #{lib}" do
14
+ before(:all) do
15
+ DataMapper.finalize
16
+
17
+ @harness = Class.new(SerializerTestHarness) {
18
+ def method_name
19
+ :to_xml
20
+ end
21
+
22
+ protected
23
+
24
+ def deserialize(result)
25
+ f = lambda do |element|
26
+ case element.attributes["type"]
27
+ when "hash"
28
+ element.elements.to_a.inject({}) do |a, e|
29
+ a.update(e.name => f[e])
30
+ end
31
+ when "array"
32
+ element.elements.collect do |e|
33
+ f[e]
34
+ end
35
+ else
36
+ if element.elements.empty?
37
+ cast(element.text, element.attributes["type"])
38
+ else
39
+ element.elements.to_a.inject({}) do |a, e|
40
+ a.update(e.name => f[e])
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ doc = REXML::Document.new(result)
47
+ f[doc.elements[1]]
48
+ end
49
+
50
+ def cast(value, type)
51
+ boolean_conversions = {"true" => true, "false" => false}
52
+ value = boolean_conversions[value] if boolean_conversions.has_key?(value)
53
+ value = value.to_i if value && ["integer", "datamapper::types::serial"].include?(type)
54
+ value
55
+ end
56
+ }.new
57
+ end
58
+
59
+ it_should_behave_like "A serialization method"
60
+
61
+ it "should not include the XML prologue, so that the result can be embedded in other XML documents" do
62
+ planet = Planet.new
63
+ xml = planet.to_xml(:element_name => "aplanet")
64
+ xml.should_not match(/\A<?xml/)
65
+ end
66
+
67
+ describe ':element_name option for Resource' do
68
+ it 'should be used as the root node name by #to_xml' do
69
+ planet = Planet.new
70
+ xml = planet.to_xml(:element_name => "aplanet")
71
+ REXML::Document.new(xml).elements[1].name.should == "aplanet"
72
+ end
73
+
74
+ it 'when not specified the class name underscored and with slashes replaced with dashes should be used as the root node name' do
75
+ cat = QuanTum::Cat.new
76
+ xml = cat.to_xml
77
+ REXML::Document.new(xml).elements[1].name.should == "quan_tum-cat"
78
+ end
79
+ end
80
+
81
+ describe ':collection_element_name for Collection' do
82
+ before(:each) do
83
+ @model = QuanTum::Cat
84
+ @query = DataMapper::Query.new(DataMapper::repository(:default), @model)
85
+ @collection = DataMapper::Collection.new(@query)
86
+ end
87
+
88
+ it 'when not specified the class name tableized and with slashes replaced with dashes should be used as the root node name' do
89
+ xml = DataMapper::Collection.new(@query).to_xml
90
+ REXML::Document.new(xml).elements[1].name.should == "quan_tum-cats"
91
+ end
92
+
93
+ it 'should be used as the root node name by #to_xml' do
94
+ resources = @model.load([ { 'id' => 1 } ], @query)
95
+ @collection = DataMapper::Collection.new(@query, resources)
96
+
97
+ xml = @collection.to_xml(:collection_element_name => "somanycats")
98
+ REXML::Document.new(xml).elements[1].name.should == "somanycats"
99
+ end
100
+
101
+ it 'should respect :element_name for collection elements' do
102
+ resources = @model.load([ { 'id' => 1 } ], @query)
103
+ @collection = DataMapper::Collection.new(@query, resources)
104
+
105
+ xml = @collection.to_xml(:collection_element_name => "somanycats", :element_name => 'cat')
106
+ REXML::Document.new(xml).elements[1].elements[1].name.should == "cat"
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMapper::Serializer, '#to_yaml' do
4
+ #
5
+ # ==== yummy YAML
6
+ #
7
+
8
+ before(:all) do
9
+ DataMapper.finalize
10
+ @harness = Class.new(SerializerTestHarness) do
11
+ def method_name
12
+ :to_yaml
13
+ end
14
+
15
+ def deserialize(result)
16
+ result = YAML.load(result)
17
+ process = lambda {|object|
18
+ if object.is_a?(Array)
19
+ object.collect(&process)
20
+ elsif object.is_a?(Hash)
21
+ object.inject({}) {|a, (key, value)| a.update(key.to_s => process[value]) }
22
+ else
23
+ object
24
+ end
25
+ }
26
+ process[result]
27
+ end
28
+ end.new
29
+
30
+ @ruby_192 = RUBY_VERSION >= '1.9.2'
31
+ @to_yaml = true
32
+ end
33
+
34
+ it_should_behave_like 'A serialization method'
35
+ it_should_behave_like 'A serialization method that also serializes core classes'
36
+
37
+ it 'should allow static YAML dumping' do
38
+ object = Cow.create(
39
+ :id => 89,
40
+ :composite => 34,
41
+ :name => 'Berta',
42
+ :breed => 'Guernsey'
43
+ )
44
+ result = @harness.deserialize(YAML.dump(object))
45
+ result['name'].should == 'Berta'
46
+ end
47
+
48
+ it 'should allow static YAML dumping of a collection' do
49
+ object = Cow.create(
50
+ :id => 89,
51
+ :composite => 34,
52
+ :name => 'Berta',
53
+ :breed => 'Guernsey'
54
+ )
55
+ result = @harness.deserialize(YAML.dump(Cow.all))
56
+ result[0]['name'].should == 'Berta'
57
+ end
58
+
59
+ end
data/spec/rcov.opts ADDED
@@ -0,0 +1,6 @@
1
+ --exclude "spec,^/"
2
+ --sort coverage
3
+ --callsites
4
+ --xrefs
5
+ --profile
6
+ --text-summary
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --loadby random
3
+ --format profile
4
+ --backtrace
@@ -0,0 +1,26 @@
1
+ require 'dm-core/spec/setup'
2
+ require 'dm-core/spec/lib/adapter_helpers'
3
+ require 'dm-core/spec/lib/pending_helpers'
4
+
5
+ require 'dm-validations' # FIXME: must be required before dm-serializer
6
+ require 'dm-serializer'
7
+ require 'dm-migrations'
8
+
9
+ require File.expand_path('spec/lib/serialization_method_shared_spec')
10
+
11
+ # require fixture resources
12
+ SPEC_ROOT = Pathname(__FILE__).dirname.expand_path
13
+ Pathname.glob((SPEC_ROOT + 'fixtures/**/*.rb').to_s).each { |file| require file }
14
+
15
+ class SerializerTestHarness
16
+ def test(object, *args)
17
+ deserialize(object.send(method_name, *args))
18
+ end
19
+ end
20
+
21
+ DataMapper::Spec.setup
22
+
23
+ Spec::Runner.configure do |config|
24
+ config.extend(DataMapper::Spec::Adapters::Helpers)
25
+ config.include(DataMapper::Spec::PendingHelpers)
26
+ end
data/tasks/spec.rake ADDED
@@ -0,0 +1,38 @@
1
+ spec_defaults = lambda do |spec|
2
+ spec.pattern = 'spec/**/*_spec.rb'
3
+ spec.libs << 'lib' << 'spec'
4
+ spec.spec_opts << '--options' << 'spec/spec.opts'
5
+ end
6
+
7
+ begin
8
+ require 'spec/rake/spectask'
9
+
10
+ Spec::Rake::SpecTask.new(:spec, &spec_defaults)
11
+ rescue LoadError
12
+ task :spec do
13
+ abort 'rspec is not available. In order to run spec, you must: gem install rspec'
14
+ end
15
+ end
16
+
17
+ begin
18
+ require 'rcov'
19
+ require 'spec/rake/verify_rcov'
20
+
21
+ Spec::Rake::SpecTask.new(:rcov) do |rcov|
22
+ spec_defaults.call(rcov)
23
+ rcov.rcov = true
24
+ rcov.rcov_opts = File.read('spec/rcov.opts').split(/\s+/)
25
+ end
26
+
27
+ RCov::VerifyTask.new(:verify_rcov => :rcov) do |rcov|
28
+ rcov.threshold = 100
29
+ end
30
+ rescue LoadError
31
+ %w[ rcov verify_rcov ].each do |name|
32
+ task name do
33
+ abort "rcov is not available. In order to run #{name}, you must: gem install rcov"
34
+ end
35
+ end
36
+ end
37
+
38
+ task :default => :spec
data/tasks/yard.rake ADDED
@@ -0,0 +1,9 @@
1
+ begin
2
+ require 'yard'
3
+
4
+ YARD::Rake::YardocTask.new
5
+ rescue LoadError
6
+ task :yard do
7
+ abort 'YARD is not available. In order to run yard, you must: gem install yard'
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'pathname'
3
+ require 'yardstick/rake/measurement'
4
+ require 'yardstick/rake/verify'
5
+
6
+ # yardstick_measure task
7
+ Yardstick::Rake::Measurement.new
8
+
9
+ # verify_measurements task
10
+ Yardstick::Rake::Verify.new do |verify|
11
+ verify.threshold = 100
12
+ end
13
+ rescue LoadError
14
+ %w[ yardstick_measure verify_measurements ].each do |name|
15
+ task name.to_s do
16
+ abort "Yardstick is not available. In order to run #{name}, you must: gem install yardstick"
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ardm-serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Martin Emde
8
+ - Guy van den Berg
9
+ - Dan Kubb
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-01-29 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ardm-core
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.2'
29
+ - !ruby/object:Gem::Dependency
30
+ name: fastercsv
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '1.5'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '1.5'
43
+ - !ruby/object:Gem::Dependency
44
+ name: multi_json
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.0'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '1.0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: json
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '1.6'
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '1.6'
71
+ - !ruby/object:Gem::Dependency
72
+ name: json_pure
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '1.6'
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '1.6'
85
+ - !ruby/object:Gem::Dependency
86
+ name: rake
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '0.9'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: '0.9'
99
+ - !ruby/object:Gem::Dependency
100
+ name: rspec
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: '1.3'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: '1.3'
113
+ description: DataMapper plugin for serializing Resources and Collections
114
+ email:
115
+ - me@martinemde.com
116
+ - dan.kubb@gmail.com
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files:
120
+ - LICENSE
121
+ - README.rdoc
122
+ files:
123
+ - ".gitignore"
124
+ - ".travis.yml"
125
+ - Gemfile
126
+ - LICENSE
127
+ - README.rdoc
128
+ - Rakefile
129
+ - ardm-serializer.gemspec
130
+ - benchmarks/to_json.rb
131
+ - benchmarks/to_xml.rb
132
+ - lib/ardm-serializer.rb
133
+ - lib/dm-serializer.rb
134
+ - lib/dm-serializer/common.rb
135
+ - lib/dm-serializer/to_csv.rb
136
+ - lib/dm-serializer/to_json.rb
137
+ - lib/dm-serializer/to_xml.rb
138
+ - lib/dm-serializer/to_yaml.rb
139
+ - lib/dm-serializer/version.rb
140
+ - lib/dm-serializer/xml.rb
141
+ - lib/dm-serializer/xml/libxml.rb
142
+ - lib/dm-serializer/xml/nokogiri.rb
143
+ - lib/dm-serializer/xml/rexml.rb
144
+ - spec/fixtures/cow.rb
145
+ - spec/fixtures/planet.rb
146
+ - spec/fixtures/quan_tum_cat.rb
147
+ - spec/fixtures/vehicle.rb
148
+ - spec/lib/serialization_method_shared_spec.rb
149
+ - spec/public/serializer_spec.rb
150
+ - spec/public/to_csv_spec.rb
151
+ - spec/public/to_json_spec.rb
152
+ - spec/public/to_xml_spec.rb
153
+ - spec/public/to_yaml_spec.rb
154
+ - spec/rcov.opts
155
+ - spec/spec.opts
156
+ - spec/spec_helper.rb
157
+ - tasks/spec.rake
158
+ - tasks/yard.rake
159
+ - tasks/yardstick.rake
160
+ homepage: https://github.com/ar-dm/ardm-serializer
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.2.2
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Ardm fork of dm-serializer
184
+ test_files: []
185
+ has_rdoc: