basic_assumption 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ module BasicAssumption
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 5
5
- POINT = 1
5
+ POINT = 2
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{POINT}"
7
7
  end
8
8
  end
@@ -1,49 +1,65 @@
1
1
  require 'spec_helper'
2
2
  require 'basic_assumption/default_assumption/class_resolver'
3
3
 
4
- class ::ExampleClass
4
+ class BasicAssumption::DefaultAssumption::ExampleClass
5
5
  attr_reader :foo, :bar
6
+
6
7
  def initialize(foo=nil, bar=nil)
7
8
  @foo = foo; @bar = bar
8
9
  end
9
10
  end
10
11
 
11
- describe BasicAssumption::DefaultAssumption::ClassResolver do
12
- let(:resolver) { BasicAssumption::DefaultAssumption::ClassResolver.new(nil) }
13
- context '#camelize, given a symbol or string' do
14
- it 'returns an upper camel-cased string' do
15
- resolver.camelize('camel').should eql('Camel')
16
- end
17
- it 'treats underscores as word boundaries and removes them' do
18
- resolver.camelize('camel_case_word').should eql('CamelCaseWord')
19
- end
12
+ module Foo
13
+ class ExampleClass
20
14
  end
21
- context '#constantize' do
22
- it 'retrieves a constant from the BasicAssumption::DefaultAssumption namespace' do
23
- resolver.constantize('ClassResolver').should eql(BasicAssumption::DefaultAssumption::ClassResolver)
15
+ end
16
+
17
+ class TopLevelExampleClass
18
+ end
19
+
20
+ describe BasicAssumption::DefaultAssumption::ClassResolver do
21
+
22
+ context '#klass' do
23
+ it 'returns a class from the BasicAssumption::DefaultAssumption namespace' do
24
+ resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:example_class, 'BasicAssumption::DefaultAssumption')
25
+ resolver.klass.should equal(BasicAssumption::DefaultAssumption::ExampleClass)
24
26
  end
25
- it 'raises an error if no such constant is found' do
26
- expect { resolver.constantize('CamelCase') }.to raise_error(NameError)
27
+
28
+ context "specifying a namespace" do
29
+ it 'returns a class from that namespace' do
30
+ resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:example_class, 'Foo')
31
+ resolver.klass.should equal(Foo::ExampleClass)
32
+ end
33
+
34
+ it 'returns a class from the top-level namespace by default' do
35
+ resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:top_level_example_class)
36
+ resolver.klass.should equal(TopLevelExampleClass)
37
+ end
27
38
  end
28
39
  end
40
+
29
41
  context '#instance' do
30
- before(:each) do
31
- @resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:example_class)
32
- @resolver.stub!(:constantize).and_return(ExampleClass)
33
- end
34
- it 'returns an instance of the resolved class' do
35
- @resolver.instance.should be_a_kind_of(ExampleClass)
42
+
43
+ it 'returns a class instance from the BasicAssumption::DefaultAssumption namespace' do
44
+ resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:example_class, 'BasicAssumption::DefaultAssumption')
45
+ resolver.instance.should be_a_kind_of(BasicAssumption::DefaultAssumption::ExampleClass)
36
46
  end
37
- it 'accepts and passes on any arguments to the instantiation of the object' do
38
- example_object = @resolver.instance('foo', 'bar')
39
- example_object.foo.should eql('foo')
40
- example_object.bar.should eql('bar')
47
+
48
+ it 'raises an error if no such constant is found' do
49
+ resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:no_such_class)
50
+ expect { resolver.instance }.to raise_error(NameError)
41
51
  end
42
- end
43
- context '::instance' do
44
- it 'returns an instance of the resolved class' do
45
- mod = BasicAssumption::DefaultAssumption
46
- mod::ClassResolver.instance(:base).should be_an_instance_of(mod::Base)
52
+
53
+ context "specifying a namespace" do
54
+ it 'returns a class instance from that namespace' do
55
+ resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:example_class, 'Foo')
56
+ resolver.instance.should be_a_kind_of(Foo::ExampleClass)
57
+ end
58
+
59
+ it 'returns a class instance from the top-level namespace' do
60
+ resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:top_level_example_class)
61
+ resolver.instance.should be_a_kind_of(TopLevelExampleClass)
62
+ end
47
63
  end
48
64
  end
49
65
  end
@@ -2,45 +2,202 @@ require 'spec_helper'
2
2
  require 'active_support'
3
3
  require 'basic_assumption/default_assumption/rails'
4
4
 
