objectify 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.document +5 -0
  2. data/.gitignore +47 -2
  3. data/.rspec +1 -0
  4. data/Gemfile +1 -1
  5. data/Gemfile.lock +105 -16
  6. data/LICENSE.txt +20 -0
  7. data/README.md +230 -0
  8. data/Rakefile +14 -7
  9. data/lib/objectify.rb +1 -26
  10. data/lib/objectify/config/action.rb +31 -0
  11. data/lib/objectify/config/context.rb +97 -0
  12. data/lib/objectify/config/policies.rb +18 -0
  13. data/lib/objectify/executor.rb +21 -0
  14. data/lib/objectify/injector.rb +39 -0
  15. data/lib/objectify/instantiator.rb +14 -0
  16. data/lib/objectify/instrumentation.rb +12 -0
  17. data/lib/objectify/logging.rb +22 -0
  18. data/lib/objectify/policy_chain_executor.rb +27 -0
  19. data/lib/objectify/rails/application.rb +16 -0
  20. data/lib/objectify/rails/controller.rb +117 -0
  21. data/lib/objectify/rails/helpers.rb +14 -0
  22. data/lib/objectify/rails/log_subscriber.rb +59 -0
  23. data/lib/objectify/rails/railtie.rb +11 -0
  24. data/lib/objectify/rails/renderer.rb +47 -0
  25. data/lib/objectify/rails/routing.rb +97 -0
  26. data/lib/objectify/resolver.rb +27 -0
  27. data/lib/objectify/resolver_locator.rb +73 -0
  28. data/lib/objectify/route.rb +4 -0
  29. data/lib/objectify/version.rb +1 -1
  30. data/objectify.gemspec +13 -8
  31. data/spec/config/action_spec.rb +79 -0
  32. data/spec/config/context_spec.rb +137 -0
  33. data/spec/config/policies_spec.rb +23 -0
  34. data/spec/executor_spec.rb +47 -0
  35. data/spec/injector_spec.rb +88 -0
  36. data/spec/instantiator_spec.rb +22 -0
  37. data/spec/policy_chain_executor_spec.rb +60 -0
  38. data/spec/rails/renderer_spec.rb +63 -0
  39. data/spec/rails/routing_spec.rb +171 -0
  40. data/spec/resolver_locator_spec.rb +109 -0
  41. data/spec/resolver_spec.rb +28 -0
  42. data/spec/route_spec.rb +11 -0
  43. data/spec/spec_helper.rb +13 -5
  44. metadata +142 -62
  45. data/spec/objectify_spec.rb +0 -60
