dicer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. data/.gitignore +17 -0
  2. data/.pryrc +2 -0
  3. data/.rbenv-version +1 -0
  4. data/.rspec +2 -0
  5. data/Gemfile +12 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +81 -0
  8. data/Rakefile +1 -0
  9. data/bench/call_only.rb +77 -0
  10. data/bench/method_call.rb +76 -0
  11. data/bench/send.rb +30 -0
  12. data/dicer.gemspec +23 -0
  13. data/examples/rails-3.2/.gitignore +15 -0
  14. data/examples/rails-3.2/.rspec +2 -0
  15. data/examples/rails-3.2/Gemfile +24 -0
  16. data/examples/rails-3.2/README.rdoc +261 -0
  17. data/examples/rails-3.2/Rakefile +7 -0
  18. data/examples/rails-3.2/app/assets/images/rails.png +0 -0
  19. data/examples/rails-3.2/app/assets/javascripts/application.js +13 -0
  20. data/examples/rails-3.2/app/assets/stylesheets/application.css +13 -0
  21. data/examples/rails-3.2/app/behaviors/book_store_customer.rb +14 -0
  22. data/examples/rails-3.2/app/behaviors/testable.rb +7 -0
  23. data/examples/rails-3.2/app/contexts/application_context.rb +5 -0
  24. data/examples/rails-3.2/app/contexts/book_store_purchase_context.rb +5 -0
  25. data/examples/rails-3.2/app/controllers/application_controller.rb +3 -0
  26. data/examples/rails-3.2/app/controllers/book_store_controller.rb +24 -0
  27. data/examples/rails-3.2/app/helpers/application_helper.rb +2 -0
  28. data/examples/rails-3.2/app/mailers/.gitkeep +0 -0
  29. data/examples/rails-3.2/app/models/.gitkeep +0 -0
  30. data/examples/rails-3.2/app/models/book.rb +6 -0
  31. data/examples/rails-3.2/app/models/book_purchase.rb +6 -0
  32. data/examples/rails-3.2/app/models/user.rb +6 -0
  33. data/examples/rails-3.2/app/views/layouts/application.html.erb +14 -0
  34. data/examples/rails-3.2/config/application.rb +70 -0
  35. data/examples/rails-3.2/config/boot.rb +6 -0
  36. data/examples/rails-3.2/config/database.yml +25 -0
  37. data/examples/rails-3.2/config/environment.rb +5 -0
  38. data/examples/rails-3.2/config/environments/development.rb +37 -0
  39. data/examples/rails-3.2/config/environments/production.rb +67 -0
  40. data/examples/rails-3.2/config/environments/test.rb +37 -0
  41. data/examples/rails-3.2/config/initializers/backtrace_silencers.rb +7 -0
  42. data/examples/rails-3.2/config/initializers/inflections.rb +15 -0
  43. data/examples/rails-3.2/config/initializers/mime_types.rb +5 -0
  44. data/examples/rails-3.2/config/initializers/secret_token.rb +7 -0
  45. data/examples/rails-3.2/config/initializers/session_store.rb +8 -0
  46. data/examples/rails-3.2/config/initializers/wrap_parameters.rb +14 -0
  47. data/examples/rails-3.2/config/locales/en.yml +5 -0
  48. data/examples/rails-3.2/config/routes.rb +7 -0
  49. data/examples/rails-3.2/config.ru +4 -0
  50. data/examples/rails-3.2/db/migrate/20130104095330_create_users.rb +10 -0
  51. data/examples/rails-3.2/db/migrate/20130104171610_create_books.rb +9 -0
  52. data/examples/rails-3.2/db/migrate/20130104172409_create_book_purchases.rb +10 -0
  53. data/examples/rails-3.2/db/schema.rb +36 -0
  54. data/examples/rails-3.2/db/seeds.rb +7 -0
  55. data/examples/rails-3.2/lib/assets/.gitkeep +0 -0
  56. data/examples/rails-3.2/lib/tasks/.gitkeep +0 -0
  57. data/examples/rails-3.2/log/.gitkeep +0 -0
  58. data/examples/rails-3.2/public/404.html +26 -0
  59. data/examples/rails-3.2/public/422.html +26 -0
  60. data/examples/rails-3.2/public/500.html +25 -0
  61. data/examples/rails-3.2/public/favicon.ico +0 -0
  62. data/examples/rails-3.2/public/index.html +241 -0
  63. data/examples/rails-3.2/public/robots.txt +5 -0
  64. data/examples/rails-3.2/script/rails +6 -0
  65. data/examples/rails-3.2/spec/controllers/book_store_controller_spec.rb +37 -0
  66. data/examples/rails-3.2/spec/factories/book_purchases.rb +8 -0
  67. data/examples/rails-3.2/spec/factories/books.rb +7 -0
  68. data/examples/rails-3.2/spec/factories/users.rb +8 -0
  69. data/examples/rails-3.2/spec/generators/behavior_generator_spec.rb +26 -0
  70. data/examples/rails-3.2/spec/generators/context_generator_spec.rb +25 -0
  71. data/examples/rails-3.2/spec/models/book_purchase_spec.rb +17 -0
  72. data/examples/rails-3.2/spec/models/book_spec.rb +15 -0
  73. data/examples/rails-3.2/spec/models/user_spec.rb +15 -0
  74. data/examples/rails-3.2/spec/spec_helper.rb +26 -0
  75. data/examples/rails-3.2/vendor/assets/javascripts/.gitkeep +0 -0
  76. data/examples/rails-3.2/vendor/assets/stylesheets/.gitkeep +0 -0
  77. data/examples/rails-3.2/vendor/plugins/.gitkeep +0 -0
  78. data/examples/rails-3.2-with-mongoid/.gitignore +15 -0
  79. data/examples/rails-3.2-with-mongoid/.rspec +2 -0
  80. data/examples/rails-3.2-with-mongoid/Gemfile +24 -0
  81. data/examples/rails-3.2-with-mongoid/README.rdoc +261 -0
  82. data/examples/rails-3.2-with-mongoid/Rakefile +7 -0
  83. data/examples/rails-3.2-with-mongoid/app/assets/images/rails.png +0 -0
  84. data/examples/rails-3.2-with-mongoid/app/assets/javascripts/application.js +13 -0
  85. data/examples/rails-3.2-with-mongoid/app/assets/stylesheets/application.css +13 -0
  86. data/examples/rails-3.2-with-mongoid/app/helpers/application_helper.rb +2 -0
  87. data/examples/rails-3.2-with-mongoid/app/mailers/.gitkeep +0 -0
  88. data/examples/rails-3.2-with-mongoid/app/models/.gitkeep +0 -0
  89. data/examples/rails-3.2-with-mongoid/app/models/book.rb +7 -0
  90. data/examples/rails-3.2-with-mongoid/app/models/book_purchase.rb +6 -0
  91. data/examples/rails-3.2-with-mongoid/app/models/user.rb +8 -0
  92. data/examples/rails-3.2-with-mongoid/app/views/layouts/application.html.erb +14 -0
  93. data/examples/rails-3.2-with-mongoid/config/application.rb +70 -0
  94. data/examples/rails-3.2-with-mongoid/config/boot.rb +6 -0
  95. data/examples/rails-3.2-with-mongoid/config/environment.rb +5 -0
  96. data/examples/rails-3.2-with-mongoid/config/environments/development.rb +31 -0
  97. data/examples/rails-3.2-with-mongoid/config/environments/production.rb +64 -0
  98. data/examples/rails-3.2-with-mongoid/config/environments/test.rb +35 -0
  99. data/examples/rails-3.2-with-mongoid/config/initializers/backtrace_silencers.rb +7 -0
  100. data/examples/rails-3.2-with-mongoid/config/initializers/inflections.rb +15 -0
  101. data/examples/rails-3.2-with-mongoid/config/initializers/mime_types.rb +5 -0
  102. data/examples/rails-3.2-with-mongoid/config/initializers/secret_token.rb +7 -0
  103. data/examples/rails-3.2-with-mongoid/config/initializers/session_store.rb +8 -0
  104. data/examples/rails-3.2-with-mongoid/config/initializers/wrap_parameters.rb +10 -0
  105. data/examples/rails-3.2-with-mongoid/config/locales/en.yml +5 -0
  106. data/examples/rails-3.2-with-mongoid/config/mongoid.yml +80 -0
  107. data/examples/rails-3.2-with-mongoid/config/routes.rb +7 -0
  108. data/examples/rails-3.2-with-mongoid/config.ru +4 -0
  109. data/examples/rails-3.2-with-mongoid/db/seeds.rb +7 -0
  110. data/examples/rails-3.2-with-mongoid/lib/assets/.gitkeep +0 -0
  111. data/examples/rails-3.2-with-mongoid/lib/tasks/.gitkeep +0 -0
  112. data/examples/rails-3.2-with-mongoid/log/.gitkeep +0 -0
  113. data/examples/rails-3.2-with-mongoid/public/404.html +26 -0
  114. data/examples/rails-3.2-with-mongoid/public/422.html +26 -0
  115. data/examples/rails-3.2-with-mongoid/public/500.html +25 -0
  116. data/examples/rails-3.2-with-mongoid/public/favicon.ico +0 -0
  117. data/examples/rails-3.2-with-mongoid/public/index.html +241 -0
  118. data/examples/rails-3.2-with-mongoid/public/robots.txt +5 -0
  119. data/examples/rails-3.2-with-mongoid/script/rails +6 -0
  120. data/examples/rails-3.2-with-mongoid/vendor/assets/javascripts/.gitkeep +0 -0
  121. data/examples/rails-3.2-with-mongoid/vendor/assets/stylesheets/.gitkeep +0 -0
  122. data/examples/rails-3.2-with-mongoid/vendor/plugins/.gitkeep +0 -0
  123. data/lib/dicer/behavior.rb +17 -0
  124. data/lib/dicer/config.rb +15 -0
  125. data/lib/dicer/context/description.rb +27 -0
  126. data/lib/dicer/context.rb +30 -0
  127. data/lib/dicer/contextable.rb +19 -0
  128. data/lib/dicer/delegator.rb +80 -0
  129. data/lib/dicer/middleware.rb +20 -0
  130. data/lib/dicer/railtie/action_controller.rb +48 -0
  131. data/lib/dicer/railtie/active_record.rb +29 -0
  132. data/lib/dicer/railtie/context.rb +30 -0
  133. data/lib/dicer/railtie/contextable.rb +10 -0
  134. data/lib/dicer/railtie.rb +65 -0
  135. data/lib/dicer/version.rb +3 -0
  136. data/lib/dicer.rb +6 -0
  137. data/lib/generators/behavior/behavior_generator.rb +20 -0
  138. data/lib/generators/behavior/template/behavior.rb +3 -0
  139. data/lib/generators/context/context_generator.rb +32 -0
  140. data/lib/generators/context/template/context.rb +2 -0
  141. data/resource/dice.png +0 -0
  142. data/resource/dicer_logo.png +0 -0
  143. data/spec/dicer/behavior_spec.rb +18 -0
  144. data/spec/dicer/context/description_spec.rb +41 -0
  145. data/spec/dicer/context_spec.rb +54 -0
  146. data/spec/dicer/contextable_spec.rb +35 -0
  147. data/spec/dicer/delegator_spec.rb +34 -0
  148. data/spec/dicer/railtie/context_spec.rb +106 -0
  149. data/spec/dicer/railtie/contextable_spec.rb +18 -0
  150. data/spec/spec_helper.rb +27 -0
  151. data/spec/support/cleaner.rb +5 -0
  152. data/spec/support/cleaning_context.rb +8 -0
  153. data/spec/support/entity.rb +31 -0
  154. data/spec/support/value.rb +22 -0
  155. metadata +250 -0
