restful_serializer 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,6 @@
1
1
  *.swp
2
+ .rvmrc
3
+ log/*
2
4
  .vimrc
3
5
  .current
4
6
  spec/*.log
@@ -6,7 +6,7 @@ If the model has a name column, it will be used to describe the resource.
6
6
 
7
7
  This library does not attempt to provide href info for transitions, or deal much with questions of authorization beyond what is specified in the serialization configuration lines. It assumes that these issues would be resolved in the controller. It assumes standard naming conventions for routes.
8
8
 
9
- Tested with Rails 2.3.11. Requires the Rails framework to be loaded.
9
+ Requires Rails 3.
10
10
 
11
11
  == Example
12
12
 
@@ -124,3 +124,10 @@ Example output:
124
124
  = More Docs
125
125
 
126
126
  Please see the Restful rdoc and the specs for more details.
127
+
128
+ * https://github.com/jpartlow/restful_serializer
129
+ * API: http://rubydoc.info/gems/restful_serializer/frames
130
+
131
+ = Rails 2
132
+
133
+ This gem requires Rails 3. To use it with Rails 2, you will need version 0.1.4, which is tagged 'rails2' in github.
data/Rakefile CHANGED
@@ -5,22 +5,21 @@ require 'rubygems'
5
5
  require 'rake'
6
6
  require 'restful/version'
7
7
 
8
- require 'spec/rake/spectask'
9
- Spec::Rake::SpecTask.new(:spec) do |spec|
10
- spec.libs << 'lib' << 'spec'
11
- spec.spec_files = FileList['spec/**/*_spec.rb']
8
+ require 'rspec/core/rake_task'
9
+
10
+ RSpec::Core::RakeTask.new(:spec) do |spec|
11
+ spec.pattern = 'spec/**/*_spec.rb'
12
12
  end
13
13
 
14
- Spec::Rake::SpecTask.new(:rcov) do |spec|
15
- spec.libs << 'lib' << 'spec'
14
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
16
15
  spec.pattern = 'spec/**/*_spec.rb'
17
16
  spec.rcov = true
18
17
  end
19
18
 
20
19
  task :default => :spec
21
20
 
22
- require 'rake/rdoctask'
23
- Rake::RDocTask.new do |rdoc|
21
+ require 'rdoc/task'
22
+ RDoc::Task.new do |rdoc|
24
23
  version = Restful::VERSION
25
24
 
26
25
  rdoc.rdoc_dir = 'rdoc'
@@ -1,5 +1,7 @@
1
1
  # This file is part of restful_serializer. Copyright 2011 Joshua Partlow. This is free software, see the LICENSE file for details.
2
2
 
3
+ require 'deep_merge/rails_compat'
4
+
3
5
  # This library is used to decorate ActiveRecord with methods to assist in generating
4
6
  # Restful content for Web Services.
5
7
  #
@@ -171,20 +173,6 @@ module Restful
171
173
  end
172
174
  end
173
175
 
174
- # Rails 2.3 Hash is extended with :deep_merge and :deep_merge! already, and route
175
- # generation requires these versions. We're using the +deep_merge+ gem's versions,
176
- # and need to setup aliases to ensure that Rails versions remain available while
177
- # we are still able to access the gem's versions.
178
- Hash.class_eval do
179
- alias_method :rails_deep_merge, :deep_merge
180
- alias_method :rails_deep_merge!, :deep_merge!
181
- require 'deep_merge'
182
- alias_method :_rs_deep_merge, :deep_merge
183
- alias_method :_rs_deep_merge!, :deep_merge!
184
- alias_method :deep_merge, :rails_deep_merge
185
- alias_method :deep_merge!, :rails_deep_merge!
186
- end
187
-
188
176
  ActiveRecord::Base.send(:include, Restful::Extensions)
189
- ActiveRecord::Associations::AssociationProxy.send(:include, Restful::Extensions)
177
+ ActiveRecord::Associations::CollectionProxy.send(:include, Restful::Extensions)
190
178
  Array.send(:include, Restful::Extensions)