@@ -0,0 +1,171 @@
1
+ require "spec_helper"
2
+ require "objectify/rails/routing"
3
+
4
+ describe "Objectify::Rails::Routing::ObjectifyMapper" do
5
+ before do
6
+ @policies = stub("Policies")
7
+ @objectify = stub("Objectify", :append_policy_responders => nil,
8
+ :append_defaults => nil,
9
+ :append_action => nil,
10
+ :policies => @policies,
11
+ :append_resolutions => nil,
12
+ :objectify_controller => "some_controller")
13
+ @rails_mapper = stub("RailsMapper", :resources => nil,
14
+ :match => nil)
15
+ @application = stub("Application", :objectify => @objectify)
16
+ @action = stub("Action")
17
+ @action_factory = stub("ActionFactory", :new => @action)
18
+ klass = Objectify::Rails::Routing::ObjectifyMapper
19
+ @mapper = klass.new(@rails_mapper,
20
+ @application,
21
+ @action_factory)
22
+ end
23
+
24
+ context "adding a resource" do
25
+ before do
26
+ @mapper.resources(:pictures, :policies => :some_policy,
27
+ :create => {
28
+ :policies => :blocked_user,
29
+ :service => :picture_creation_v2
30
+ })
31
+ end
32
+
33
+ it "correctly adds the resource to the rails mapper" do
34
+ opts = { :controller => @objectify.objectify_controller,
35
+ :defaults => {:objectify => {:resource => :pictures}}}
36
+ @rails_mapper.should have_received(:resources).
37
+ with(:pictures, opts)
38
+ end
39
+
40
+ it "creates an action for each RESOURCE_ACTIONS" do
41
+ opts = {
42
+ :create => {
43
+ :policies => :blocked_user,
44
+ :service => :picture_creation_v2
45
+ },
46
+ :policies => :some_policy
47
+ }
48
+ Objectify::Rails::Routing::RESOURCE_ACTIONS.each do |action|
49
+ @action_factory.should have_received(:new).
50
+ with(:pictures, action, opts, @policies)
51
+ end
52
+ end
53
+
54
+ it "appends each of the actions to the objectify object" do
55
+ @objectify.should have_received(:append_action).
56
+ with(@action).times(7)
57
+ end
58
+ end
59
+
60
+ context "adding a resource with its own defaults" do
61
+ before do
62
+ @mapper.resources(:pictures, :policies => :some_policy,
63
+ :create => {
64
+ :policies => :blocked_user,
65
+ :service => :picture_creation_v2
66
+ },
67
+ :defaults => {:some => :bullshit})
68
+ end
69
+
70
+ it "correctly adds the resource to the rails mapper" do
71
+ opts = { :controller => @objectify.objectify_controller,
72
+ :defaults => {:objectify => {:resource => :pictures},
73
+ :some => :bullshit}}
74
+ @rails_mapper.should have_received(:resources).
75
+ with(:pictures, opts)
76
+ end
77
+
78
+ it "creates an action for each RESOURCE_ACTIONS" do
79
+ opts = {
80
+ :create => {
81
+ :policies => :blocked_user,
82
+ :service => :picture_creation_v2
83
+ },
84
+ :policies => :some_policy
85
+ }
86
+ Objectify::Rails::Routing::RESOURCE_ACTIONS.each do |action|
87
+ @action_factory.should have_received(:new).
88
+ with(:pictures, action, opts, @policies)
89
+ end
90
+ end
91
+
92
+ it "appends each of the actions to the objectify object" do
93
+ @objectify.should have_received(:append_action).
94
+ with(@action).times(7)
95
+ end
96
+ end
97
+
98
+ context "adding a resource with an alternative objectify_controller" do
99
+ before do
100
+ @objectify.stubs(:objectify_controller).returns("asdfbsdf")
101
+ @mapper.resources(:pictures, :policies => :some_policy,
102
+ :create => {
103
+ :policies => :blocked_user,
104
+ :service => :picture_creation_v2
105
+ },
106
+ :defaults => {:some => :bullshit})
107
+ end
108
+
109
+ it "correctly adds the resource to the rails mapper" do
110
+ opts = { :controller => @objectify.objectify_controller,
111
+ :defaults => {:objectify => {:resource => :pictures},
112
+ :some => :bullshit}}
113
+ @rails_mapper.should have_received(:resources).
114
+ with(:pictures, opts)
115
+ end
116
+ end
117
+
118
+ context "setting defaults" do
119
+ before do
120
+ @opts = { :policies => :fuckitttttt }
121
+ @mapper.defaults @opts
122
+ end
123
+
124
+ it "hands the defaults to the objectify context" do
125
+ @objectify.should have_received(:append_defaults).with(@opts)
126
+ end
127
+ end
128
+
129
+ context "setting policy responders" do
130
+ before do
131
+ @opts = { :authenticated => :unauthenticated_responder }
132
+ @mapper.policy_responders @opts
133
+ end
134
+
135
+ it "hands the policy responders to the objectify context" do
136
+ @objectify.should have_received(:append_policy_responders).
137
+ with(@opts)
138
+ end
139
+ end
140
+
141
+ context "adding resolutions" do
142
+ before do
143
+ @opts = { :authenticated => :unauthenticated_responder }
144
+ @mapper.resolutions @opts
145
+ end
146
+
147
+ it "hands them to the context" do
148
+ @objectify.should have_received(:append_resolutions).
149
+ with(@opts)
150
+ end
151
+ end
152
+
153
+ context "adding a legacy action" do
154
+ before do
155
+ @opts = {:policies => "asdf"}
156
+ @mapper.legacy_action :controller, [:action1, :action2], @opts
157
+ end
158
+
159
+ it "creates actions for each of the specified actions" do
160
+ @action_factory.should have_received(:new).
161
+ with(:controller, :action1, @opts, @policies)
162
+
163
+ @action_factory.should have_received(:new).
164
+ with(:controller, :action2, @opts, @policies)
165
+ end
166
+
167
+ it "passes the actions to the objectify objects" do
168
+ @objectify.should have_received(:append_action).with(@action).twice
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,109 @@
1
+ require "spec_helper"
2
+ require "objectify/resolver_locator"
3
+
4
+ describe "Objectify::ArrayResolverLocator" do
5
+ before do
6
+ @locator_a = stub("Resolver", :name => :a)
7
+ @locator_b = stub("Resolver", :name => :b)
8
+ @resolvers = [@locator_a, @locator_b]
9
+ @resolver_locator = Objectify::ArrayResolverLocator.new(@resolvers)
10
+ end
11
+
12
+ it "returns locators by name" do
13
+ @resolver_locator.call(:a).should == @locator_a
14
+ @resolver_locator.call(:b).should == @locator_b
15
+ end
16
+ end
17
+
18
+ describe "Objectify::MultiResolverLocator" do
19
+ before do
20
+ @locator_a = stub("LocatorA", :call => nil)
21
+ @locator_a.stubs(:call).with(:a).returns(:resolver_a)
22
+ @locator_b = stub("LocatorB", :call => nil)
23
+ @locator_b.stubs(:call).with(:b).returns(:resolver_b)
24
+
25
+ @locators = [@locator_a, @locator_b]
26
+ @resolver_locator = Objectify::MultiResolverLocator.new(@locators)
27
+ end
28
+
29
+ it "searches through its locators for a resolver" do
30
+ @resolver_locator.call(:a).should == :resolver_a
31
+ @resolver_locator.call(:b).should == :resolver_b
32
+ end
33
+
34
+ it "raises ArgumentError when a resolver can't be found" do
35
+ not_found = lambda { @resolver_locator.call(:asdf) }
36
+ not_found.should raise_error(ArgumentError)
37
+ end
38
+
39
+ it "accepts a temporary, contextual locator which takes priority" do
40
+ @temp_locator = stub("TempLocator", :call => :temp_resolver)
41
+ @resolver_locator.context(@temp_locator)
42
+ @resolver_locator.call(:a).should == :temp_resolver
43
+ @resolver_locator.clear_context
44
+ @resolver_locator.call(:a).should == :resolver_a
45
+ end
46
+ end
47
+
48
+ describe "Objectify::NamedValueResolverLocator" do
49
+ before do
50
+ @resolver = stub("Resolver")
51
+ @factory = stub("ResolverFactory", :new => @resolver)
52
+ @locator = Objectify::NamedValueResolverLocator.new(@factory)
53
+ end
54
+
55
+ it "#call returns the per-name resolver" do
56
+ @locator.add(:name, :value)
57
+ @locator.call(:name).should == @resolver
58
+ end
59
+
60
+ it "#add creates a resolver for the supplied key/value pair" do
61
+ @locator.add(:name, :value)
62
+ @factory.should have_received(:new).with(:name, :value)
63
+ end
64
+ end
65
+
66
+ class MyResolver; end
67
+ module Something; class MyResolver; end; end
68
+
69
+ describe "Objectify::ConstResolverLocator" do
70
+ context "in the global namespace" do
71
+ before do
72
+ @locator = Objectify::ConstResolverLocator.new
73
+ end
74
+
75
+ it "finds resolvers by const name" do
76
+ @locator.call(:my).should be_instance_of(MyResolver)
77
+ end
78
+
79
+ it "returns nil if the const is missing" do
80
+ @locator.call(:missing).should be_nil
81
+ end
82
+
83
+ it "keeps a cache of instantiated resolvers" do
84
+ obj1 = @locator.call(:my)
85
+ obj2 = @locator.call(:my)
86
+ obj1.object_id.should == obj2.object_id
87
+ end
88
+ end
89
+
90
+ context "in an arbitrary namespace" do
91
+ before do
92
+ @locator = Objectify::ConstResolverLocator.new("something")
93
+ end
94
+
95
+ it "finds resolvers by const name" do
96
+ @locator.call(:my).should be_instance_of(Something::MyResolver)
97
+ end
98
+
99
+ it "returns nil if the const is missing" do
100
+ @locator.call(:missing).should be_nil
101
+ end
102
+
103
+ it "keeps a cache of instantiated resolvers" do
104
+ obj1 = @locator.call(:my)
105
+ obj2 = @locator.call(:my)
106
+ obj1.object_id.should == obj2.object_id
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+ require "objectify/resolver"
3
+
4
+ describe "Objectify::NamedValueResolver" do
5
+ before do
6
+ @named_value_resolver = Objectify::NamedValueResolver.new(:name, :value)
7
+ end
8
+
9
+ it "returns the supplied name" do
10
+ @named_value_resolver.name.should == :name
11
+ end
12
+
13
+ it "returns the supplied value" do
14
+ @named_value_resolver.call.should == :value
15
+ end
16
+ end
17
+
18
+ class MyResolver; end
19
+
20
+ describe "Objectify::NameTranslationResolver" do
21
+ before do
22
+ @resolver = Objectify::NameTranslationResolver.new(:a, :my_resolver)
23
+ end
24
+
25
+ it "instantiates resolvers" do
26
+ @resolver.call.should be_instance_of(MyResolver)
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+ require "objectify/route"
3
+
4
+ describe "Objectify::Route" do
5
+ context "two routes with the same path" do
6
+ it "have the same #hash value" do
7
+ Objectify::Route.new(:pictures, :index).should ==
8
+ Objectify::Route.new(:pictures, :index)
9
+ end
10
+ end
11
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,15 @@
1
- Dir["support/**/*.rb"].each {|f| require f }
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require "rubygems"
5
+ require "rspec"
6
+ require "mocha"
7
+ require "bourne"
2
8
 