@@ -0,0 +1,48 @@
1
+ module Dicer
2
+ module ActionController
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ prepend_before_filter do
7
+ Dicer::Context.current_controller = self
8
+
9
+ default_context = self.class.default_contexts[params[:action].to_sym]
10
+ Dicer::Context.current = default_context.new if default_context.present?
11
+ end
12
+
13
+ helper_method :context
14
+ helper_method :in_context
15
+ end
16
+
17
+ module ClassMethods
18
+ def context_for(action, context_class = nil, &context_define)
19
+ context = context_class || Class.new(Dicer::Context, &context_define)
20
+
21
+ default_contexts[action.to_s.to_sym] = context
22
+ end
23
+
24
+ def default_contexts
25
+ @default_contexts ||= {}.tap do |hash|
26
+ hash.default = ApplicationContext if defined?(ApplicationContext)
27
+ end
28
+ end
29
+ end
30
+
31
+ def context(context = nil)
32
+ context.present? ?
33
+ Dicer::Context.current = context :
34
+ Dicer::Context.current
35
+ end
36
+ private :context
37
+
38
+ def in_context(context, &block)
39
+ current_context = Dicer::Context.current
40
+ Dicer::Context.current = context.is_a?(Class) ? context.new : context
41
+
42
+ block.call(Dicer::Context.current)
43
+ ensure
44
+ Dicer::Context.current = current_context
45
+ end
46
+ private :in_context
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ module Dicer
2
+ module ActiveRecord
3
+ extend ActiveSupport::Concern
4
+ include Dicer::Contextable
5
+
6
+ included do
7
+ def self.new_with_dicer(*args, &block)
8
+ if Dicer.config.auto_supply && Dicer::Context.current.present?
9
+ new_without_dicer(*args, &block).in_context
10
+ else
11
+ new_without_dicer(*args, &block)
12
+ end
13
+ end
14
+
15
+ def self.instantiate_with_dicer(*args)
16
+ if Dicer.config.auto_supply && Dicer::Context.current.present?
17
+ instantiate_without_dicer(*args).in_context
18
+ else
19
+ instantiate_without_dicer(*args)
20
+ end
21
+ end
22
+
23
+ class << self
24
+ alias_method_chain :new, :dicer
25
+ alias_method_chain :instantiate, :dicer
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ require 'dicer/context'
2
+ require 'forwardable'
3
+
4
+ module Dicer
5
+ class Context
6
+ extend Forwardable
7
+
8
+ def self.current=(context)
9
+ Middleware.store[:context] = context
10
+ end
11
+
12
+ def self.current
13
+ Middleware.store[:context]
14
+ end
15
+
16
+ def self.current_controller=(controller)
17
+ Middleware.store[:current_controller] = controller
18
+ end
19
+
20
+ def self.current_controller
21
+ Middleware.store[:current_controller]
22
+ end
23
+
24
+ def controller
25
+ self.class.current_controller
26
+ end
27
+
28
+ def_delegators :controller, :request, :cookies, :session, :params
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ require 'dicer/contextable'
2
+
3
+ module Dicer
4
+ module Contextable
5
+ def in_context_with_dicer(context = Dicer::Context.current, &block)
6
+ in_context_without_dicer(context, &block)
7
+ end
8
+ alias_method_chain :in_context, :dicer
9
+ end
10
+ end
@@ -0,0 +1,65 @@
1
+ require 'dicer/middleware'
2
+
3
+ require 'dicer/railtie/action_controller'
4
+ require 'dicer/railtie/active_record'
5
+
6
+ require 'dicer/railtie/context'
7
+ require 'dicer/railtie/contextable'
8
+
9
+ module Dicer
10
+ class Railtie < ::Rails::Railtie
11
+ def self.generator
12
+ config.respond_to?(:app_generators) ? :app_generators : :generators
13
+ end
14
+
15
+ config.dicer = Dicer.config
16
+
17
+ initializer 'dicer' do |app|
18
+ config.after_initialize do
19
+ if Dicer.config.auto_supply && !defined?(ActiveRecord)
20
+ puts "Dicer's auto_supply option is supporting ActiveRecord only"
21
+ end
22
+ end
23
+
24
+ # Paths
25
+ app.config.paths.add('app/contexts', :eager_laod => true)
26
+ app.config.paths.add('app/behaviors', :eager_laod => true)
27
+
28
+ # Middleware
29
+ app.config.middleware.use Dicer::Middleware
30
+
31
+ # ActionController
32
+ ActiveSupport.on_load :action_controller do |controller|
33
+ Dicer.setup_action_controller(controller)
34
+ end
35
+
36
+ # ActiveRecord
37
+ ActiveSupport.on_load :active_record do |orm|
38
+ Dicer.setup_active_record(orm)
39
+ end
40
+
41
+ # Mongoid
42
+ ActiveSupport.on_load :mongoid do |orm|
43
+ Dicer.setup_mongoid(orm)
44
+ end
45
+ end
46
+ end
47
+
48
+ def self.setup_action_controller(controller)
49
+ controller.class_eval do
50
+ include Dicer::ActionController
51
+ end
52
+ end
53
+
54
+ def self.setup_active_record(orm)
55
+ orm.class_eval do
56
+ include Dicer::ActiveRecord
57
+ end
58
+ end
59
+
60
+ def self.setup_mongoid(orm)
61
+ orm.module_eval do
62
+ include Dicer::Contextable
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module Dicer
2
+ VERSION = '0.0.1'
3
+ end
data/lib/dicer.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'dicer/version'
2
+ require 'dicer/config'
3
+ require 'dicer/context'
4
+ require 'dicer/behavior'
5
+ require 'dicer/contextable'
6
+ require 'dicer/railtie' if defined?(Rails)
@@ -0,0 +1,20 @@
1
+ require 'rails/generators'
2
+
3
+ module Rails
4
+ module Generators
5
+ class BehaviorGenerator < NamedBase
6
+ source_root File.expand_path('../template', __FILE__)
7
+
8
+ def create_behavior_file
9
+ template(
10
+ 'behavior.rb',
11
+ File.join(
12
+ 'app/behaviors',
13
+ class_path,
14
+ "#{file_name}.rb"
15
+ )
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module <%= class_name %>
2
+ include Dicer::Behavior
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'rails/generators'
2
+
3
+ module Rails
4
+ module Generators
5
+ class ContextGenerator < NamedBase
6
+ source_root File.expand_path('../template', __FILE__)
7
+
8
+ check_class_collision :suffix => 'Context'
9
+
10
+ class_option :parent,
11
+ :type => :string,
12
+ :desc => 'The parent class for the generated context'
13
+
14
+ def create_context_file
15
+ template(
16
+ 'context.rb',
17
+ File.join(
18
+ 'app/contexts',
19
+ class_path,
20
+ "#{file_name}_context.rb"
21
+ )
22
+ )
23
+ end
24
+
25
+ private
26
+ def parent_class_name
27
+ options[:parent] || defined?(ApplicationContext) ?
28
+ 'ApplicationContext' : 'Dicer::Context'
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,2 @@
1
+ class <%= class_name %>Context < <%= parent_class_name %>
2
+ end
data/resource/dice.png ADDED
Binary file
Binary file
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dicer::Behavior do
4
+ subject(:behavior) do
5
+ Module.new do
6
+ include Dicer::Behavior
7
+
8
+ def foo; 2; end
9
+ end
10
+ end
11
+
12
+ describe '.behavior_methods' do
13
+ subject { behavior.behavior_methods }
14
+
15
+ it { should have(1).item }
16
+ it { should include(:foo) }
17
+ end
18
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dicer::Context::Description do
4
+ subject(:description) do
5
+ Dicer::Context::Description.new(Entity) do
6
+ it_behaves_like Cleaner
7
+ end
8
+ end
9
+
10
+ describe '#described_class' do
11
+ subject { description.described_class }
12
+
13
+ it { should == Entity }
14
+ end
15
+
16
+ describe '#behaviors' do
17
+ subject { description.behaviors }
18
+
19
+ it { should have(1).behavior }
20
+ it { should include(Cleaner) }
21
+ end
22
+
23
+ describe '#delegator' do
24
+ subject { description.delegator }
25
+
26
+ it { should be_a(Class) }
27
+
28
+ describe 'decorated entity' do
29
+ subject(:entity) { description.delegator.new(Entity.new({})) }
30
+
31
+ it { should respond_to(:clean) }
32
+ it { should be_kind_of(Entity) }
33
+
34
+ describe '#class' do
35
+ subject { entity.class }
36
+
37
+ it { should == Entity }
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dicer::Context do
4
+ let(:context_class) do
5
+ Class.new(Dicer::Context) do
6
+ describe Entity do
7
+ it_behaves_like Cleaner
8
+ end
9
+
10
+ describe Entity do
11
+ end
12
+ end
13
+ end
14
+
15
+ subject(:context) { context_class.new }
16
+
17
+ describe '.descriptions' do
18
+ subject { context_class.descriptions }
19
+
20
+ it { should have(1).item }
21
+ end
22
+
23
+ describe '.describe' do
24
+ subject(:describe) do
25
+ context_class.describe(Value) {}
26
+ end
27
+
28
+ specify do
29
+ expect { describe }.to change { context_class.descriptions.size }.by(1)
30
+ end
31
+ end
32
+
33
+ describe '#supply' do
34
+ context 'with described class' do
35
+ let(:entity) { Entity.new({}) }
36
+
37
+ subject { context.supply(entity) }
38
+
39
+ it { should be_kind_of(Entity) }
40
+ it { should == entity }
41
+ it { should respond_to(:clean) }
42
+ end
43
+
44
+ context 'with not described class' do
45
+ let(:value) { Value.new({}) }
46
+
47
+ subject { context.supply(value) }
48
+
49
+ it { should be_kind_of(Value) }
50
+ it { should == value }
51
+ it { should_not respond_to(:clean) }
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dicer::Contextable do
4
+ let(:entity) { Entity.new({}) }
5
+ let(:cleaning_context) { CleaningContext.new }
6
+
7
+ describe '#in_context' do
8
+ subject { entity.in_context(cleaning_context) }
9
+
10
+ it { should respond_to(:clean) }
11
+ it { should == entity }
12
+
13
+ context 'with block' do
14
+ it do
15
+ entity.in_context(cleaning_context) do |cleaner|
16
+ cleaner.should respond_to(:clean)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ describe '#as' do
23
+ subject { entity.as(Cleaner) }
24
+
25
+ it { should respond_to(:clean) }
26
+
27
+ context 'with block' do
28
+ it do
29
+ entity.as(Cleaner) do |cleaner|
30
+ cleaner.should respond_to(:clean)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dicer::Delegator do
4
+ let(:delegator) { Dicer::Delegator.make(Entity, [Cleaner]) }
5
+ let(:entity) { Entity.new(:test => 1) }
6
+
7
+ subject(:delegated) { delegator.new(entity) }
8
+
9
+ it { should == entity }
10
+
11
+ describe '#__id__' do
12
+ subject { delegated.__id__ }
13
+
14
+ it { should_not == entity.__id__ }
15
+ end
16
+
17
+ describe '#test' do
18
+ subject { delegated.test }
19
+
20
+ it { should == 1 }
21
+ end
22
+
23
+ describe '#id' do
24
+ subject { delegated.id }
25
+
26
+ it { should == entity.id }
27
+ end
28
+
29
+ describe '#clean' do
30
+ subject { delegated.clean }
31
+
32
+ it { should be_true }
33
+ end
34
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dicer::Context do
4
+ let(:context) { Dicer::Context.new }
5
+ let(:controller) { ActionController::Base.new }
6
+
7
+ describe '.current' do
8
+ subject { described_class.current }
9
+
10
+ it { should be_nil }
11
+
12
+ context 'when set current context' do
13
+ before(:all) do
14
+ described_class.current = context
15
+ end
16
+
17
+ it { should == context }
18
+
19
+ context 'in another thread' do
20
+ around {|e| Thread.new(e) {|e| e.run }.join }
21
+
22
+ it { should_not == context }
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '.current_controller' do
28
+ subject { described_class.current_controller }
29
+
30
+ it { should be_nil }
31
+
32
+ context 'when set current controller' do
33
+ before(:all) do
34
+ described_class.current_controller = controller
35
+ end
36
+
37
+ it { should == controller }
38
+
39
+ context 'when another thread' do
40
+ around {|e| Thread.new(e) {|e| e.run }.join }
41
+
42
+ it { should_not == controller }
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#controller' do
48
+ before { described_class.current_controller = controller }
49
+
50
+ subject { context.controller }
51
+
52
+ it { should == controller }
53
+ end
54
+
55
+ describe '#request' do
56
+ let(:request) { mock }
57
+
58
+ before do
59
+ controller.request = request
60
+ described_class.current_controller = controller
61
+ end
62
+
63
+ subject { context.request }
64
+
65
+ it { should == request }
66
+ end
67
+
68
+ describe '#params' do
69
+ let(:params) { mock }
70
+
71
+ before do
72
+ controller.params = params
73
+ described_class.current_controller = controller
74
+ end
75
+
76
+ subject { context.params }
77
+
78
+ it { should == params }
79
+ end
80
+
81
+ describe '#cookies' do
82
+ let(:cookies) { mock }
83
+
84
+ before do
85
+ controller.request = mock(:cookie_jar => cookies)
86
+ described_class.current_controller = controller
87
+ end
88
+
89
+ subject { context.cookies }
90
+
91
+ it { should == cookies }
92
+ end
93
+
94
+ describe '#session' do
95
+ let(:session) { mock }
96
+
97
+ before do
98
+ controller.request = mock(:session => session)
99
+ described_class.current_controller = controller
100
+ end
101
+
102
+ subject { context.session }
103
+
104
+ it { should == session }
105
+ end
106
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dicer::Contextable do
4
+ let(:entity) { Entity.new({}) }
5
+ let(:cleaning_context) { CleaningContext.new }
6
+
7
+ describe '#in_context' do
8
+ context 'current context is cleaning context' do
9
+ before do
10
+ Dicer::Context.current = cleaning_context
11
+ end
12
+
13
+ subject { entity.in_context }
14
+
15
+ it { should respond_to(:clean) }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'rails'
4
+ require 'action_controller'
5
+ require 'action_controller/test_case'
6
+ require 'active_record'
7
+
8
+ require 'simplecov'
9
+ SimpleCov.add_filter('spec')
10
+ SimpleCov.start
11
+
12
+ Bundler.require(:default, :test)
13
+
14
+ RSpec.configure do |config|
15
+ config.treat_symbols_as_metadata_keys_with_true_values = true
16
+ config.run_all_when_everything_filtered = true
17
+
18
+ config.before(:suite) do
19
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each do |lib|
20
+ require lib
21
+ end
22
+ end
23
+
24
+ config.before(:all) do
25
+ Dicer::Middleware.clear!
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module Cleaner
2
+ include Dicer::Behavior
3
+
4
+ def clean; true; end
5
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../entity', __FILE__)
2
+ require File.expand_path('../cleaner', __FILE__)
3
+
4
+ class CleaningContext < Dicer::Context
5
+ describe Entity do
6
+ it_behaves_like Cleaner
7
+ end
8
+ end
@@ -0,0 +1,31 @@
1
+ require 'securerandom'
2
+
3
+ class Entity
4
+ include Dicer::Contextable
5
+
6
+ def initialize(data)
7
+ @id = SecureRandom.hex
8
+
9
+ data.each_pair do |key, val|
10
+ instance_variable_set(:"@#{key}", val)
11
+
12
+ if key.to_s != 'id'
13
+ (class << self; self; end).class_eval(<<-EOF)
14
+ def #{key}
15
+ @#{key}
16
+ end
17
+
18
+ def #{key}=(v)
19
+ @#{key} = v
20
+ end
21
+ EOF
22
+ end
23
+ end
24
+ end
25
+
26
+ attr_reader :id
27
+
28
+ def ==(other)
29
+ other.class == self.class && other.id == self.id
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ class Value < Hash
2
+ def [](val)
3
+ super(val.to_s)
4
+ end
5
+
6
+ def []=(name, val)
7
+ super(name.to_s, val)
8
+ end
9
+
10
+ def method_missing(name, *args)
11
+ if name.to_s[-1] == '='
12
+ name = name[0 .. -2]
13
+ self[name] = args[0]
14
+ (class << self; self; end).class_eval(<<-EOF)
15
+ def #{name}; self["#{name}"]; end
16
+ def #{name}=(v); self["#{name}"] = v; end
17
+ EOF
18
+ else
19
+ super
20
+ end
21
+ end
22
+ end