5
- class Model; end
5
+ class Model
6
+ attr_accessor :age, :color, :owner_id
7
+ def initialize(hash = {})
8
+ hash.each do |k, v|
9
+ self.send("#{k}=", v)
10
+ end
11
+ end
12
+ end
6
13
 
7
14
  describe BasicAssumption::DefaultAssumption::Rails do
8
15
 
16
+ let(:default) { BasicAssumption::DefaultAssumption::Rails.new(:model, {}, request) }
17
+ let(:request) { stub(:params => params, :get? => true) }
18
+ let(:params) { {'id' => 42} }
19
+
9
20
  context "#block" do
10
- let(:default) { BasicAssumption::DefaultAssumption::Rails.new }
11
- let(:params) { stub(:[] => 42) }
21
+ let(:relation) { stub("where", :find => nil) }
12
22
 
13
23
  before(:each) do
14
- Model.stub!(:find)
15
- default.stub!(:params).and_return(params)
24
+ Model.stub!(:where).and_return(relation)
16
25
  end
17
26
 
18
- context "when context[:find_on_id] is true" do
19
- it "looks for a params[model_id] and params[id] in its calling context" do
20
- params.should_receive(:[]).with('model_id').and_return(nil)
21
- params.should_receive(:[]).with('id')
22
- default.block.call(:model, {:find_on_id => true})
27
+ it "attempts to find a model instance based off the given name" do
28
+ relation.should_receive(:find).with(42).and_return(:model)
29
+ BasicAssumption::DefaultAssumption::Rails.new(:model, {}, request).result.should eql(:model)
30
+ end
31
+
32
+ context "when passed an alternative model name" do
33
+ it "finds a model instance based off the alternative name" do
34
+ relation.should_receive(:find).with(42).and_return(:model)
35
+ BasicAssumption::DefaultAssumption::Rails.new(:my_model, {:as => :model}, request).result.should eql(:model)
23
36
  end
24
37
  end
25
38
 
26
- context "when context[:find_on_id] is not true" do
27
- it "looks for a params[model_id] in its calling context" do
28
- params.should_receive(:[]).with('model_id').and_return(nil)
29
- params.should_not_receive(:[]).with('id')
30
- default.block.call(:model, {:find_on_id => nil})
39
+ context "when the name given to assume is plural" do
40
+ let(:name) { :models }
41
+
42
+ %w(create destroy edit index new show update).each do |action|
43
+ let(:params) { {'action' => action} }
44
+
45
+ context "when action is #{action}" do
46
+ it "finds all the records of the model class" do
47
+ Model.should_receive(:where)
48
+ default.block.call(name, {})
49
+ end
50
+ end
31
51
  end
52
+
32
53
  end
33
54
 
34
- it "attempts to find a model instance based off the given name" do
35
- Model.should_receive(:find).with(42).and_return(:model)
36
- default.block.call(:model, {}).should eql(:model)
55
+ context "when the name given to assume is singular" do
56
+ let(:name) { :model }
57
+
58
+ context "in the index action" do
59
+ let(:params) { { 'action' => 'index' } }
60
+
61
+ before { params['model'] = {:initializer => 'value'} }
62
+ it "creates a new model instance and passes in appropriate params" do
63
+ Model.should_receive(:new).with({:initializer => 'value'}).and_return(name)
64
+ default.block.call(name, {}).should eql(name)
65
+ end
66
+ end
67
+
68
+ context "in the show action" do
69
+ let(:params) { { 'id' => 42, 'action' => 'show' } }
70
+
71
+ it "attempts to find a model instance based off the given name" do
72
+ relation.should_receive(:find).with(42).and_return(name)
73
+ default.block.call(name, {}).should eql(:model)
74
+ end
75
+ end
76
+
77
+ context "in the edit action" do
78
+ let(:params) { { 'id' => 42, 'action' => 'edit' } }
79
+ let(:model) { stub("model", :attributes= => {}) }
80
+
81
+ before { relation.should_receive(:find).with(42).and_return(model) }
82
+
83
+ it "attempts to find a model instance based off the given name" do
84
+ default.block.call(name, {}).should eql(model)
85
+ end
86
+
87
+ it "sets the updated attributes" do
88
+ model.should_receive(:attributes=).once
89
+ default.block.call(name, {})
90
+ end
91
+ end
92
+
93
+ context "in the update action" do
94
+ let(:params) { {'id' => 42, 'action' => 'update'} }
95
+ let(:model) { stub("model", :attributes= => {}) }
96
+
97
+ before { relation.should_receive(:find).with(42).and_return(model) }
98
+
99
+ it "attempts to find a model instance based off the given name" do
100
+ default.block.call(name, {}).should eql(model)
101
+ end
102
+
103
+ it "sets the updated attributes" do
104
+ model.should_receive(:attributes=).once
105
+ default.block.call(name, {})
106
+ end
107
+ end
108
+
109
+ context "in the destroy action" do
110
+ let(:params) { { 'id' => 42, 'action' => 'destroy' } }
111
+
112
+ it "attempts to find a model instance based off the given name" do
113
+ relation.should_receive(:find).with(42).and_return(name)
114
+ default.block.call(name, {}).should eql(:model)
115
+ end
116
+ end
117
+
118
+ context "in the create action" do
119
+ let(:params) do
120
+ {
121
+ 'action' => 'create',
122
+ 'model' => { 'age' => 27, 'color' => 'blue' }
123
+ }
124
+ end
125
+
126
+ context "the model instance" do
127
+ subject { default.block.call(name, {}) }
128
+ its(:age) { should be(27) }
129
+ its(:color) { should eql('blue') }
130
+ end
131
+ end
132
+
133
+ context "in the new action" do
134
+ let(:params) do
135
+ {
136
+ 'action' => 'new',
137
+ 'model' => { 'age' => 27, 'color' => 'blue' }
138
+ }
139
+ end
140
+
141
+ context "the model instance" do
142
+ subject { default.block.call(name, {}) }
143
+ its(:age) { should be(27) }
144
+ its(:color) { should eql('blue') }
145
+ end
146
+ end
37
147
  end