3
- if defined? RSpec
4
- RSpec.configure do |config|
5
- config.mock_with :rspec
6
- end
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
+
13
+ RSpec.configure do |config|
14
+ config.mock_with :mocha
7
15
  end
metadata CHANGED
@@ -1,98 +1,178 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: objectify
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
4
5
  prerelease:
5
- version: 0.0.3
6
6
  platform: ruby
7
- authors:
8
- - Andrew Kiellor
7
+ authors:
8
+ - James Golick
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-03-14 00:00:00 +11:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
17
- name: activesupport
12
+ date: 2012-05-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70192971750460 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.4.0
22
+ type: :development
18
23
  prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: *70192971750460
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70192971705480 !ruby/object:Gem::Requirement
20
28
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
24
- version: "2.3"
25
- type: :runtime
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70192971705480
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &70192970967320 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.6.4
44
+ type: :development
29
45
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
46
+ version_requirements: *70192970967320
47
+ - !ruby/object:Gem::Dependency
48
+ name: bourne
49
+ requirement: &70192970924140 !ruby/object:Gem::Requirement
31
50
  none: false
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
35
- version: "0"
51
+ requirements:
52
+ - - =
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
36
55
  type: :development
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
39
- name: rake
40
56
  prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
57
+ version_requirements: *70192970924140
58
+ - !ruby/object:Gem::Dependency
59
+ name: mocha
60
+ requirement: &70192970892180 !ruby/object:Gem::Requirement
42
61
  none: false
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: "0"
62
+ requirements:
63
+ - - =
64
+ - !ruby/object:Gem::Version
65
+ version: 0.9.8
47
66
  type: :development