@@ -21,7 +21,7 @@ module Restful
21
21
  def self.included(base)
22
22
  base.class_eval do
23
23
  extend ClassMethods
24
- class_inheritable_array :options
24
+ class_attribute :options
25
25
  self.options = []
26
26
  end
27
27
  end
@@ -131,7 +131,7 @@ module Restful
131
131
  # will be raised.
132
132
  #
133
133
  # Empty container options and defaulted options are skipped.
134
- def deep_merge!(other)
134
+ def deeper_merge!(other)
135
135
  other_hash = case other
136
136
  when Configurable then other.to_hash(:ignore_empty => true, :skip_defaults => true)
137
137
  else other
@@ -139,7 +139,7 @@ module Restful
139
139
 
140
140
  hash = to_hash(:ignore_empty => true, :skip_defaults => true)
141
141
  new_configurable = self.class.new
142
- hash._rs_deep_merge!(other_hash)
142
+ hash.deeper_merge!(other_hash)
143
143
  return new_configurable.set(hash)
144
144
  end
145
145
 
@@ -310,6 +310,7 @@ module Restful
310
310
  #
311
311
  # Resources configurations are set with a call to +register_resource+.
312
312
  class WebService < Configurator
313
+ self.options = []
313
314
  option :name
314
315
  option :api_prefix
315
316
  option :default_url_options, :type => :hash
@@ -368,6 +369,7 @@ module Restful
368
369
  # options specifically set for the class will be used. Default false.
369
370
  #
370
371
  class Resource < Configurator
372
+ self.options = []
371
373
  option :name_method, :default => :name
372
374
  option :url_for
373
375
  option :associations, :type => :hash
@@ -399,6 +401,7 @@ module Restful
399
401
  #
400
402
  # See ActiveRecord::Serialization.to_json for more details
401
403
  class ARSerialization < Configurator
404
+ self.options = []
402
405
  option :only, :type => :array
403
406
  option :except, :type => :array
404
407
  option :include, :type => :hash, :element_type => 'Restful::Configuration::ARSerialization'
@@ -48,7 +48,7 @@ module Restful
48
48
  # # "http://example.com/user/1/comments"
49
49
  #
50
50
  class Url
51
- include ActionController::UrlWriter
51
+ include Rails.application.routes.url_helpers
52
52
  @@initialized = false
53
53
 
54
54
  attr_accessor :options
@@ -157,9 +157,9 @@ module Restful
157
157
 
158
158
  # Encode as a resource hash.
159
159
  def serialize
160
- case
161
- when subject.respond_to?(:attribute_names) then _serialize_active_record
160
+ case
162
161
  when subject.kind_of?(Array) then _serialize_array
162
+ when subject.respond_to?(:attribute_names) then _serialize_active_record
163
163
  else ActiveSupport::JSON.decode(subject.to_json) # just capture the hash of the object structure
164
164
  end
165
165
  end
@@ -225,9 +225,9 @@ module Restful
225
225
  self.resource_configuration =
226
226
  (klass == base_klass || class_configuration.no_inherited_options) ?
227
227
  class_configuration.deep_clone :
228
- base_configuration.deep_merge!(class_configuration)
228
+ base_configuration.deeper_merge!(class_configuration)
229
229
 
230
- self.resource_configuration = resource_configuration.deep_merge!(passed_configuration)
230
+ self.resource_configuration = self.resource_configuration.deeper_merge!(passed_configuration)
231
231
 
232
232
  yield(resource_configuration) if block_given?
233
233
 
@@ -236,7 +236,7 @@ module Restful
236
236
 
237
237
  def _serialize_active_record
238
238
  restful = DeepHash[
239
- klass => ActiveRecord::Serialization::Serializer.new(subject, active_record_serialization_options).serializable_record
239
+ klass => subject.serializable_hash(active_record_serialization_options)
240
240
  ]
