serialization_scopes 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +6 -0
- data/Rakefile +29 -0
- data/VERSION +1 -0
- data/lib/serialization_scopes.rb +33 -0
- data/rails/init.rb +4 -0
- data/serialization_scopes.gemspec +49 -0
- data/spec/helper.rb +12 -0
- data/spec/serialization_scopes_spec.rb +63 -0
- metadata +99 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require 'rake'
|
5
|
+
require 'spec/rake/spectask'
|
6
|
+
|
7
|
+
desc 'Run specs.'
|
8
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
9
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
10
|
+
t.spec_opts = ["-c"]
|
11
|
+
end
|
12
|
+
|
13
|
+
desc 'Default: run specs.'
|
14
|
+
task :default => :spec
|
15
|
+
|
16
|
+
begin
|
17
|
+
require 'jeweler'
|
18
|
+
Jeweler::Tasks.new do |gemspec|
|
19
|
+
gemspec.name = "serialization_scopes"
|
20
|
+
gemspec.summary = "Named scopes for ActiveRecord serialization methods (to_xml, to_json)"
|
21
|
+
gemspec.description = "Adds named scopes for ActiveRecord serialization methods (to_xml, to_json)"
|
22
|
+
gemspec.email = "dimitrij@blacksquaremedia.com"
|
23
|
+
gemspec.homepage = "http://github.com/dim/serialization_scopes"
|
24
|
+
gemspec.authors = ["Dimitrij Denissenko"]
|
25
|
+
gemspec.add_runtime_dependency "activerecord", ">= 2.3.0", '< 3.0.0'
|
26
|
+
end
|
27
|
+
Jeweler::GemcutterTasks.new
|
28
|
+
rescue LoadError
|
29
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module SerializationScopes
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.class_inheritable_reader :serialization_scopes
|
6
|
+
base.write_inheritable_attribute :serialization_scopes, {}
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def serialization_scope(name, options = {})
|
11
|
+
serialization_scopes[name.to_sym] = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def scoped_serialization_options(options = {})
|
15
|
+
name = options.delete(:scope)
|
16
|
+
scopes = name.present? && serialization_scopes[name.to_sym] ? serialization_scopes[name.to_sym] : serialization_scopes[:default]
|
17
|
+
scopes.each do |key, values|
|
18
|
+
options[key] ||= Array(values)
|
19
|
+
options[key] = Array(options[key]) & Array(values)
|
20
|
+
end if scopes
|
21
|
+
options
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_xml(options = {})
|
26
|
+
super self.class.scoped_serialization_options(options)
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_json(options = {})
|
30
|
+
super self.class.scoped_serialization_options(options)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,49 @@
|
|
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.1.0"
|
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.files = [
|
16
|
+
".gitignore",
|
17
|
+
"Gemfile",
|
18
|
+
"Rakefile",
|
19
|
+
"VERSION",
|
20
|
+
"lib/serialization_scopes.rb",
|
21
|
+
"rails/init.rb",
|
22
|
+
"serialization_scopes.gemspec",
|
23
|
+
"spec/helper.rb",
|
24
|
+
"spec/serialization_scopes_spec.rb"
|
25
|
+
]
|
26
|
+
s.homepage = %q{http://github.com/dim/serialization_scopes}
|
27
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
28
|
+
s.require_paths = ["lib"]
|
29
|
+
s.rubygems_version = %q{1.3.7}
|
30
|
+
s.summary = %q{Named scopes for ActiveRecord serialization methods (to_xml, to_json)}
|
31
|
+
s.test_files = [
|
32
|
+
"spec/helper.rb",
|
33
|
+
"spec/serialization_scopes_spec.rb"
|
34
|
+
]
|
35
|
+
|
36
|
+
if s.respond_to? :specification_version then
|
37
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
38
|
+
s.specification_version = 3
|
39
|
+
|
40
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
41
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 2.3.0", "< 3.0.0"])
|
42
|
+
else
|
43
|
+
s.add_dependency(%q<activerecord>, [">= 2.3.0", "< 3.0.0"])
|
44
|
+
end
|
45
|
+
else
|
46
|
+
s.add_dependency(%q<activerecord>, [">= 2.3.0", "< 3.0.0"])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
ENV["RAILS_ENV"] ||= 'test'
|
2
|
+
$: << File.join(File.dirname(__FILE__),'..', 'lib')
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'active_record'
|
6
|
+
|
7
|
+
require File.join(File.dirname(__FILE__),'..', 'rails', 'init.rb')
|
8
|
+
require 'spec/autorun'
|
9
|
+
require 'spec/mocks'
|
10
|
+
|
11
|
+
ActiveRecord::Base.stub(:establish_connection)
|
12
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
+
|
3
|
+
describe SerializationScopes do
|
4
|
+
|
5
|
+
class SomeModel < ActiveRecord::Base
|
6
|
+
serialization_scope :default, :only => [:id, :name], :methods => :currency
|
7
|
+
serialization_scope :admin, :only => [:id, :secret]
|
8
|
+
|
9
|
+
def self.columns
|
10
|
+
@columns ||= [
|
11
|
+
ActiveRecord::ConnectionAdapters::Column.new('id', nil, 'integer'),
|
12
|
+
ActiveRecord::ConnectionAdapters::Column.new('name', nil, 'string'),
|
13
|
+
ActiveRecord::ConnectionAdapters::Column.new('secret', nil, 'string')
|
14
|
+
]
|
15
|
+
end
|
16
|
+
|
17
|
+
def after_initialize
|
18
|
+
self.id = 1
|
19
|
+
self.name = 'Any'
|
20
|
+
self.secret = 'key'
|
21
|
+
end
|
22
|
+
|
23
|
+
def currency
|
24
|
+
'USD'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should constraint to_xml' do
|
29
|
+
SomeModel.new.to_xml.should == %(<?xml version="1.0" encoding="UTF-8"?>
|
30
|
+
<some-model>
|
31
|
+
<id type="integer">1</id>
|
32
|
+
<name>Any</name>
|
33
|
+
<currency>USD</currency>
|
34
|
+
</some-model>
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
def as_hash(options = {})
|
39
|
+
ActiveSupport::JSON.decode(SomeModel.new.to_json(options))
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should constraint to_json' do
|
43
|
+
as_hash.should == { "name" => "Any", "currency" => "USD", "id" => 1 }
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should allow further restrictions' do
|
47
|
+
as_hash(:only => :name).should == { "name" => "Any", "currency" => "USD" }
|
48
|
+
as_hash(:methods => []).should == { "name" => "Any", "id" => 1 }
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should deny extensions if (default) scope is selected' do
|
52
|
+
as_hash(:only => [:id, :secret]).should == { "id" => 1, "currency" => "USD" }
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should have separate behaviours for different scopes' do
|
56
|
+
as_hash(:scope => :admin).should == { "id" => 1, "secret" => "key" }
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should fallback to default scope if invalid scope is given' do
|
60
|
+
as_hash(:scope => :invalid).should == { "name" => "Any", "currency" => "USD", "id" => 1 }
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serialization_scopes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Dimitrij Denissenko
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-19 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
version: 2.3.0
|
33
|
+
- - <
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
hash: 7
|
36
|
+
segments:
|
37
|
+
- 3
|
38
|
+
- 0
|
39
|
+
- 0
|
40
|
+
version: 3.0.0
|
41
|
+
requirement: *id001
|
42
|
+
prerelease: false
|
43
|
+
type: :runtime
|
44
|
+
name: activerecord
|
45
|
+
description: Adds named scopes for ActiveRecord serialization methods (to_xml, to_json)
|
46
|
+
email: dimitrij@blacksquaremedia.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files: []
|
52
|
+
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- Rakefile
|
57
|
+
- VERSION
|
58
|
+
- lib/serialization_scopes.rb
|
59
|
+
- rails/init.rb
|
60
|
+
- serialization_scopes.gemspec
|
61
|
+
- spec/helper.rb
|
62
|
+
- spec/serialization_scopes_spec.rb
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: http://github.com/dim/serialization_scopes
|
65
|
+
licenses: []
|
66
|
+
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options:
|
69
|
+
- --charset=UTF-8
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.3.7
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Named scopes for ActiveRecord serialization methods (to_xml, to_json)
|
97
|
+
test_files:
|
98
|
+
- spec/helper.rb
|
99
|
+
- spec/serialization_scopes_spec.rb
|