48
- version_requirements: *id003
49
- description: A simple gem to make hashes and arrays of hashes behave like objects using proxies.
50
- email:
51
- - akiellor@gmail.com
67
+ prerelease: false
68
+ version_requirements: *70192970892180
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: &70192970864040 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: 3.0.0
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70192970864040
80
+ - !ruby/object:Gem::Dependency
81
+ name: i18n
82
+ requirement: &70192969536640 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: *70192969536640
91
+ description: ''
92
+ email:
93
+ - jamesgolick@gmail.com
52
94
  executables: []
53
-
54
95
  extensions: []
55
-
56
96
  extra_rdoc_files: []
57
-
58
- files:
97
+ files:
98
+ - .document
59
99
  - .gitignore
100
+ - .rspec
60
101
  - Gemfile
61
102
  - Gemfile.lock
103
+ - LICENSE.txt
104
+ - README.md
62
105
  - Rakefile
63
106
  - lib/objectify.rb
107
+ - lib/objectify/config/action.rb
108
+ - lib/objectify/config/context.rb
109
+ - lib/objectify/config/policies.rb
110
+ - lib/objectify/executor.rb
111
+ - lib/objectify/injector.rb
112
+ - lib/objectify/instantiator.rb
113
+ - lib/objectify/instrumentation.rb
114
+ - lib/objectify/logging.rb
115
+ - lib/objectify/policy_chain_executor.rb
116
+ - lib/objectify/rails/application.rb
117
+ - lib/objectify/rails/controller.rb
118
+ - lib/objectify/rails/helpers.rb
119
+ - lib/objectify/rails/log_subscriber.rb
120
+ - lib/objectify/rails/railtie.rb
121
+ - lib/objectify/rails/renderer.rb
122
+ - lib/objectify/rails/routing.rb
123
+ - lib/objectify/resolver.rb
124
+ - lib/objectify/resolver_locator.rb
125
+ - lib/objectify/route.rb
64
126
  - lib/objectify/version.rb