148
+ end
38
149
 
39
- context "when passed an alternative model name" do
40
- it "finds a model instance based off the alternative name" do
41
- Model.should_receive(:find).with(42).and_return(:model)
42
- default.block.call(:my_model, {:as => :model}).should eql(:model)
150
+ context "ensuring ownership" do
151
+ class User
152
+ def id
153
+ 5678
154
+ end
155
+ end
156
+
157
+ class Controller
158
+ def owner
159
+ User.new
160
+ end
161
+ end
162
+
163
+ let(:params) { {'id' => 123} }
164
+
165
+ it "accepts a symbol" do
166
+ Model.should_receive(:where).with(:user_id => 5678).and_return(stub(:find => nil))
167
+
168
+ default = BasicAssumption::DefaultAssumption::Rails.new(:model, {:owner => :owner, :controller => Controller.new}, request)
169
+
170
+ default.result
171
+ end
172
+
173
+ it "accepts a proc" do
174
+ Model.should_receive(:where).with(:user_id => 5678).and_return(stub(:find => nil))
175
+
176
+ default = BasicAssumption::DefaultAssumption::Rails.new(:model, {:owner => proc { owner }, :controller => Controller.new}, request)
177
+
178
+ default.result
179
+ end
180
+
181
+ context "with a specified column name" do
182
+
183
+ it "obeys the column name" do
184
+ Model.should_receive(:where).with(:user_id => 5678).and_return(stub(:find => nil))
185
+
186
+ owner_context = {:column_name => :user_id, :object => proc { owner }}
187
+ default = BasicAssumption::DefaultAssumption::Rails.new(:model, {:owner => owner_context, :controller => Controller.new}, request)
188
+
189
+ default.result
190
+ end
191
+
192
+ it "obeys the column name given a proc" do
193
+ Model.should_receive(:where).with(:owner_id => 5678).and_return(stub(:find => nil))
194
+
195
+ owner_context = {:column_name => :owner_id, :object => :owner}
196
+ default = BasicAssumption::DefaultAssumption::Rails.new(:model, {:owner => owner_context, :controller => Controller.new}, request)
197
+
198
+ default.result
43
199
  end
44
200
  end
45
201
  end
202
+
46
203
  end
@@ -185,9 +185,11 @@ describe BasicAssumption do
185
185
  controller_class.class_eval do
186
186
  assume(:model)
187
187
  end
188
- controller_instance.stub(:params => {'model_id' => 123})
189
- ::Model.should_receive(:find).with(123)
190
- controller_instance.model.should be_a_kind_of(Object)
188
+ controller_instance.stub(:request => stub(:params => {'id' => 123}, :get? => true))
189
+ relation = stub("where")
190
+ relation.should_receive(:find).with(123)
191
+ ::Model.stub(:where => relation)
192
+ controller_instance.model
191
193
  end
192
194
  end
193
195
  end
metadata CHANGED
@@ -1,30 +1,40 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: basic_assumption
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.5.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.2
6
5
  platform: ruby