241
241
  restful['name'] = name if name
242
242
  restful['href'] = href
@@ -248,7 +248,7 @@ module Restful
248
248
  end
249
249
 
250
250
  def _serialize_array
251
- restful = subject.map do |e|
251
+ restful = subject.to_a.map do |e|
252
252
  array_options = options.clone
253
253
  array_options = { :shallow => true }.merge(array_options)
254
254
  Serializer.new(e, web_service, array_options, &configure_block).serialize
@@ -1,3 +1,3 @@
1
1
  module Restful
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -23,9 +23,8 @@ Gem::Specification.new do |s|
23
23
  s.files = `git ls-files`.split("\n")
24
24
  s.test_files = `git ls-files spec/*`.split("\n")
25
25
 
26
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
27
- s.add_runtime_dependency(%q<activerecord>, [">= 2.1.0", "<3.0.0"])
28
- s.add_runtime_dependency(%q<activesupport>, [">= 2.1.0", "<3.0.0"])
29
- s.add_runtime_dependency(%q<actionpack>, [">= 2.1.0", "<3.0.0"])
30
- s.add_runtime_dependency(%q<deep_merge>, [">= 0.1.0"])
26
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
27
+ s.add_development_dependency(%q<sqlite3>)
28
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.0", "< 4.0.0"])
29
+ s.add_runtime_dependency(%q<deep_merge>, [">= 1.0.0"])
31
30
  end
@@ -148,7 +148,7 @@ describe Restful::Configuration do
148
148
  end
149
149
 
150
150
  it "should provide a deep merge" do
151
- merged = @test.deep_merge!(@different)
151
+ merged = @test.deeper_merge!(@different)
152
152
  merged.name.should == @different.name
153
153
  merged.subs[:sub1].should == @sub1
154
154
  merged.subs[:sub2].stuff.should == @sub2.stuff + @difsub2.stuff
@@ -157,9 +157,9 @@ describe Restful::Configuration do
157
157
  end
158
158
 
159
159
  it "should raise errors if attempt to deep merge with different options" do
160
- @test.deep_merge!(nil).should == @test
161
- lambda { @test.deep_merge!(@sub1) }.should raise_error(ArgumentError)
162
- lambda { @test.deep_merge!(:frotz => :spaniel) }.should raise_error(ArgumentError)
160
+ @test.deeper_merge!(nil).should == @test
161
+ lambda { @test.deeper_merge!(@sub1) }.should raise_error(ArgumentError)
162
+ lambda { @test.deeper_merge!(:frotz => :spaniel) }.should raise_error(ArgumentError)
163
163
  end
164
164
 
165
165
  end
@@ -1,7 +1,8 @@
1
1
  require 'rubygems'
2
2
 
3
- gem 'activerecord', '>=2.1.0'
3
+ gem 'activerecord', '>=3.0.0'
4
4
  require 'active_record'
5
+ require 'logger'
5
6
 
6
7
  ActiveRecord::Base.establish_connection({'adapter' => 'sqlite3', 'database' => ':memory:'})
7
8
  ActiveRecord::Base.logger = Logger.new("#{File.dirname(__FILE__)}/active_record.log")
@@ -3,15 +3,18 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
3
3
  # These routes are only required by some tests, but route generation issues crop
4
4
  # up depending on the order in which restful/* files are required. The reasons
5
5
  # are mysterious, though they may have something to do with ActionController::UrlWriter
6
- ActionController::Routing::Routes.clear!
7
- ActionController::Routing::Routes.draw do |map|
8
- map.resources :foos
9
- map.prefix_foo 'prefix/foos/:id', :controller => 'foos', :action => 'show'
10
- map.custom_foo 'custom_foo/:id', :controller => 'foos', :action => 'show'
11
- map.resources :bars do |bars|
12
- bars.resources :dingos
6
+ # ^^^ this was a rails2 note and may no longer apply
7
+ Rails.application.routes.clear!
8
+ Rails.application.routes.draw do
9
+ resources :foos
10
+ controller :foos do
11
+ match 'prefix/foos/:id' => :show, :as => :prefix_foo
12
+ match 'custom_foo/:id' => :show, :as => :custom_foo
13
+ end
14
+ resources :bars do
15
+ resources :dingos
13
16
  end
