dm-serializer 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1 @@
1
+
data/Manifest.txt ADDED
@@ -0,0 +1,20 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ TODO
7
+ autotest/discover.rb
8
+ autotest/dmserializer_rspec.rb
9
+ lib/dm-serializer.rb
10
+ lib/dm-serializer/version.rb
11
+ spec/fixtures/cow.rb
12
+ spec/fixtures/planet.rb
13
+ spec/fixtures/quatum_cat.rb
14
+ spec/spec.opts
15
+ spec/spec_helper.rb
16
+ spec/unit/serializer_spec.rb
17
+ spec/unit/to_csv_spec.rb
18
+ spec/unit/to_json_spec.rb
19
+ spec/unit/to_xml_spec.rb
20
+ spec/unit/to_yaml_spec.rb
@@ -1,4 +1,3 @@
1
- dm-serializer
2
- =============
1
+ = dm-serializer
3
2
 
4
3
  DataMapper plugin for serializing DataMapper objects.
data/Rakefile CHANGED
@@ -1,51 +1,44 @@
1
1
  require 'rubygems'
2
2
  require 'spec'
3
- require 'rake/clean'
4
- require 'rake/gempackagetask'
5
3
  require 'spec/rake/spectask'
6
4
  require 'pathname'
7
5
 
8
- CLEAN.include '{log,pkg}/'
6
+ ROOT = Pathname(__FILE__).dirname.expand_path
7
+ require ROOT + 'lib/dm-serializer/version'
9
8
 
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
9
+ AUTHOR = "Guy van den Berg"
10
+ EMAIL = "vandenberg.guy@gmail.com"
11
+ GEM_NAME = "dm-serializer"
12
+ GEM_VERSION = DataMapper::Serializer::VERSION
13
+ GEM_DEPENDENCIES = [["dm-core", GEM_VERSION]]
14
+ GEM_CLEAN = ["log", "pkg", "coverage"]
15
+ GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.txt LICENSE TODO ] }
16
+
17
+ PROJECT_NAME = "datamapper"
18
+ PROJECT_URL = "http://github.com/sam/dm-more/tree/master/dm-serializer"
19
+ PROJECT_DESCRIPTION = PROJECT_SUMMARY = "DataMapper plugin for serializing DataMapper objects"
20
+
21
+ require ROOT.parent + 'tasks/hoe'
25
22
 
26
23
  task :default => [ :spec ]
27
24
 
28
25
  WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
29
26
  SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
30
27
 
31
- Rake::GemPackageTask.new(spec) do |pkg|
32
- pkg.gem_spec = spec
33
- end
34
-
35
- desc "Install #{spec.name} #{spec.version} (default ruby)"
28
+ desc "Install #{GEM_NAME} #{GEM_VERSION} (default ruby)"
36
29
  task :install => [ :package ] do
37
- sh "#{SUDO} gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources", :verbose => false
30
+ sh "#{SUDO} gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources", :verbose => false
38
31
  end
39
32
 
40
- desc "Uninstall #{spec.name} #{spec.version} (default ruby)"
33
+ desc "Uninstall #{GEM_NAME} #{GEM_VERSION} (default ruby)"
41
34
  task :uninstall => [ :clobber ] do
42
- sh "#{SUDO} gem uninstall #{spec.name} -v#{spec.version} -I -x", :verbose => false
35
+ sh "#{SUDO} gem uninstall #{GEM_NAME} -v#{GEM_VERSION} -I -x", :verbose => false
43
36
  end
44
37
 
45
38
  namespace :jruby do
46
- desc "Install #{spec.name} #{spec.version} with JRuby"
39
+ desc "Install #{GEM_NAME} #{GEM_VERSION} with JRuby"
47
40
  task :install => [ :package ] do