65
127
  - objectify.gemspec
66
- - spec/objectify_spec.rb
128
+ - spec/config/action_spec.rb
129
+ - spec/config/context_spec.rb
130
+ - spec/config/policies_spec.rb
131
+ - spec/executor_spec.rb
132
+ - spec/injector_spec.rb
133
+ - spec/instantiator_spec.rb
134
+ - spec/policy_chain_executor_spec.rb
135
+ - spec/rails/renderer_spec.rb
136
+ - spec/rails/routing_spec.rb
137
+ - spec/resolver_locator_spec.rb
138
+ - spec/resolver_spec.rb
139
+ - spec/route_spec.rb
67
140
  - spec/spec_helper.rb
68
- has_rdoc: true
69
- homepage: ""
141
+ homepage: ''
70
142
  licenses: []
71
-
72
143
  post_install_message:
73
144
  rdoc_options: []
74
-
75
- require_paths:
145
+ require_paths:
76
146
  - lib
77
- required_ruby_version: !ruby/object:Gem::Requirement
147
+ required_ruby_version: !ruby/object:Gem::Requirement
78
148
  none: false
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: "0"
83
- required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
154
  none: false
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- version: "0"
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
89
159
  requirements: []
90
-
91
160
  rubyforge_project: objectify
92
- rubygems_version: 1.5.0
161
+ rubygems_version: 1.8.10
93
162
  signing_key:
94
163
  specification_version: 3
95
- summary: A simple gem to make hashes and arrays of hashes behave like objects.
96
- test_files:
97
- - spec/objectify_spec.rb
164
+ summary: ''
165
+ test_files:
166
+ - spec/config/action_spec.rb
167
+ - spec/config/context_spec.rb
168
+ - spec/config/policies_spec.rb
169
+ - spec/executor_spec.rb
170
+ - spec/injector_spec.rb
171
+ - spec/instantiator_spec.rb
172
+ - spec/policy_chain_executor_spec.rb
173
+ - spec/rails/renderer_spec.rb
174
+ - spec/rails/routing_spec.rb
175
+ - spec/resolver_locator_spec.rb
176
+ - spec/resolver_spec.rb
177
+ - spec/route_spec.rb
98
178
  - spec/spec_helper.rb