7
- authors:
6
+ authors:
8
7
  - Matt Yoho
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
-
13
- date: 2011-10-05 00:00:00 -04:00
14
- default_executable:
15
- dependencies: []
16
-
17
- description: "\n Allows a simple declarative idiom for accessing resources in controllers and views\n via a well-defined interface that increases testability and reduces shared state.\n "
11
+ date: 2015-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: "\n Allows a simple declarative idiom for accessing resources in controllers
28
+ and views\n via a well-defined interface that increases testability and reduces
29
+ shared state.\n "
18
30
  email: mby@mattyoho.com
19
31
  executables: []
20
-
21
32
  extensions: []
22
-
23
- extra_rdoc_files:
33
+ extra_rdoc_files:
24
34
  - HISTORY.rdoc
25
35
  - MIT-LICENSE
26
36
  - README.rdoc
27
- files:
37
+ files:
28
38
  - HISTORY.rdoc
29
39
  - MIT-LICENSE
30
40
  - README.rdoc
@@ -32,51 +42,51 @@ files:
32
42
  - lib/basic_assumption/configuration.rb
33
43
  - lib/basic_assumption/configuration/active_record.rb
34
44
  - lib/basic_assumption/default_assumption.rb
45
+ - lib/basic_assumption/default_assumption/action.rb
35
46
  - lib/basic_assumption/default_assumption/base.rb
36
47
  - lib/basic_assumption/default_assumption/class_resolver.rb
48
+ - lib/basic_assumption/default_assumption/name.rb
49
+ - lib/basic_assumption/default_assumption/owner_attributes.rb
37
50
  - lib/basic_assumption/default_assumption/rails.rb
38
- - lib/basic_assumption/default_assumption/restful_rails.rb
39
51
  - lib/basic_assumption/rails.rb
40
52
  - lib/basic_assumption/rspec.rb
41
53
  - lib/basic_assumption/version.rb
42
54
  - rails/init.rb
43
- - spec/spec_helper.rb
44
- - spec/basic_assumption_spec.rb
45
55
  - spec/basic_assumption/configuration_spec.rb
46
- - spec/basic_assumption/default_assumption_spec.rb
47
56
  - spec/basic_assumption/default_assumption/base_spec.rb
48
57
  - spec/basic_assumption/default_assumption/class_resolver_spec.rb
49
58
  - spec/basic_assumption/default_assumption/rails_spec.rb
50
- - spec/basic_assumption/default_assumption/restful_rails_spec.rb
51
- has_rdoc: true
59
+ - spec/basic_assumption/default_assumption_spec.rb
60
+ - spec/basic_assumption_spec.rb
61
+ - spec/spec_helper.rb
52
62
  homepage: http://github.com/mattyoho/basic_assumption
53
63
  licenses: []
54
-
64
+ metadata: {}
55
65
  post_install_message:
56
- rdoc_options:
57
- - --charset=UTF-8
58
- require_paths:
66
+ rdoc_options:
67
+ - "--charset=UTF-8"
68
+ require_paths:
59
69
  - lib
60
- required_ruby_version: !ruby/object:Gem::Requirement
61
- none: false
62
- requirements:
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
63
72
  - - ">="
64
- - !ruby/object:Gem::Version
65
- version: "0"
66
- required_rubygems_version: !ruby/object:Gem::Requirement
67
- none: false
68
- requirements:
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
69
77
  - - ">="
70
- - !ruby/object:Gem::Version
71
- version: "0"
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
72
80
  requirements: []
73
-
74
81
  rubyforge_project:
75
- rubygems_version: 1.6.2
82
+ rubygems_version: 2.2.3
76
83
  signing_key:
77
- specification_version: 3
78
- summary: Allows a simple declarative idiom for accessing resources in controllers and views, cleaning up controller code and removing the need to explicitly reference instance variables inside views. Custom default behavior can be defined in a pluggable manner.
79
- test_files:
84
+ specification_version: 4
85
+ summary: Allows a simple declarative idiom for accessing resources in controllers
86
+ and views, cleaning up controller code and removing the need to explicitly reference
87
+ instance variables inside views. Custom default behavior can be defined in a pluggable
88
+ manner.
89
+ test_files:
80
90
  - spec/spec_helper.rb
81
91
  - spec/basic_assumption_spec.rb
82
92
  - spec/basic_assumption/configuration_spec.rb
@@ -84,4 +94,3 @@ test_files:
84
94
  - spec/basic_assumption/default_assumption/base_spec.rb
85
95
  - spec/basic_assumption/default_assumption/class_resolver_spec.rb
86
96
  - spec/basic_assumption/default_assumption/rails_spec.rb
87
- - spec/basic_assumption/default_assumption/restful_rails_spec.rb