dm-serializer 0.9.11 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,4 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
1
+ require 'spec_helper'
3
2
 
4
3
  describe DataMapper::Serialize, '#to_json' do
5
4
  #
@@ -10,10 +9,14 @@ describe DataMapper::Serialize, '#to_json' do
10
9
  DataMapper.auto_migrate!
11
10
  query = DataMapper::Query.new(DataMapper::repository(:default), Cow)
12
11
 
13
- @collection = DataMapper::Collection.new(query) do |c|
14
- c.load([1, 2, 'Betsy', 'Jersey'])
15
- c.load([10, 20, 'Berta', 'Guernsey'])
16
- end
12
+ keys = %w[ id composite name breed ]
13
+
14
+ resources = [
15
+ keys.zip([ 1, 2, 'Betsy', 'Jersey' ]).to_hash,
16
+ keys.zip([ 10, 20, 'Berta', 'Guernsey' ]).to_hash,
17
+ ]
18
+
19
+ @collection = DataMapper::Collection.new(query, query.model.load(resources, query))
17
20
 
18
21
  @harness = Class.new(SerializerTestHarness) do
19
22
  def method_name
@@ -1,5 +1,4 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
1
+ require 'spec_helper'
3
2
 
4
3
  {
5
4
  'REXML' => nil,
@@ -27,29 +26,35 @@ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
27
26
  protected
28
27
 
29
28
  def deserialize(result)
30
- doc = REXML::Document.new(result)
31
- root = doc.elements[1]
32
- if root.attributes["type"] == "array"
33
- root.elements.collect do |element|
34
- a = {}
35
- element.elements.each do |v|
36
- a.update(v.name => cast(v.text, v.attributes["type"]))
29
+ f = lambda do |element|
30
+ case element.attributes["type"]
31
+ when "hash"
32
+ element.elements.to_a.inject({}) do |a, e|
33
+ a.update(e.name => f[e])
34
+ end
35
+ when "array"
36
+ element.elements.collect do |e|
37
+ f[e]
38
+ end
39
+ else
40
+ if element.elements.empty?
41
+ cast(element.text, element.attributes["type"])
42
+ else
43
+ element.elements.to_a.inject({}) do |a, e|
44
+ a.update(e.name => f[e])
45
+ end
37
46
  end
38
- a
39
- end
40
- else
41
- a = {}
42
- root.elements.each do |v|
43
- a.update(v.name => cast(v.text, v.attributes["type"]))
44
47
  end
45
- a
46
48
  end
49
+
50
+ doc = REXML::Document.new(result)
51
+ f[doc.elements[1]]
47
52
  end
48
53
 
49
54
  def cast(value, type)
50
55
  boolean_conversions = {"true" => true, "false" => false}
51
56
  value = boolean_conversions[value] if boolean_conversions.has_key?(value)
52
- value = value.to_i if value && type == "integer"
57
+ value = value.to_i if value && ["integer", "datamapper::types::serial"].include?(type)
53
58
  value
54
59
  end
55
60
  end.new
@@ -62,7 +67,7 @@ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
62
67
  it "should not include the XML prologue, so that the result can be embedded in other XML documents" do
63
68
  planet = Planet.new
64
69
  xml = planet.to_xml(:element_name => "aplanet")
65
- xml.should_not =~ /\A<\?xml/
70
+ xml.should_not match(/\A<?xml/)
66
71
  end
67
72
 
68
73
  describe ':element_name option for Resource' do
@@ -81,24 +86,27 @@ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
81
86
 
82
87
  describe ':collection_element_name for Collection' do
83
88
  before(:each) do
84
- query = DataMapper::Query.new(DataMapper::repository(:default), QuanTum::Cat)
85
- @collection = DataMapper::Collection.new(query) {}
89
+ @model = QuanTum::Cat
90
+ @query = DataMapper::Query.new(DataMapper::repository(:default), @model)
91
+ @collection = DataMapper::Collection.new(@query)
86
92
  end
87
93
 
88
94
  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 = @collection.to_xml
95
+ xml = DataMapper::Collection.new(@query).to_xml
90
96
  REXML::Document.new(xml).elements[1].name.should == "quan_tum-cats"
91
97
  end
92
98
 
93
99
  it 'should be used as the root node name by #to_xml' do
94
- @collection.load([1])
100
+ resources = @model.load([ { 'id' => 1 } ], @query)
101
+ @collection = DataMapper::Collection.new(@query, resources)
95
102
 
96
103
  xml = @collection.to_xml(:collection_element_name => "somanycats")
97
104
  REXML::Document.new(xml).elements[1].name.should == "somanycats"
98
105
  end
99
106
 
100
107
  it 'should respect :element_name for collection elements' do
101
- @collection.load([1])
108
+ resources = @model.load([ { 'id' => 1 } ], @query)
109
+ @collection = DataMapper::Collection.new(@query, resources)
102
110
 
103
111
  xml = @collection.to_xml(:collection_element_name => "somanycats", :element_name => 'cat')
104
112
  REXML::Document.new(xml).elements[1].elements[1].name.should == "cat"
@@ -1,6 +1,4 @@
1
- require 'pathname'
2
- require 'yaml'
3
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
1
+ require 'spec_helper'
4
2
 
5
3
  describe DataMapper::Serialize, '#to_yaml' do
6
4
  #
@@ -13,20 +11,18 @@ describe DataMapper::Serialize, '#to_yaml' do
13
11
  :to_yaml
14
12
  end
15
13
 
16
- protected
17
-
18
14
  def deserialize(result)
19
- stringify_keys = lambda {|hash| hash.inject({}) {|a, (key, value)| a.update(key.to_s => value) }}
20
15
  result = YAML.load(result)
21
- (process = lambda {|object|
16
+ process = lambda {|object|
22
17
  if object.is_a?(Array)
23
18
  object.collect(&process)
24
19
  elsif object.is_a?(Hash)
25
- stringify_keys[object]
20
+ object.inject({}) {|a, (key, value)| a.update(key.to_s => process[value]) }
26
21
  else
27
22
  object
28
23
  end
29
- })[result]
24
+ }
25
+ process[result]
30
26
  end
31
27
  end.new
32
28
  end
@@ -34,4 +30,26 @@ describe DataMapper::Serialize, '#to_yaml' do
34
30
  it_should_behave_like 'A serialization method'
35
31
  it_should_behave_like 'A serialization method that also serializes core classes'
36
32
 
33
+ it 'should allow static YAML dumping' do
34
+ object = Cow.create(
35
+ :id => 89,
36
+ :composite => 34,
37
+ :name => 'Berta',
38
+ :breed => 'Guernsey'
39
+ )
40
+ result = @harness.deserialize(YAML.dump(object))
41
+ result['name'].should == 'Berta'
42
+ end
43
+
44
+ it 'should allow static YAML dumping of a collection' do
45
+ object = Cow.create(
46
+ :id => 89,
47
+ :composite => 34,
48
+ :name => 'Berta',
49
+ :breed => 'Guernsey'
50
+ )
51
+ result = @harness.deserialize(YAML.dump(Cow.all))
52
+ result[0]['name'].should == 'Berta'
53
+ end
54
+
37
55
  end
@@ -1 +1,2 @@
1
1
  --colour
2
+ --loadby random
@@ -1,11 +1,18 @@
1
- require 'pathname'
2
1
  require 'rubygems'
3
2
 
4
- gem 'dm-core', '0.9.11'
3
+ # use local dm-core if running from a typical dev checkout.
4
+ lib = File.join('..', '..', 'dm-core', 'lib')
5
+ $LOAD_PATH.unshift(lib) if File.directory?(lib)
5
6
  require 'dm-core'
6
7
 
7
- spec_dir_path = Pathname(__FILE__).dirname.expand_path
8
- $LOAD_PATH.unshift(spec_dir_path.parent + 'lib/')
8
+ # use local dm-validations if running from dm-more directly
9
+ lib = File.join('..', 'dm-validations', 'lib')
10
+ $LOAD_PATH.unshift(lib) if File.directory?(lib)
11
+ require 'dm-validations'
12
+
13
+ # Support running specs with 'rake spec' and 'spec'
14
+ $LOAD_PATH.unshift('lib') unless $LOAD_PATH.include?('lib')
15
+
9
16
  require 'dm-serializer'
10
17
 
11
18
  def load_driver(name, default_uri)
@@ -34,9 +41,9 @@ class SerializerTestHarness
34
41
  end
35
42
  end
36
43
 
37
- require spec_dir_path + 'lib/serialization_method_shared_spec'
44
+ require 'spec/lib/serialization_method_shared_spec'
38
45
 
39
46
  # require fixture resources
40
- Dir[(spec_dir_path + "fixtures/*.rb").to_s].each do |fixture_file|
47
+ Dir.glob("spec/fixtures/*.rb").each do |fixture_file|
41
48
  require fixture_file
42
49
  end
@@ -4,7 +4,7 @@ end
4
4
 
5
5
  desc "Install #{GEM_NAME} #{GEM_VERSION}"
6
6
  task :install => [ :package ] do
7
- sudo_gem "install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources"
7
+ sudo_gem "install pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources"
8
8
  end
9
9
 
10
10
  desc "Uninstall #{GEM_NAME} #{GEM_VERSION}"
@@ -1,6 +1,4 @@
1
1
  begin
2
- gem 'rspec', '~>1.2'
3
- require 'spec'
4
2
  require 'spec/rake/spectask'
5
3
 
6
4
  task :default => [ :spec ]
@@ -8,16 +6,18 @@ begin
8
6
  desc 'Run specifications'
9
7
  Spec::Rake::SpecTask.new(:spec) do |t|
10
8
  t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
11
- t.spec_files = Pathname.glob((ROOT + 'spec/**/*_spec.rb').to_s).map { |f| f.to_s }
9
+ t.libs << 'lib' << 'spec' # needed for CI rake spec task, duplicated in spec_helper
12
10
 
13
11
  begin
14
- gem 'rcov', '~>0.8'
12
+ require 'rcov'
15
13
  t.rcov = JRUBY ? false : (ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true)
16
14
  t.rcov_opts << '--exclude' << 'spec'
17
15
  t.rcov_opts << '--text-summary'
18
16
  t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
19
17
  rescue LoadError
20
18
  # rcov not installed
19
+ rescue SyntaxError
20
+ # rcov syntax invalid
21
21
  end
22
22
  end
23
23
  rescue LoadError
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.11
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guy van den Berg
@@ -9,19 +9,10 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-29 00:00:00 -07:00
12
+ date: 2009-09-16 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: dm-core
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - "="
22
- - !ruby/object:Gem::Version
23
- version: 0.9.11
24
- version:
14
+ dependencies: []
15
+
25
16
  description: DataMapper plugin for serializing DataMapper objects
26
17
  email:
27
18
  - vandenberg.guy [a] gmail [d] com
@@ -30,19 +21,20 @@ executables: []
30
21
  extensions: []
31
22
 
32
23
  extra_rdoc_files:
33
- - README.textile
24
+ - README.rdoc
34
25
  - LICENSE
35
26
  - TODO
36
- - History.txt
27
+ - History.rdoc
37
28
  files:
38
- - History.txt
29
+ - History.rdoc
39
30
  - LICENSE
40
31
  - Manifest.txt
41
- - README.textile
32
+ - README.rdoc
42
33
  - Rakefile
43
34
  - TODO
44
35
  - autotest/discover.rb
45
36
  - autotest/dmserializer_rspec.rb
37
+ - benchmarks/to_json.rb
46
38
  - benchmarks/to_xml.rb
47
39
  - lib/dm-serializer.rb
48
40
  - lib/dm-serializer/common.rb
@@ -69,11 +61,13 @@ files:
69
61
  - tasks/install.rb
70
62
  - tasks/spec.rb
71
63
  has_rdoc: true
72
- homepage: http://github.com/sam/dm-more/tree/master/dm-serializer
64
+ homepage: http://github.com/datamapper/dm-more/tree/master/dm-serializer
65
+ licenses: []
66
+
73
67
  post_install_message:
74
68
  rdoc_options:
75
69
  - --main
76
- - README.txt
70
+ - README.rdoc
77
71
  require_paths:
78
72
  - lib
79
73
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -91,9 +85,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
85
  requirements: []
92
86
 
93
87
  rubyforge_project: datamapper
94
- rubygems_version: 1.3.1
88
+ rubygems_version: 1.3.5
95
89
  signing_key:
96
- specification_version: 2
90
+ specification_version: 3
97
91
  summary: DataMapper plugin for serializing DataMapper objects
98
92
  test_files: []
99
93
 
@@ -1,61 +0,0 @@
1
- h1. dm-serializer
2
-
3
- h2. Overview
4
-
5
- dm-serializer allows DataMapper models and collections to be serialized to a variety of formats (currently JSON, XML, YAML and CSV).
6
-
7
- h2. How it works
8
-
9
- One method is added to each model/collection for each serialization type - @to_json@, @to_xml@, @to_yaml@, and @to_csv@. With the exception of @to_csv@, all of these methods share the same interface. @to_json@ will be used for examples. Any method specific behaviour is documented in its own section below.
10
-
11
- <pre><code>require 'dm-serializer'
12
-
13
- class Cow
14
- include DataMapper::Resource
15
-
16
- property :id, Integer, :key => true
17
- property :name, String
18
-
19
- def description
20
- "A Cow"
21
- end
22
- end
23
-
24
- cow = Cow.create(
25
- :id => 1,
26
- :name => "Berta"
27
- )
28
-
29
- cow.to_json # => { "id": 1, "name": "Berta" }
30
- cow.to_json(:only => [:name]) # => { "name": "Berta" }
31
- cow.to_json(:exclude => [:id]) # => { "name": "Berta" }
32
- cow.to_json(:methods => [:desc]) # => { "id": 1, "name": "Berta", "desc": "A Cow" }</code></pre>
33
-
34
- You can include associations by passing the association accessor the @:methods@ option.
35
-
36
- If you want to only load a particular serialization method, that's cool, you can do that:
37
-
38
- <pre><code>require 'dm-serializer/to_json'</code></pre>
39
-
40
- h2. to_xml
41
-
42
- @to_xml@ supports some extra options to allow you to override the element names
43
-
44
- <pre><code>cow.to_xml( :element_name => 'bovine') # => <bovine><id>1</id><name>Berta</name></bovine>
45
- cows.to_xml(:collection_element_name => 'kine') # => <kine><bovine><id>1</id><name>Berta</name></bovine></kine></code></pre>
46
-
47
- If you would like a nice speed boost (~5x), require @libxml@ or @nokogiri@ before @dm-serializer@, and that library will be used rather than REXML.
48
-
49
- h2. to_csv
50
-
51
- @to_csv@ currently doesn't support any options yet. It will in the future. It will not support serializing child associations.
52
-
53
- h2. Arrays, Hashes, and other core classes
54
-
55
- @dm-serializer@ only adds serialization methods to DataMapper objects and collections, however some libraries used (json, yaml) add methods to core classes, such as @Array@. Note that passing @dm-serializer@ options (such as @:only@) to these methods is *not supported*.
56
-
57
- <pre><code>Cow.all.to_a.to_yaml(:only => 'name') # WILL NOT WORK</code></pre>
58
-
59
- h2. Beware
60
-
61
- If you go spelunking through the code you will find other undocumented options. Use at your own risk, I plan on removing or changing these in the near future.