ardm-serializer 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b9a3b759e035df936b3dedfd92358708957ac7e8
4
+ data.tar.gz: dff5952407b0df0c25c9652d56af3de02e7e049c
5
+ SHA512:
6
+ metadata.gz: 5679f89c1bcae61186706075dea152605ffdfdb71a285ec5bf82c19a10f7bdea8ca8ba29d06960145a96274b329fbb51369dec99014b11f1cb9484da55e77439
7
+ data.tar.gz: f513b39338c5da07f3d070e5cab7a9b5b459f81d02122f3fef834e926b335611ad63eaae3ef6f14b820169be65e02f387afa6c1f697292f3982c96f81fe77178
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## Rubinius
17
+ *.rbc
18
+
19
+ ## PROJECT::GENERAL
20
+ *.gem
21
+ coverage
22
+ rdoc
23
+ pkg
24
+ tmp
25
+ doc
26
+ log
27
+ .yardoc
28
+ measurements
29
+
30
+ ## BUNDLER
31
+ .bundle
32
+ Gemfile.*
33
+
34
+ ## PROJECT::SPECIFIC
35
+ spec/db/
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.5
7
+ - 2.2.0
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: 2.1.5
11
+ - rvm: 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,81 @@
1
+ require 'pathname'
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ SOURCE = ENV.fetch('SOURCE', :git).to_sym
8
+ REPO_POSTFIX = SOURCE == :path ? '' : '.git'
9
+ DATAMAPPER = SOURCE == :path ? Pathname(__FILE__).dirname.parent : 'http://github.com/ar-dm'
10
+ DM_VERSION = '~> 1.2.0'
11
+ DO_VERSION = '~> 0.10.6'
12
+ DM_DO_ADAPTERS = %w[ sqlite postgres mysql oracle sqlserver ]
13
+ CURRENT_BRANCH = ENV.fetch('GIT_BRANCH', 'master')
14
+
15
+ gem 'fastercsv', '~> 1.5'
16
+ gem 'multi_json', '~> 1.0'
17
+ gem 'json', '~> 1.6', :platforms => [ :ruby_18, :jruby ]
18
+ gem 'json_pure', '~> 1.6', :platforms => [ :mswin ]
19
+
20
+ gem 'ardm-core', DM_VERSION,
21
+ SOURCE => "#{DATAMAPPER}/ardm-core#{REPO_POSTFIX}",
22
+ :branch => CURRENT_BRANCH
23
+
24
+ group :development do
25
+ gem 'ardm-validations', DM_VERSION,
26
+ SOURCE => "#{DATAMAPPER}/ardm-validations#{REPO_POSTFIX}",
27
+ :branch => CURRENT_BRANCH
28
+ end
29
+
30
+ group :testing do
31
+ gem 'nokogiri', '~> 1.6'
32
+ gem 'libxml-ruby', '~> 2.0', :platforms => [ :mri, :mswin ]
33
+ end
34
+
35
+ platforms :mri_18 do
36
+ group :quality do
37
+
38
+ gem 'rcov', '~> 0.9.10'
39
+ gem 'yard', '~> 0.7.2'
40
+ gem 'yardstick', '~> 0.4'
41
+
42
+ end
43
+ end
44
+
45
+ group :datamapper do
46
+
47
+ adapters = ENV['ADAPTER'] || ENV['ADAPTERS']
48
+ adapters = adapters.to_s.tr(',', ' ').split.uniq - %w[ in_memory ]
49
+
50
+ if (do_adapters = DM_DO_ADAPTERS & adapters).any?
51
+ do_options = {}
52
+ do_options[:git] = "#{DATAMAPPER}/do#{REPO_POSTFIX}" if ENV['DO_GIT'] == 'true'
53
+
54
+ gem 'data_objects', DO_VERSION, do_options.dup
55
+
56
+ do_adapters.each do |adapter|
57
+ adapter = 'sqlite3' if adapter == 'sqlite'
58
+ gem "do_#{adapter}", DO_VERSION, do_options.dup
59
+ end
60
+
61
+ gem 'ardm-do-adapter', DM_VERSION,
62
+ SOURCE => "#{DATAMAPPER}/ardm-do-adapter#{REPO_POSTFIX}",
63
+ :branch => CURRENT_BRANCH
64
+ end
65
+
66
+ adapters.each do |adapter|
67
+ gem "ardm-#{adapter}-adapter", DM_VERSION,
68
+ SOURCE => "#{DATAMAPPER}/ardm-#{adapter}-adapter#{REPO_POSTFIX}",
69
+ :branch => CURRENT_BRANCH
70
+ end
71
+
72
+ plugins = ENV['PLUGINS'] || ENV['PLUGIN']
73
+ plugins = plugins.to_s.tr(',', ' ').split.push('ardm-migrations').uniq
74
+
75
+ plugins.each do |plugin|
76
+ gem plugin, DM_VERSION,
77
+ SOURCE => "#{DATAMAPPER}/#{plugin}#{REPO_POSTFIX}",
78
+ :branch => CURRENT_BRANCH
79
+ end
80
+
81
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 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.rdoc ADDED
@@ -0,0 +1,64 @@
1
+ = dm-serializer
2
+
3
+ == 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
+ == How it works
8
+
9
+ One method is added to each model/collection for each serialization type - <tt>to_json</tt>, <tt>to_xml</tt>, <tt>to_yaml</tt>, and <tt>to_csv</tt>. With the exception of <tt>to_csv</tt>, all of these methods share the same interface. <tt>to_json</tt> will be used for examples. Any method specific behaviour is documented in its own section below.
10
+
11
+ 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" }
33
+
34
+ You can include associations by passing the association accessor the <tt>:methods</tt> option.
35
+
36
+ If you want to only load a particular serialization method, that's cool, you can do that:
37
+
38
+ require 'dm-serializer/to_json'
39
+
40
+ == to_xml
41
+
42
+ <tt>to_xml</tt> supports some extra options to allow you to override the element names
43
+
44
+ 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>
46
+
47
+
48
+ If you would like a nice speed boost (~5x), require <tt>libxml</tt> or <tt>nokogiri</tt> before <tt>dm-serializer</tt>, and that library will be used rather than REXML.
49
+
50
+ == to_csv
51
+
52
+ <tt>to_csv</tt> currently doesn't support any options yet. It will in the future. It will not support serializing child associations.
53
+
54
+
55
+ == Arrays, Hashes, and other core classes
56
+
57
+ <tt>dm-serializer</tt> only adds serialization methods to DataMapper objects and collections, however some libraries used (json, yaml) add methods to core classes, such as <tt>Array</tt>. Note that passing <tt>dm-serializer</tt> options (such as <tt>:only</tt>) to these methods is *not supported*.
58
+
59
+ Cow.all.to_a.to_yaml(:only => 'name') # WILL NOT WORK
60
+
61
+
62
+ == Beware
63
+
64
+ 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.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ FileList['tasks/**/*.rake'].each { |task| import task }
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/dm-serializer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "ardm-serializer"
6
+ gem.version = DataMapper::Serializer::VERSION
7
+
8
+ gem.authors = [ 'Martin Emde', 'Guy van den Berg', 'Dan Kubb' ]
9
+ gem.email = [ 'me@martinemde.com', "dan.kubb@gmail.com" ]
10
+ gem.summary = 'Ardm fork of dm-serializer'
11
+ gem.description = "DataMapper plugin for serializing Resources and Collections"
12
+ gem.homepage = "https://github.com/ar-dm/ardm-serializer"
13
+ gem.license = 'MIT'
14
+
15
+ gem.files = `git ls-files`.split("\n")
16
+ gem.test_files = `git ls-files -- {spec}/*`.split("\n")
17
+ gem.extra_rdoc_files = %w[LICENSE README.rdoc]
18
+ gem.require_paths = [ "lib" ]
19
+
20
+ gem.add_runtime_dependency 'ardm-core', '~> 1.2'
21
+ gem.add_runtime_dependency 'fastercsv', '~> 1.5'
22
+ gem.add_runtime_dependency 'multi_json', '~> 1.0'
23
+ gem.add_runtime_dependency 'json', '~> 1.6'
24
+ gem.add_runtime_dependency 'json_pure', '~> 1.6'
25
+
26
+ gem.add_development_dependency 'rake', '~> 0.9'
27
+ gem.add_development_dependency 'rspec', '~> 1.3'
28
+ end
@@ -0,0 +1,137 @@
1
+ require "rubygems"
2
+ require 'pathname'
3
+ require 'better-benchmark'
4
+
5
+ gem 'dm-core', '1.0.0'
6
+ require 'dm-core'
7
+
8
+ spec_dir_path = Pathname(__FILE__).dirname.expand_path
9
+ $LOAD_PATH.unshift(spec_dir_path.parent + 'lib/')
10
+ require 'dm-serializer'
11
+
12
+ def load_driver(name, default_uri)
13
+ begin
14
+ DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
15
+ DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
16
+ DataMapper::Repository.adapters[:alternate] = DataMapper::Repository.adapters[name]
17
+ true
18
+ rescue LoadError => e
19
+ warn "Could not load do_#{name}: #{e}"
20
+ false
21
+ end
22
+ end
23
+
24
+ HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
25
+
26
+ module DataMapper
27
+ module Serialize
28
+ # Serialize a Resource to JavaScript Object Notation (JSON; RFC 4627)
29
+ #
30
+ # @return <String> a JSON representation of the Resource
31
+ def to_json_old(*args)
32
+ options = args.first || {}
33
+ result = '{ '
34
+ fields = []
35
+
36
+ propset = properties_to_serialize(options)
37
+
38
+ fields += propset.map do |property|
39
+ "#{property.name.to_json}: #{send(property.name).to_json}"
40
+ end
41
+
42
+ # add methods
43
+ (options[:methods] || []).each do |meth|
44
+ if self.respond_to?(meth)
45
+ fields << "#{meth.to_json}: #{send(meth).to_json}"
46
+ end
47
+ end
48
+
49
+ # Note: if you want to include a whole other model via relation, use :methods
50
+ # comments.to_json(:relationships=>{:user=>{:include=>[:first_name],:methods=>[:age]}})
51
+ # add relationships
52
+ # TODO: This needs tests and also needs to be ported to #to_xml and #to_yaml
53
+ (options[:relationships] || {}).each do |rel,opts|
54
+ if self.respond_to?(rel)
55
+ fields << "#{rel.to_json}: #{send(rel).to_json(opts)}"
56
+ end
57
+ end
58
+
59
+ result << fields.join(', ')
60
+ result << ' }'
61
+ result
62
+ end
63
+ end
64
+
65
+ class Collection
66
+ def to_json_old(*args)
67
+ opts = args.first || {}
68
+ '[' << map { |e| e.to_json_old(opts) }.join(',') << ']'
69
+ end
70
+ end
71
+ end
72
+
73
+
74
+
75
+
76
+ class Cow
77
+ include DataMapper::Resource
78
+
79
+ property :id, Integer, :key => true
80
+ property :composite, Integer, :key => true
81
+ property :name, String
82
+ property :breed, String
83
+
84
+ has n, :baby_cows, :model => 'Cow'
85
+ belongs_to :mother_cow, :model => 'Cow', :required => false
86
+ end
87
+
88
+ DataMapper.auto_migrate!
89
+ cow = Cow.create(
90
+ :id => 89,
91
+ :composite => 34,
92
+ :name => 'Berta',
93
+ :breed => 'Guernsey'
94
+ )
95
+
96
+ 10.times do |n|
97
+ Cow.create(
98
+ :id => n*10,
99
+ :composite => n,
100
+ :name => "Bertha"*n,
101
+ :breed => "Mooing#{n}"
102
+ )
103
+ end
104
+
105
+ all_cows = Cow.all.reload
106
+
107
+ puts "Benchmarking single resource serialization."
108
+ puts "Set 1: old method"
109
+ puts "Set 2: new method"
110
+ result = Benchmark.compare_realtime(
111
+ :iterations => 10,
112
+ :inner_iterations => 20000,
113
+ :verbose => true
114
+ ) { |iteration|
115
+ cow.to_json_old
116
+ }.with { |iteration|
117
+ cow.to_json
118
+ }
119
+
120
+ Benchmark.report_on result
121
+
122
+ puts
123
+
124
+ puts "Benchmarking collection serialization."
125
+ puts "Set 1: old method"
126
+ puts "Set 2: new method"
127
+ result = Benchmark.compare_realtime(
128
+ :iterations => 10,
129
+ :inner_iterations => 5000,
130
+ :verbose => true
131
+ ) { |iteration|
132
+ all_cows.to_json_old
133
+ }.with { |iteration|
134
+ all_cows.to_json
135
+ }
136
+
137
+ Benchmark.report_on result
@@ -0,0 +1,89 @@
1
+ require "rubygems"
2
+ require 'pathname'
3
+
4
+ gem 'dm-core', '1.0.0'
5
+ require 'dm-core'
6
+
7
+ spec_dir_path = Pathname(__FILE__).dirname.expand_path
8
+ $LOAD_PATH.unshift(spec_dir_path.parent + 'lib/')
9
+ require 'dm-serializer'
10
+
11
+ def load_driver(name, default_uri)
12
+ begin
13
+ DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
14
+ DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
15
+ DataMapper::Repository.adapters[:alternate] = DataMapper::Repository.adapters[name]
16
+ true
17
+ rescue LoadError => e
18
+ warn "Could not load do_#{name}: #{e}"
19
+ false
20
+ end
21
+ end
22
+
23
+ HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
24
+
25
+ class Cow
26
+ include DataMapper::Resource
27
+
28
+ property :id, Integer, :key => true
29
+ property :composite, Integer, :key => true
30
+ property :name, String
31
+ property :breed, String
32
+
33
+ has n, :baby_cows, :model => 'Cow'
34
+ belongs_to :mother_cow, :model => 'Cow'
35
+ end
36
+
37
+ require "benchwarmer"
38
+
39
+ TIMES = 2000
40
+ DataMapper.auto_migrate!
41
+ cow = Cow.create(
42
+ :id => 89,
43
+ :composite => 34,
44
+ :name => 'Berta',
45
+ :breed => 'Guernsey'
46
+ )
47
+ all_cows = Cow.all
48
+
49
+ puts "REXML"
50
+ Benchmark.warmer(TIMES) do
51
+ group("Serialization:") do
52
+ report "Single Resource" do
53
+ cow.to_xml
54
+ end
55
+ report "Collection" do
56
+ all_cows.to_xml
57
+ end
58
+ end
59
+ end
60
+
61
+ require 'nokogiri'
62
+ load 'dm-serializer/xml_serializers.rb'
63
+
64
+ puts "Nokogiri"
65
+ Benchmark.warmer(TIMES) do
66
+ group("Serialization:") do
67
+ report "Single Resource" do
68
+ cow.to_xml
69
+ end
70
+ report "Collection" do
71
+ all_cows.to_xml
72
+ end
73
+ end
74
+ end
75
+
76
+ require 'libxml'
77
+ load 'dm-serializer/xml_serializers.rb'
78
+
79
+ puts "LibXML"
80
+ Benchmark.warmer(TIMES) do
81
+ group("Serialization:") do
82
+ report "Single Resource" do
83
+ cow.to_xml
84
+ end
85
+ report "Collection" do
86
+ all_cows.to_xml
87
+ end
88
+ end
89
+ end