serialization_scopes 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -2
- data/Gemfile.lock +26 -6
- data/Rakefile +4 -7
- data/VERSION +1 -1
- data/serialization_scopes.gemspec +4 -6
- data/spec/helper.rb +20 -6
- data/spec/serialization_scopes_spec.rb +7 -2
- metadata +7 -17
- data/lib/serialization_scopes/matchers/serialize_matcher.rb +0 -99
- data/lib/serialization_scopes/matchers.rb +0 -8
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,26 +1,46 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
|
5
|
-
activesupport (=
|
6
|
-
|
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)
|
7
18
|
gemcutter (0.6.1)
|
8
19
|
git (1.2.5)
|
20
|
+
i18n (0.4.1)
|
9
21
|
jeweler (1.4.0)
|
10
22
|
gemcutter (>= 0.1.0)
|
11
23
|
git (>= 1.2.5)
|
12
24
|
rubyforge (>= 2.0.0)
|
13
25
|
json_pure (1.4.6)
|
14
26
|
rake (0.8.7)
|
15
|
-
rspec (
|
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)
|
16
35
|
rubyforge (2.0.4)
|
17
36
|
json_pure (>= 1.1.7)
|
37
|
+
tzinfo (0.3.22)
|
18
38
|
|
19
39
|
PLATFORMS
|
20
40
|
ruby
|
21
41
|
|
22
42
|
DEPENDENCIES
|
23
|
-
activerecord (
|
43
|
+
activerecord (>= 3.0.0.rc)
|
24
44
|
jeweler
|
25
45
|
rake
|
26
|
-
rspec
|
46
|
+
rspec (>= 2.0.0.beta.17)
|
data/Rakefile
CHANGED
@@ -2,13 +2,10 @@ require 'bundler'
|
|
2
2
|
Bundler.setup
|
3
3
|
|
4
4
|
require 'rake'
|
5
|
-
require '
|
5
|
+
require 'rspec/mocks/version'
|
6
|
+
require 'rspec/core/rake_task'
|
6
7
|
|
7
|
-
|
8
|
-
Spec::Rake::SpecTask.new('spec') do |t|
|
9
|
-
t.spec_files = FileList['spec/**/*_spec.rb']
|
10
|
-
t.spec_opts = ["-c"]
|
11
|
-
end
|
8
|
+
RSpec::Core::RakeTask.new(:spec)
|
12
9
|
|
13
10
|
desc 'Default: run specs.'
|
14
11
|
task :default => :spec
|
@@ -22,7 +19,7 @@ begin
|
|
22
19
|
gemspec.email = "dimitrij@blacksquaremedia.com"
|
23
20
|
gemspec.homepage = "http://github.com/dim/serialization_scopes"
|
24
21
|
gemspec.authors = ["Dimitrij Denissenko"]
|
25
|
-
gemspec.add_runtime_dependency "activerecord", "
|
22
|
+
gemspec.add_runtime_dependency "activerecord", "> 2.5.0"
|
26
23
|
end
|
27
24
|
Jeweler::GemcutterTasks.new
|
28
25
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{serialization_scopes}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Dimitrij Denissenko"]
|
@@ -23,8 +23,6 @@ Gem::Specification.new do |s|
|
|
23
23
|
"Rakefile",
|
24
24
|
"VERSION",
|
25
25
|
"lib/serialization_scopes.rb",
|
26
|
-
"lib/serialization_scopes/matchers.rb",
|
27
|
-
"lib/serialization_scopes/matchers/serialize_matcher.rb",
|
28
26
|
"rails/init.rb",
|
29
27
|
"serialization_scopes.gemspec",
|
30
28
|
"spec/helper.rb",
|
@@ -45,12 +43,12 @@ Gem::Specification.new do |s|
|
|
45
43
|
s.specification_version = 3
|
46
44
|
|
47
45
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
-
s.add_runtime_dependency(%q<activerecord>, ["
|
46
|
+
s.add_runtime_dependency(%q<activerecord>, ["> 2.5.0"])
|
49
47
|
else
|
50
|
-
s.add_dependency(%q<activerecord>, ["
|
48
|
+
s.add_dependency(%q<activerecord>, ["> 2.5.0"])
|
51
49
|
end
|
52
50
|
else
|
53
|
-
s.add_dependency(%q<activerecord>, ["
|
51
|
+
s.add_dependency(%q<activerecord>, ["> 2.5.0"])
|
54
52
|
end
|
55
53
|
end
|
56
54
|
|
data/spec/helper.rb
CHANGED
@@ -1,12 +1,26 @@
|
|
1
1
|
ENV["RAILS_ENV"] ||= 'test'
|
2
|
-
|
2
|
+
$:.unshift File.dirname(__FILE__)
|
3
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
3
4
|
|
4
5
|
require 'rubygems'
|
5
|
-
require 'active_record'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
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'
|
10
21
|
|
11
|
-
|
22
|
+
RSpec.configure do |c|
|
23
|
+
c.mock_with :rspec
|
24
|
+
end
|
12
25
|
|
26
|
+
ActiveRecord::Base.include_root_in_json = false
|
@@ -2,9 +2,14 @@ require File.expand_path(File.dirname(__FILE__) + '/helper')
|
|
2
2
|
|
3
3
|
describe SerializationScopes do
|
4
4
|
|
5
|
+
before do
|
6
|
+
ActiveRecord::Base.stub!(:establish_connection)
|
7
|
+
end
|
8
|
+
|
5
9
|
class SomeModel < ActiveRecord::Base
|
6
10
|
serialization_scope :default, :only => [:id, :name], :methods => :currency
|
7
11
|
serialization_scope :admin, :only => [:id, :secret]
|
12
|
+
after_initialize :set_defaults
|
8
13
|
|
9
14
|
def self.columns
|
10
15
|
@columns ||= [
|
@@ -14,7 +19,7 @@ describe SerializationScopes do
|
|
14
19
|
]
|
15
20
|
end
|
16
21
|
|
17
|
-
def
|
22
|
+
def set_defaults
|
18
23
|
self.id = 1
|
19
24
|
self.name = 'Any'
|
20
25
|
self.secret = 'key'
|
@@ -28,8 +33,8 @@ describe SerializationScopes do
|
|
28
33
|
it 'should constraint to_xml' do
|
29
34
|
SomeModel.new.to_xml.should == %(<?xml version="1.0" encoding="UTF-8"?>
|
30
35
|
<some-model>
|
31
|
-
<id type="integer">1</id>
|
32
36
|
<name>Any</name>
|
37
|
+
<id type="integer">1</id>
|
33
38
|
<currency>USD</currency>
|
34
39
|
</some-model>
|
35
40
|
)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serialization_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 1
|
9
8
|
- 2
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dimitrij Denissenko
|
@@ -22,22 +22,14 @@ dependencies:
|
|
22
22
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
23
|
none: false
|
24
24
|
requirements:
|
25
|
-
- - "
|
25
|
+
- - ">"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
hash:
|
27
|
+
hash: 27
|
28
28
|
segments:
|
29
29
|
- 2
|
30
|
-
-
|
31
|
-
- 0
|
32
|
-
version: 2.3.0
|
33
|
-
- - <
|
34
|
-
- !ruby/object:Gem::Version
|
35
|
-
hash: 7
|
36
|
-
segments:
|
37
|
-
- 3
|
38
|
-
- 0
|
30
|
+
- 5
|
39
31
|
- 0
|
40
|
-
version:
|
32
|
+
version: 2.5.0
|
41
33
|
requirement: *id001
|
42
34
|
prerelease: false
|
43
35
|
type: :runtime
|
@@ -58,8 +50,6 @@ files:
|
|
58
50
|
- Rakefile
|
59
51
|
- VERSION
|
60
52
|
- lib/serialization_scopes.rb
|
61
|
-
- lib/serialization_scopes/matchers.rb
|
62
|
-
- lib/serialization_scopes/matchers/serialize_matcher.rb
|
63
53
|
- rails/init.rb
|
64
54
|
- serialization_scopes.gemspec
|
65
55
|
- spec/helper.rb
|
@@ -1,99 +0,0 @@
|
|
1
|
-
module SerializationScopes # :nodoc:
|
2
|
-
module Shoulda # :nodoc:
|
3
|
-
module Matchers
|
4
|
-
|
5
|
-
# Ensures that records are serialized correctly
|
6
|
-
#
|
7
|
-
# Example:
|
8
|
-
# describe Article do
|
9
|
-
# it { should serialize }
|
10
|
-
# it { should serialize.to(:article) }
|
11
|
-
# it { should serialize.to(:article).with_attributes(6) }
|
12
|
-
# it { should serialize.for(:reader).to(:article).with_attributes(4) }
|
13
|
-
# end
|
14
|
-
def serialize
|
15
|
-
SerializeMatcher.new
|
16
|
-
end
|
17
|
-
|
18
|
-
class SerializeMatcher
|
19
|
-
|
20
|
-
def for(scope)
|
21
|
-
@scope = scope
|
22
|
-
self
|
23
|
-
end
|
24
|
-
|
25
|
-
def to(value)
|
26
|
-
@to = value
|
27
|
-
self
|
28
|
-
end
|
29
|
-
|
30
|
-
def with_attributes(count)
|
31
|
-
@count = count
|
32
|
-
self
|
33
|
-
end
|
34
|
-
|
35
|
-
def matches?(subject)
|
36
|
-
@subject = subject
|
37
|
-
@subject.id ||= rand(10000000)
|
38
|
-
correct_root? && correct_column_count?
|
39
|
-
end
|
40
|
-
|
41
|
-
attr_reader :failure_message, :negative_failure_message
|
42
|
-
|
43
|
-
def description
|
44
|
-
result = "serialize"
|
45
|
-
result << " with #{@scope.inspect} scope" unless @scope.nil?
|
46
|
-
result << " to #{@to.inspect}" unless @to.nil?
|
47
|
-
result << " with #{@count} attributes" unless @count.nil?
|
48
|
-
result
|
49
|
-
end
|
50
|
-
|
51
|
-
def failure_message
|
52
|
-
"Expected #{expectation}"
|
53
|
-
end
|
54
|
-
|
55
|
-
def negative_failure_message
|
56
|
-
"Did not expect #{expectation}"
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def root_name
|
62
|
-
serialized.keys.first
|
63
|
-
end
|
64
|
-
|
65
|
-
def attributes
|
66
|
-
serialized[root_name] ? serialized[root_name].keys : []
|
67
|
-
end
|
68
|
-
|
69
|
-
def serialized
|
70
|
-
@serialized ||= ActiveSupport::JSON.decode(@subject.to_json(:scope => @scope))
|
71
|
-
end
|
72
|
-
|
73
|
-
def expectation
|
74
|
-
result = "to #{description}"
|
75
|
-
result << ", but #{failure_description}"
|
76
|
-
result
|
77
|
-
end
|
78
|
-
|
79
|
-
def correct_root?
|
80
|
-
@to.nil? || @to == root_name
|
81
|
-
end
|
82
|
-
|
83
|
-
def correct_column_count?
|
84
|
-
@count.nil? || @count == attributes.size
|
85
|
-
end
|
86
|
-
|
87
|
-
def failure_description
|
88
|
-
if !correct_root?
|
89
|
-
"root was #{root_name.inspect}"
|
90
|
-
elsif !correct_column_count?
|
91
|
-
"#{attributes.size} attributes were created (#{attributes.map(&:to_s).sort.join(', ')})"
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|