14
- map.resources :things
17
+ resources :things
15
18
  end
16
19
 
17
20
  describe Restful::Serializer do
@@ -80,11 +83,11 @@ describe Restful::Serializer do
80
83
  end
81
84
 
82
85
  def check_routing
83
- ActionController::Routing::Routes.routes.size.should == 30
84
- by_method = ActionController::Routing::Routes.routes.group_by { |r|
85
- r.conditions[:method]
86
+ Rails.application.routes.routes.size.should == 30
87
+ by_method = Rails.application.routes.routes.group_by { |r|
88
+ method = r.verb.blank? ? nil : r.verb.downcase.to_sym
86
89
  }
87
- # ActionController::Routing::Routes.routes.each { |r| puts r }
90
+ # Rails.application.routes.routes.each { |r| puts r }
88
91
  by_method[:get].size.should == 4 * 4
89
92
  by_method[:put].size.should == 4
90
93
  by_method[:post].size.should == 4
@@ -1,21 +1,35 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'rubygems'
4
- require 'action_controller'
5
- require 'database'
6
3
 
7
- require 'spec'
8
- require 'spec/autorun'
9
- require 'pp'
4
+ ENV['RAILS_ENV'] = 'test'
10
5
 
11
- require 'restful'
6
+ require 'rubygems'
7
+ gem 'actionpack', '>=3.0.0'
8
+ gem 'activesupport', '>=3.0.0'
9
+ gem 'activerecord', '>=3.0.0'
10
+ gem 'railties', '>=3.0.0'
12
11
 
13
- #class TestLogger
14
- # [:debug, :info, :warn, :error].each do |m|
15
- # define_method(m) { |message| puts "#{m.to_s.upcase}: #{message}" }
16
- # end
17
- #end
12
+ # Only the parts of rails we want to use
13
+ # if you want everything, use "rails/all"
14
+ require "action_controller/railtie"
15
+
16
+ # Define the application and configuration
17
+ module RestfulTest
18
+ class Application < ::Rails::Application
19
+ # configuration here if needed
20
+ config.active_support.deprecation = :stderr
21
+ end
22
+ end
23
+
24
+ # Initialize the application
25
+ RestfulTest::Application.initialize!
18
26
 
19
- Spec::Runner.configure do |config|
27
+ require 'rspec'
28
+
29
+ RSpec.configure do |config|
20
30
 
21
31
  end
32
+
33
+ require 'database'
34
+ require 'pp'
35
+ require 'restful'
metadata CHANGED
@@ -1,137 +1,72 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: restful_serializer
3
- version: !ruby/object:Gem::Version
4
- hash: 19
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 4
10
- version: 0.1.4
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Josh Partlow
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-06-01 00:00:00 -07:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-03-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rspec
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &15152020 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 13
30
- segments:
31
- - 1
32
- - 2
33
- - 9
34
- version: 1.2.9
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
35
22
  type: :development
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: activerecord
39
23
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *15152020
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &15151260 !ruby/object:Gem::Requirement
41
28
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 11
46
- segments:
47
- - 2
48
- - 1
49
- - 0
50
- version: 2.1.0
51
- - - <
52
- - !ruby/object:Gem::Version
53
- hash: 7
54
- segments:
55
- - 3
56
- - 0
57
- - 0
58
- version: 3.0.0
59
- type: :runtime
60
- version_requirements: *id002
61
- - !ruby/object:Gem::Dependency
62
- name: activesupport
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
63
34
  prerelease: false
64
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *15151260
36
+ - !ruby/object:Gem::Dependency
37
+ name: rails
38
+ requirement: &15149300 !ruby/object:Gem::Requirement
65
39
  none: false
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- hash: 11
70
- segments:
71
- - 2
72
- - 1
73
- - 0
74
- version: 2.1.0
75
- - - <
76
- - !ruby/object:Gem::Version
77
- hash: 7
78
- segments:
79
- - 3
80
- - 0
81
- - 0
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
82
43
  version: 3.0.0
83
- type: :runtime
84
- version_requirements: *id003
85
- - !ruby/object:Gem::Dependency
86
- name: actionpack
87
- prerelease: false
88
- requirement: &id004 !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ">="
92
- - !ruby/object:Gem::Version
93
- hash: 11
94
- segments:
95
- - 2
96
- - 1
97
- - 0
98
- version: 2.1.0
99
44
  - - <
100
- - !ruby/object:Gem::Version
101
- hash: 7
102
- segments:
103
- - 3
104
- - 0
105
- - 0
106
- version: 3.0.0
45
+ - !ruby/object:Gem::Version
46
+ version: 4.0.0
107
47
  type: :runtime
108
- version_requirements: *id004
109
- - !ruby/object:Gem::Dependency
110
- name: deep_merge
111
48
  prerelease: false
112
- requirement: &id005 !ruby/object:Gem::Requirement
49
+ version_requirements: *15149300
50
+ - !ruby/object:Gem::Dependency
51
+ name: deep_merge
52
+ requirement: &15147460 !ruby/object:Gem::Requirement
113
53
  none: false
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- hash: 27
118
- segments:
119
- - 0
120
- - 1
121
- - 0
122
- version: 0.1.0
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: 1.0.0
123
58
  type: :runtime
124
- version_requirements: *id005
125
- description: This library is used to decorate ActiveRecord with methods to assist in generating Restful content for Web Services.
59
+ prerelease: false
60
+ version_requirements: *15147460
61
+ description: This library is used to decorate ActiveRecord with methods to assist
62
+ in generating Restful content for Web Services.
126
63
  email: jpartlow@glatisant.org
127
64
  executables: []
128
-
129
65
  extensions: []
130
-
131
- extra_rdoc_files:
66
+ extra_rdoc_files:
132
67
  - LICENSE
133
68
  - README.rdoc
134
- files:
69
+ files:
135
70
  - .gitignore
136
71
  - LICENSE
137
72
  - README.rdoc
@@ -148,44 +83,33 @@ files:
148
83
  - spec/spec.opts
149
84
  - spec/spec_helper.rb
150
85
  - spec/web_service_spec.rb
151
- has_rdoc: true
152
86
  homepage: http://github.com/jpartlow/restful_serializer
153
87
  licenses: []
154
-
155
88
  post_install_message:
156
- rdoc_options:
89
+ rdoc_options:
157
90
  - --main=README.rdoc
158
91
  - --charset=UTF-8
159
- require_paths:
92
+ require_paths:
160
93
  - lib
161
- required_ruby_version: !ruby/object:Gem::Requirement
94
+ required_ruby_version: !ruby/object:Gem::Requirement
162
95
  none: false
163
- requirements:
164
- - - ">="
165
- - !ruby/object:Gem::Version
166
- hash: 3
167
- segments:
168
- - 0
169
- version: "0"
170
- required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
101
  none: false
172
- requirements:
173
- - - ">="
174
- - !ruby/object:Gem::Version
175
- hash: 23
176
- segments:
177
- - 1
178
- - 3
179
- - 6
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
180
105
  version: 1.3.6
181
106
  requirements: []
182
-
183
107
  rubyforge_project:
184
- rubygems_version: 1.6.2
108
+ rubygems_version: 1.8.10
185
109
  signing_key:
186
110
  specification_version: 3
187
111
  summary: Helps with serializing activerecord instances as Restful resources.
188
- test_files:
112
+ test_files:
189
113
  - spec/configuration_spec.rb
190
114
  - spec/database.rb
191
115
  - spec/restful_spec.rb