serialization_scopes 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/serialization_scopes.rb +24 -11
- metadata +31 -49
- data/.gitignore +0 -1
- data/Gemfile +0 -6
- data/Gemfile.lock +0 -46
- data/Rakefile +0 -26
- data/serialization_scopes.gemspec +0 -56
- data/spec/helper.rb +0 -26
- data/spec/serialization_scopes_spec.rb +0 -68
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/serialization_scopes.rb
CHANGED
@@ -1,32 +1,41 @@
|
|
1
1
|
module SerializationScopes
|
2
|
+
extend ActiveSupport::Concern
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
base.write_inheritable_attribute :serialization_scopes, {}
|
4
|
+
included do
|
5
|
+
class_inheritable_reader :serialization_scopes
|
6
|
+
write_inheritable_attribute :serialization_scopes, {}
|
7
7
|
end
|
8
8
|
|
9
9
|
module ClassMethods
|
10
|
+
|
10
11
|
def serialization_scope(name, options = {})
|
11
12
|
serialization_scopes[name.to_sym] = options
|
12
13
|
end
|
13
14
|
|
14
15
|
def scoped_serialization_options(options = {})
|
15
|
-
|
16
|
-
|
17
|
-
scopes.
|
18
|
-
|
19
|
-
|
16
|
+
options ||= {}
|
17
|
+
name = (options || {})[:scope]
|
18
|
+
scopes = name.present? && serialization_scopes[name.to_sym] ? serialization_scopes[name.to_sym] : serialization_scopes[:default]
|
19
|
+
scopes.each do |key, scope_options|
|
20
|
+
custom_options = options[key]
|
21
|
+
options[key] = if key == :except
|
22
|
+
custom_options ? (Array.wrap(custom_options) + Array.wrap(scope_options)).uniq : Array.wrap(scope_options)
|
23
|
+
elsif [:only, :methods, :include].include?(key)
|
24
|
+
custom_options ? Array.wrap(custom_options) & Array.wrap(scope_options) : Array.wrap(scope_options)
|
25
|
+
else
|
26
|
+
custom_options ? custom_options : scope_options
|
27
|
+
end
|
20
28
|
end if scopes
|
21
29
|
options
|
22
30
|
end
|
31
|
+
|
23
32
|
end
|
24
33
|
|
25
34
|
def to_xml(options = {})
|
26
35
|
super self.class.scoped_serialization_options(options)
|
27
36
|
end
|
28
37
|
|
29
|
-
def
|
38
|
+
def as_json(options = {})
|
30
39
|
super self.class.scoped_serialization_options(options)
|
31
40
|
end
|
32
41
|
|
@@ -34,4 +43,8 @@ end
|
|
34
43
|
|
35
44
|
ActiveRecord::Base.class_eval do
|
36
45
|
include SerializationScopes
|
37
|
-
end
|
46
|
+
end if defined?(ActiveRecord::Base)
|
47
|
+
|
48
|
+
ActiveResource::Base.class_eval do
|
49
|
+
include SerializationScopes
|
50
|
+
end if defined?(ActiveResource::Base)
|
metadata
CHANGED
@@ -1,68 +1,47 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serialization_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dimitrij Denissenko
|
14
|
+
- Evgeniy Dolzhenko
|
14
15
|
autorequire:
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date:
|
19
|
+
date: 2011-04-20 00:00:00 +01:00
|
19
20
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
requirements:
|
25
|
-
- - ">"
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
hash: 27
|
28
|
-
segments:
|
29
|
-
- 2
|
30
|
-
- 5
|
31
|
-
- 0
|
32
|
-
version: 2.5.0
|
33
|
-
requirement: *id001
|
34
|
-
prerelease: false
|
35
|
-
type: :runtime
|
36
|
-
name: activerecord
|
37
|
-
description: Adds named scopes for ActiveRecord serialization methods (to_xml, to_json)
|
38
|
-
email: dimitrij@blacksquaremedia.com
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: Adds named scopes for ActiveRecord/ActiveResource serialization methods (to_xml, to_json)
|
24
|
+
email: info@blacksquaremedia.com
|
39
25
|
executables: []
|
40
26
|
|
41
27
|
extensions: []
|
42
28
|
|
43
|
-
extra_rdoc_files:
|
44
|
-
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
45
31
|
files:
|
46
|
-
- .gitignore
|
47
|
-
- Gemfile
|
48
|
-
- Gemfile.lock
|
49
|
-
- README
|
50
|
-
- Rakefile
|
51
32
|
- VERSION
|
33
|
+
- README
|
52
34
|
- lib/serialization_scopes.rb
|
53
|
-
- lib/serialization_scopes/matchers.rb
|
54
35
|
- lib/serialization_scopes/matchers/serialize_matcher.rb
|
36
|
+
- lib/serialization_scopes/matchers.rb
|
55
37
|
- rails/init.rb
|
56
|
-
- serialization_scopes.gemspec
|
57
|
-
- spec/helper.rb
|
58
|
-
- spec/serialization_scopes_spec.rb
|
59
38
|
has_rdoc: true
|
60
39
|
homepage: http://github.com/dim/serialization_scopes
|
61
40
|
licenses: []
|
62
41
|
|
63
42
|
post_install_message:
|
64
|
-
rdoc_options:
|
65
|
-
|
43
|
+
rdoc_options: []
|
44
|
+
|
66
45
|
require_paths:
|
67
46
|
- lib
|
68
47
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -70,26 +49,29 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
49
|
requirements:
|
71
50
|
- - ">="
|
72
51
|
- !ruby/object:Gem::Version
|
73
|
-
hash:
|
52
|
+
hash: 57
|
74
53
|
segments:
|
75
|
-
-
|
76
|
-
|
54
|
+
- 1
|
55
|
+
- 8
|
56
|
+
- 7
|
57
|
+
version: 1.8.7
|
77
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
59
|
none: false
|
79
60
|
requirements:
|
80
61
|
- - ">="
|
81
62
|
- !ruby/object:Gem::Version
|
82
|
-
hash:
|
63
|
+
hash: 23
|
83
64
|
segments:
|
84
|
-
-
|
85
|
-
|
65
|
+
- 1
|
66
|
+
- 3
|
67
|
+
- 6
|
68
|
+
version: 1.3.6
|
86
69
|
requirements: []
|
87
70
|
|
88
71
|
rubyforge_project:
|
89
|
-
rubygems_version: 1.
|
72
|
+
rubygems_version: 1.6.2
|
90
73
|
signing_key:
|
91
74
|
specification_version: 3
|
92
|
-
summary: Named scopes for ActiveRecord serialization methods (to_xml, to_json)
|
93
|
-
test_files:
|
94
|
-
|
95
|
-
- spec/serialization_scopes_spec.rb
|
75
|
+
summary: Named scopes for ActiveRecord/ActiveResource serialization methods (to_xml, to_json)
|
76
|
+
test_files: []
|
77
|
+
|
data/.gitignore
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
pkg/
|
data/Gemfile
DELETED
data/Gemfile.lock
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
activemodel (3.0.0.rc)
|
5
|
-
activesupport (= 3.0.0.rc)
|
6
|
-
builder (~> 2.1.2)
|
7
|
-
i18n (~> 0.4.1)
|
8
|
-
activerecord (3.0.0.rc)
|
9
|
-
activemodel (= 3.0.0.rc)
|
10
|
-
activesupport (= 3.0.0.rc)
|
11
|
-
arel (~> 0.4.0)
|
12
|
-
tzinfo (~> 0.3.22)
|
13
|
-
activesupport (3.0.0.rc)
|
14
|
-
arel (0.4.0)
|
15
|
-
activesupport (>= 3.0.0.beta)
|
16
|
-
builder (2.1.2)
|
17
|
-
diff-lcs (1.1.2)
|
18
|
-
gemcutter (0.6.1)
|
19
|
-
git (1.2.5)
|
20
|
-
i18n (0.4.1)
|
21
|
-
jeweler (1.4.0)
|
22
|
-
gemcutter (>= 0.1.0)
|
23
|
-
git (>= 1.2.5)
|
24
|
-
rubyforge (>= 2.0.0)
|
25
|
-
json_pure (1.4.6)
|
26
|
-
rake (0.8.7)
|
27
|
-
rspec (2.0.0.beta.19)
|
28
|
-
rspec-core (= 2.0.0.beta.19)
|
29
|
-
rspec-expectations (= 2.0.0.beta.19)
|
30
|
-
rspec-mocks (= 2.0.0.beta.19)
|
31
|
-
rspec-core (2.0.0.beta.19)
|
32
|
-
rspec-expectations (2.0.0.beta.19)
|
33
|
-
diff-lcs (>= 1.1.2)
|
34
|
-
rspec-mocks (2.0.0.beta.19)
|
35
|
-
rubyforge (2.0.4)
|
36
|
-
json_pure (>= 1.1.7)
|
37
|
-
tzinfo (0.3.22)
|
38
|
-
|
39
|
-
PLATFORMS
|
40
|
-
ruby
|
41
|
-
|
42
|
-
DEPENDENCIES
|
43
|
-
activerecord (>= 3.0.0.rc)
|
44
|
-
jeweler
|
45
|
-
rake
|
46
|
-
rspec (>= 2.0.0.beta.17)
|
data/Rakefile
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'bundler'
|
2
|
-
Bundler.setup
|
3
|
-
|
4
|
-
require 'rake'
|
5
|
-
require 'rspec/mocks/version'
|
6
|
-
require 'rspec/core/rake_task'
|
7
|
-
|
8
|
-
RSpec::Core::RakeTask.new(:spec)
|
9
|
-
|
10
|
-
desc 'Default: run specs.'
|
11
|
-
task :default => :spec
|
12
|
-
|
13
|
-
begin
|
14
|
-
require 'jeweler'
|
15
|
-
Jeweler::Tasks.new do |gemspec|
|
16
|
-
gemspec.name = "serialization_scopes"
|
17
|
-
gemspec.summary = "Named scopes for ActiveRecord serialization methods (to_xml, to_json)"
|
18
|
-
gemspec.description = "Adds named scopes for ActiveRecord serialization methods (to_xml, to_json)"
|
19
|
-
gemspec.email = "dimitrij@blacksquaremedia.com"
|
20
|
-
gemspec.homepage = "http://github.com/dim/serialization_scopes"
|
21
|
-
gemspec.authors = ["Dimitrij Denissenko"]
|
22
|
-
gemspec.add_runtime_dependency "activerecord", "> 2.5.0"
|
23
|
-
end
|
24
|
-
Jeweler::GemcutterTasks.new
|
25
|
-
rescue LoadError
|
26
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{serialization_scopes}
|
8
|
-
s.version = "0.2.1"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Dimitrij Denissenko"]
|
12
|
-
s.date = %q{2010-08-19}
|
13
|
-
s.description = %q{Adds named scopes for ActiveRecord serialization methods (to_xml, to_json)}
|
14
|
-
s.email = %q{dimitrij@blacksquaremedia.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"README"
|
17
|
-
]
|
18
|
-
s.files = [
|
19
|
-
".gitignore",
|
20
|
-
"Gemfile",
|
21
|
-
"Gemfile.lock",
|
22
|
-
"README",
|
23
|
-
"Rakefile",
|
24
|
-
"VERSION",
|
25
|
-
"lib/serialization_scopes.rb",
|
26
|
-
"lib/serialization_scopes/matchers.rb",
|
27
|
-
"lib/serialization_scopes/matchers/serialize_matcher.rb",
|
28
|
-
"rails/init.rb",
|
29
|
-
"serialization_scopes.gemspec",
|
30
|
-
"spec/helper.rb",
|
31
|
-
"spec/serialization_scopes_spec.rb"
|
32
|
-
]
|
33
|
-
s.homepage = %q{http://github.com/dim/serialization_scopes}
|
34
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
-
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version = %q{1.3.7}
|
37
|
-
s.summary = %q{Named scopes for ActiveRecord serialization methods (to_xml, to_json)}
|
38
|
-
s.test_files = [
|
39
|
-
"spec/helper.rb",
|
40
|
-
"spec/serialization_scopes_spec.rb"
|
41
|
-
]
|
42
|
-
|
43
|
-
if s.respond_to? :specification_version then
|
44
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
-
s.specification_version = 3
|
46
|
-
|
47
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
-
s.add_runtime_dependency(%q<activerecord>, ["> 2.5.0"])
|
49
|
-
else
|
50
|
-
s.add_dependency(%q<activerecord>, ["> 2.5.0"])
|
51
|
-
end
|
52
|
-
else
|
53
|
-
s.add_dependency(%q<activerecord>, ["> 2.5.0"])
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
data/spec/helper.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
ENV["RAILS_ENV"] ||= 'test'
|
2
|
-
$:.unshift File.dirname(__FILE__)
|
3
|
-
$:.unshift File.expand_path('../../lib', __FILE__)
|
4
|
-
|
5
|
-
require 'rubygems'
|
6
|
-
|
7
|
-
# Set up gems listed in the Gemfile.
|
8
|
-
gemfile = File.expand_path('../../Gemfile', __FILE__)
|
9
|
-
begin
|
10
|
-
require 'bundler'
|
11
|
-
Bundler.setup
|
12
|
-
rescue Bundler::GemNotFound => e
|
13
|
-
STDERR.puts e.message
|
14
|
-
STDERR.puts "Try running `bundle install`."
|
15
|
-
exit!
|
16
|
-
end
|
17
|
-
|
18
|
-
require 'active_record'
|
19
|
-
require 'serialization_scopes'
|
20
|
-
require 'rspec'
|
21
|
-
|
22
|
-
RSpec.configure do |c|
|
23
|
-
c.mock_with :rspec
|
24
|
-
end
|
25
|
-
|
26
|
-
ActiveRecord::Base.include_root_in_json = false
|
@@ -1,68 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
-
|
3
|
-
describe SerializationScopes do
|
4
|
-
|
5
|
-
before do
|
6
|
-
ActiveRecord::Base.stub!(:establish_connection)
|
7
|
-
end
|
8
|
-
|
9
|
-
class SomeModel < ActiveRecord::Base
|
10
|
-
serialization_scope :default, :only => [:id, :name], :methods => :currency
|
11
|
-
serialization_scope :admin, :only => [:id, :secret]
|
12
|
-
after_initialize :set_defaults
|
13
|
-
|
14
|
-
def self.columns
|
15
|
-
@columns ||= [
|
16
|
-
ActiveRecord::ConnectionAdapters::Column.new('id', nil, 'integer'),
|
17
|
-
ActiveRecord::ConnectionAdapters::Column.new('name', nil, 'string'),
|
18
|
-
ActiveRecord::ConnectionAdapters::Column.new('secret', nil, 'string')
|
19
|
-
]
|
20
|
-
end
|
21
|
-
|
22
|
-
def set_defaults
|
23
|
-
self.id = 1
|
24
|
-
self.name = 'Any'
|
25
|
-
self.secret = 'key'
|
26
|
-
end
|
27
|
-
|
28
|
-
def currency
|
29
|
-
'USD'
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'should constraint to_xml' do
|
34
|
-
SomeModel.new.to_xml.should == %(<?xml version="1.0" encoding="UTF-8"?>
|
35
|
-
<some-model>
|
36
|
-
<name>Any</name>
|
37
|
-
<id type="integer">1</id>
|
38
|
-
<currency>USD</currency>
|
39
|
-
</some-model>
|
40
|
-
)
|
41
|
-
end
|
42
|
-
|
43
|
-
def as_hash(options = {})
|
44
|
-
ActiveSupport::JSON.decode(SomeModel.new.to_json(options))
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should constraint to_json' do
|
48
|
-
as_hash.should == { "name" => "Any", "currency" => "USD", "id" => 1 }
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should allow further restrictions' do
|
52
|
-
as_hash(:only => :name).should == { "name" => "Any", "currency" => "USD" }
|
53
|
-
as_hash(:methods => []).should == { "name" => "Any", "id" => 1 }
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'should deny extensions if (default) scope is selected' do
|
57
|
-
as_hash(:only => [:id, :secret]).should == { "id" => 1, "currency" => "USD" }
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'should have separate behaviours for different scopes' do
|
61
|
-
as_hash(:scope => :admin).should == { "id" => 1, "secret" => "key" }
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'should fallback to default scope if invalid scope is given' do
|
65
|
-
as_hash(:scope => :invalid).should == { "name" => "Any", "currency" => "USD", "id" => 1 }
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|