48
- sh %{#{SUDO} jruby -S gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources}, :verbose => false
41
+ sh %{#{SUDO} jruby -S gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources}, :verbose => false
49
42
  end
50
43
  end
51
44
 
@@ -0,0 +1,3 @@
1
+ Autotest.add_discovery do
2
+ "dmserializer"
3
+ end
@@ -0,0 +1,108 @@
1
+ require 'autotest'
2
+
3
+ $VERBOSE = false
4
+
5
+ class RspecCommandError < StandardError; end
6
+
7
+ # Autotest has no full-blown snake_case to CamelCase
8
+ class Autotest::DmserializerRspec < Autotest
9
+ WHOLE_SUITE_REGEXP = %r{^spec/(unit|integration)/.*_spec\.rb}
10
+
11
+ Autotest.add_hook :initialize do |at|
12
+ at.clear_mappings
13
+ at.add_exception(/\.git|TAGS/)
14
+
15
+ # Updating a spec runs that spec.
16
+ at.add_mapping(%r{^spec/.*_spec\.rb$}) do |filename, _|
17
+ filename
18
+ end
19
+
20
+ # Updating spec helper runs the whole suite.
21
+ at.add_mapping(%r{^spec/spec_helper\.rb}) do |_, m|
22
+ at.files_matching WHOLE_SUITE_REGEXP
23
+ end
24
+
25
+ # Updating of library file runs the whole suite.
26
+ at.add_mapping(%r{^lib/.*\.rb}) do |_, m|
27
+ at.files_matching WHOLE_SUITE_REGEXP
28
+ end
29
+
30
+ # Updating of fixture resources runs the whole suite.
31
+ at.add_mapping(%r{^spec/fixtures/.*\.rb}) do |_, m|
32
+ at.files_matching WHOLE_SUITE_REGEXP
33
+ end
34
+ end
35
+
36
+
37
+ def initialize(kernel = Kernel, separator = File::SEPARATOR, alt_separator = File::ALT_SEPARATOR) # :nodoc:
38
+ super() # need parens so that Ruby doesn't pass our args
39
+ # to the superclass version which takes none..
40
+
41
+ @kernel, @separator, @alt_separator = kernel, separator, alt_separator
42
+ @spec_command = spec_command
43
+ end
44
+
45
+ attr_accessor :failures
46
+
47
+ def failed_results(results)
48
+ results.scan(/^\d+\)\n(?:\e\[\d*m)?(?:.*?Error in )?'([^\n]*)'(?: FAILED)?(?:\e\[\d*m)?\n(.*?)\n\n/m)
49
+ end
50
+
51
+ def handle_results(results)
52
+ @failures = failed_results(results)
53
+ @files_to_test = consolidate_failures @failures
54
+ unless $TESTING
55
+ if @files_to_test.empty?
56
+ hook :green
57
+ else
58
+ hook :red
59
+ end
60
+ end
61
+ @tainted = true unless @files_to_test.empty?
62
+ end
63
+
64
+ def consolidate_failures(failed)
65
+ filters = Hash.new { |h,k| h[k] = [] }
66
+ failed.each do |spec, failed_trace|
67
+ find_files.keys.select { |f| f =~ /spec\// }.each do |f|
68
+ if failed_trace =~ Regexp.new(f)
69
+ filters[f] << spec
70
+ break
71
+ end
72
+ end
73
+ end
74
+ filters
75
+ end
76
+
77
+ def make_test_cmd(files_to_test)
78
+ "#{ruby} -S #{@spec_command} #{test_cmd_options} #{files_to_test.keys.flatten.join(' ')}"
79
+ end
80
+
81
+ def test_cmd_options
82
+ # '-O specs/spec.opts' if File.exist?('specs/spec.opts')
83
+ end
84
+
85
+ # Finds the proper spec command to use. Precendence is set in the
86
+ # lazily-evaluated method spec_commands. Alias + Override that in
87
+ # ~/.autotest to provide a different spec command then the default
88
+ # paths provided.
89
+ def spec_command(separator=File::ALT_SEPARATOR)
90
+ unless defined?(@spec_command)
91
+ @spec_command = spec_commands.find { |cmd| File.exists?(cmd) }
92
+
93
+ raise RspecCommandError, "No spec command could be found" unless @spec_command
94
+
95
+ @spec_command.gsub!(File::SEPARATOR, separator) if separator
96
+ end
97
+ @spec_command
98
+ end
99
+
100
+ # Autotest will look for spec commands in the following
101
+ # locations, in this order:
102
+ #
103
+ # * default spec bin/loader installed in Rubygems
104
+ # * any spec command found in PATH
105
+ def spec_commands
106
+ [File.join(Config::CONFIG['bindir'], 'spec'), 'spec']
107
+ end
108
+ end
@@ -0,0 +1,5 @@
1
+ module DataMapper
2
+ module Serializer
3
+ VERSION = "0.9.3"
4
+ end
5
+ end
data/lib/dm-serializer.rb CHANGED
@@ -23,20 +23,22 @@ module DataMapper
23
23
  fields = []
24
24
 
25
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] || []
26
+ only_properties = Array(options[:only])
27
+ excluded_properties = Array(options[:exclude])
28
28
  exclude_read_only = options[:without_read_only_attributes] || false
29
29
 
30
- propset = self.class.properties(repository.name)
30
+ propset = self.class.properties(repository.name).reject do |p|
31
+ next if only_properties.include? p.name
32
+ excluded_properties.include?(p.name) || !(only_properties.empty? || only_properties.include?(p.name))
33
+ end
31
34
 
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)
35
+ fields += propset.map do |property|
36
+ "#{property.name.to_json}: #{send(property.getter).to_json}"
37
+ end
38
+
39
+ if self.respond_to?(:serialize_properties)
40
+ self.serialize_properties.each do |k,v|
41
+ fields << "#{k.to_json}: #{v.to_json}"
40
42
  end
41
43
  end
42
44
 
@@ -75,6 +77,7 @@ module DataMapper
75
77
  #
76
78
  # @return <REXML::Document> an XML representation of this Resource
77
79
  def to_xml(opts = {})
80
+
78
81
  to_xml_document(opts).to_s
79
82
  end
80
83
 
@@ -108,8 +111,8 @@ module DataMapper
108
111
  # Return a REXML::Document representing this Resource
109
112
  #
110
113
  # @return <REXML::Document> an XML representation of this Resource
111
- def to_xml_document(opts={})
112
- doc = REXML::Document.new
114
+ def to_xml_document(opts={}, doc=nil)
115
+ doc ||= REXML::Document.new
113
116
  root = doc.add_element(xml_element_name)
114
117
 
115
118
  #TODO old code base was converting single quote to double quote on attribs
@@ -117,7 +120,7 @@ module DataMapper
117
120
  self.class.properties(repository.name).each do |property|
118
121
  value = send(property.name)
119
122
  node = root.add_element(property.name.to_s)
120
- if property.key?
123
+ unless property.type == String
121
124
  node.attributes["type"] = property.type.to_s.downcase
122
125
  end
123
126
  node << REXML::Text.new(value.to_s) unless value.nil?
@@ -140,12 +143,8 @@ module DataMapper
140
143
  "[" << map {|e| e.to_json(opts)}.join(",") << "]"
141
144
  end
142
145
 
143
- def to_xml
144
- result = ""
145
- each do |item|
146
- result << item.to_xml + "\n"
147
- end
148
- result
146
+ def to_xml(opts = {})
147
+ to_xml_document(opts).to_s
149
148
  end
150
149
 
151
150
  def to_csv
@@ -155,6 +154,21 @@ module DataMapper
155
154
  end
156
155
  result
157
156
  end
157
+
158
+ protected
159
+ def xml_element_name
160
+ Extlib::Inflection.tableize(self.model.to_s)
161
+ end
162
+
163
+ def to_xml_document(opts={})
164
+ doc = REXML::Document.new
165
+ root = doc.add_element(xml_element_name)
166
+ root.attributes["type"] = 'array'
167
+ each do |item|
168
+ item.send(:to_xml_document, opts, root)
169
+ end
170
+ doc
171
+ end
158
172
  end
159
173
 
160
174
  end # module DataMapper
data/spec/fixtures/cow.rb CHANGED
@@ -5,4 +5,8 @@ class Cow
5
5
  property :composite, Integer, :key => true
6
6
  property :name, String
7
7
  property :breed, String
8
+
9
+ def serialize_properties
10
+ {:extra => "Extra", :another => 42}
11
+ end
8
12
  end
@@ -0,0 +1,11 @@
1
+ class QuantumCat
2
+ include DataMapper::Resource
3
+
4
+ property :id, Serial
5
+ property :name, String
6
+ property :location, String
7
+
8
+ repository(:alternate) do
9
+ property :is_dead, Boolean
10
+ end
11
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'pathname'
3
3
 
4
- gem 'dm-core', '=0.9.2'
4
+ gem 'dm-core', '=0.9.3'
5
5
  require 'dm-core'
6
6
 
7
7
  spec_dir_path = Pathname(__FILE__).dirname.expand_path
@@ -13,10 +13,11 @@ def load_driver(name, default_uri)
13
13
  lib = "do_#{name}"
14
14
 
15
15
  begin
16
- gem lib, '=0.9.2'
16
+ gem lib, '=0.9.3'
17
17
  require lib
18
18
  DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
19
19
  DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
20
+ DataMapper::Repository.adapters[:alternate] = DataMapper::Repository.adapters[name]
20
21
  true
21
22
  rescue Gem::LoadError => e
22
23
  warn "Could not load #{lib}: #{e}"
@@ -31,4 +31,18 @@ describe DataMapper::Serialize, '#to_csv' do
31
31
  "1,2,Betsy,Jersey\n" +
32
32
  "10,20,Berta,Guernsey\n"
33
33
  end
34
+
35
+ describe "multiple repositories" do
36
+ before(:all) do
37
+ QuantumCat.auto_migrate!
38
+ repository(:alternate){QuantumCat.auto_migrate!}
39
+ end
40
+
41
+ it "should use the repsoitory for the model" do
42
+ gerry = QuantumCat.create(:name => "gerry")
43
+ george = repository(:alternate){QuantumCat.create(:name => "george", :is_dead => false)}
44
+ gerry.to_csv.should_not match(/false/)
45
+ george.to_csv.should match(/false/)
46
+ end
47
+ end
34
48
  end
@@ -51,6 +51,13 @@ describe DataMapper::Serialize, '#to_json' do
51
51
  berta["breed"].should == "Guernsey"
52
52
  end
53
53
 
54
+ it "handles extra properties" do
55
+ deserialized_hash = JSON.parse(Cow.new(:id => 1, :name => "Harry", :breed => "Angus").to_json)
56
+
57
+ deserialized_hash["extra"].should == "Extra"
58
+ deserialized_hash["another"].should == 42
59
+ end
60
+
54
61
  it "handles empty collections just fine" do
55
62
  deserialized_collection = JSON.parse(@empty_collection.to_json)
56
63
  deserialized_collection.should be_empty
@@ -99,7 +106,21 @@ describe DataMapper::Serialize, '#to_json' do
99
106
  deserialized_hash["name"].should be(nil)
100
107
  deserialized_hash["aphelion"].should == 249_209_300.4
101
108
  end
102
-
109
+
110
+ describe "multiple repositories" do
111
+ before(:all) do
112
+ QuantumCat.auto_migrate!
113
+ repository(:alternate){QuantumCat.auto_migrate!}
114
+ end
115
+
116
+ it "should use the repsoitory for the model" do
117
+ gerry = QuantumCat.create(:name => "gerry")
118
+ george = repository(:alternate){QuantumCat.create(:name => "george", :is_dead => false)}
119
+ gerry.to_json.should_not match(/is_dead/)
120
+ george.to_json.should match(/is_dead/)
121
+ end
122
+ end
123
+
103
124
  it "supports :include option for one level depth"
104
125
 
105
126
  it "supports :include option for more than one level depth"
@@ -8,6 +8,9 @@ describe DataMapper::Serialize, '#to_xml' do
8
8
 
9
9
  before(:all) do
10
10
  query = DataMapper::Query.new(DataMapper::repository(:default), Cow)
11
+
12
+ @time = DateTime.now
13
+
11
14
 
12
15
  @collection = DataMapper::Collection.new(query) do |c|
13
16
  c.load([1, 2, 'Betsy', 'Jersey'])
@@ -35,8 +38,36 @@ describe DataMapper::Serialize, '#to_xml' do
35
38
  end
36
39
 
37
40
  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
+ @collection.to_xml.should == <<-EOS.compress_lines(false)
42
+ <cows type='array'>
43
+ <cow>
44
+ <id type='integer'>1</id>
45
+ <composite type='integer'>2</composite>
46
+ <name>Betsy</name>
47
+ <breed>Jersey</breed>
48
+ </cow>
49
+ <cow>
50
+ <id type='integer'>10</id>
51
+ <composite type='integer'>20</composite>
52
+ <name>Berta</name>
53
+ <breed>Guernsey</breed>
54
+ </cow>
55
+ </cows>
56
+ EOS
41
57
  end
58
+
59
+ describe "multiple repositories" do
60
+ before(:all) do
61
+ QuantumCat.auto_migrate!
62
+ repository(:alternate){QuantumCat.auto_migrate!}
63
+ end
64
+
65
+ it "should use the repsoitory for the model" do
66
+ gerry = QuantumCat.create(:name => "gerry")
67
+ george = repository(:alternate){QuantumCat.create(:name => "george", :is_dead => false)}
68
+ gerry.to_xml.should_not match(/is_dead/)
69
+ george.to_xml.should match(/is_dead/)
70
+ end
71
+ end
72
+
42
73
  end
@@ -58,5 +58,19 @@ describe DataMapper::Serialize, '#to_yaml' do
58
58
  it "handles empty collections just fine" do
59
59
  YAML.load(@empty_collection.to_yaml).should be_empty
60
60
  end
61
+
62
+ describe "multiple repositories" do
63
+ before(:all) do
64
+ QuantumCat.auto_migrate!
65
+ repository(:alternate){QuantumCat.auto_migrate!}
66
+ end
67
+
68
+ it "should use the repsoitory for the model" do
69
+ gerry = QuantumCat.create(:name => "gerry")
70
+ george = repository(:alternate){QuantumCat.create(:name => "george", :is_dead => false)}
71
+ gerry.to_yaml.should_not match(/is_dead/)
72
+ george.to_yaml.should match(/is_dead/)
73
+ end
74
+ end
61
75
 
62
76
  end
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.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guy van den Berg
@@ -9,48 +9,67 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-25 00:00:00 -05:00
12
+ date: 2008-07-24 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: dm-core
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
20
21
  - - "="
21
22
  - !ruby/object:Gem::Version
22
- version: 0.9.2
23
+ version: 0.9.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.0
23
34
  version:
24
35
  description: DataMapper plugin for serializing DataMapper objects
25
- email: vandenberg.guy@gmail.com
36
+ email:
37
+ - vandenberg.guy@gmail.com
26
38
  executables: []
27
39
 
28
40
  extensions: []
29
41
 
30
42
  extra_rdoc_files:
31
- - README
43
+ - README.txt
32
44
  - LICENSE
33
45
  - TODO
34
46
  files:
47
+ - History.txt
48
+ - LICENSE
49
+ - Manifest.txt
50
+ - README.txt
51
+ - Rakefile
52
+ - TODO
53
+ - autotest/discover.rb
54
+ - autotest/dmserializer_rspec.rb
35
55
  - lib/dm-serializer.rb
56
+ - lib/dm-serializer/version.rb
36
57
  - spec/fixtures/cow.rb
37
58
  - spec/fixtures/planet.rb
59
+ - spec/fixtures/quatum_cat.rb
60
+ - spec/spec.opts
38
61
  - spec/spec_helper.rb
39
62
  - spec/unit/serializer_spec.rb
40
63
  - spec/unit/to_csv_spec.rb
41
64
  - spec/unit/to_json_spec.rb
42
65
  - spec/unit/to_xml_spec.rb
43
66
  - spec/unit/to_yaml_spec.rb
44
- - spec/spec.opts
45
- - Rakefile
46
- - README
47
- - LICENSE
48
- - TODO
49
67
  has_rdoc: true
50
68
  homepage: http://github.com/sam/dm-more/tree/master/dm-serializer
51
69
  post_install_message:
52
- rdoc_options: []
53
-
70
+ rdoc_options:
71
+ - --main
72
+ - README.txt
54
73
  require_paths:
55
74
  - lib
56
75
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -67,8 +86,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
86
  version:
68
87
  requirements: []
69
88
 
70
- rubyforge_project:
71
- rubygems_version: 1.0.1
89
+ rubyforge_project: datamapper
90
+ rubygems_version: 1.2.0
72
91
  signing_key:
73
92
  specification_version: 2
74
93
  summary: DataMapper plugin for serializing DataMapper objects