controll 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +19 -0
  4. data/Gemfile.lock +134 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.md +320 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/controll.gemspec +184 -0
  10. data/lib/controll.rb +14 -0
  11. data/lib/controll/assistant.rb +19 -0
  12. data/lib/controll/command.rb +24 -0
  13. data/lib/controll/commander.rb +24 -0
  14. data/lib/controll/errors.rb +1 -0
  15. data/lib/controll/executor.rb +6 -0
  16. data/lib/controll/executor/base.rb +16 -0
  17. data/lib/controll/executor/notificator.rb +12 -0
  18. data/lib/controll/flow_handler.rb +11 -0
  19. data/lib/controll/flow_handler/base.rb +19 -0
  20. data/lib/controll/flow_handler/control.rb +85 -0
  21. data/lib/controll/flow_handler/errors.rb +5 -0
  22. data/lib/controll/flow_handler/event_helper.rb +18 -0
  23. data/lib/controll/flow_handler/redirect.rb +51 -0
  24. data/lib/controll/flow_handler/redirect/action.rb +45 -0
  25. data/lib/controll/flow_handler/redirect/mapper.rb +41 -0
  26. data/lib/controll/flow_handler/render.rb +51 -0
  27. data/lib/controll/helper.rb +101 -0
  28. data/lib/controll/helper/event_matcher.rb +21 -0
  29. data/lib/controll/helper/hash_access.rb +26 -0
  30. data/lib/controll/helper/notify.rb +74 -0
  31. data/lib/controll/helper/params.rb +20 -0
  32. data/lib/controll/helper/path_resolver.rb +45 -0
  33. data/lib/controll/helper/session.rb +20 -0
  34. data/lib/controll/notify.rb +7 -0
  35. data/lib/controll/notify/base.rb +75 -0
  36. data/lib/controll/notify/flash.rb +52 -0
  37. data/lib/controll/notify/typed.rb +39 -0
  38. data/spec/acceptance/app_test.rb +156 -0
  39. data/spec/app/.gitignore +15 -0
  40. data/spec/app/Gemfile +6 -0
  41. data/spec/app/Gemfile.lock +108 -0
  42. data/spec/app/README.rdoc +261 -0
  43. data/spec/app/Rakefile +7 -0
  44. data/spec/app/app/controllers/application_controller.rb +6 -0
  45. data/spec/app/app/controllers/posts_controller.rb +52 -0
  46. data/spec/app/app/models/.gitkeep +0 -0
  47. data/spec/app/app/models/post.rb +88 -0
  48. data/spec/app/app/views/layouts/application.html.erb +13 -0
  49. data/spec/app/app/views/posts/_form.html.erb +31 -0
  50. data/spec/app/app/views/posts/edit.html.erb +6 -0
  51. data/spec/app/app/views/posts/index.html.erb +23 -0
  52. data/spec/app/app/views/posts/new.html.erb +5 -0
  53. data/spec/app/app/views/posts/show.html.erb +8 -0
  54. data/spec/app/config.ru +4 -0
  55. data/spec/app/config/application.rb +16 -0
  56. data/spec/app/config/boot.rb +6 -0
  57. data/spec/app/config/environment.rb +5 -0
  58. data/spec/app/config/environments/development.rb +21 -0
  59. data/spec/app/config/environments/test.rb +29 -0
  60. data/spec/app/config/initializers/backtrace_silencers.rb +7 -0
  61. data/spec/app/config/initializers/inflections.rb +15 -0
  62. data/spec/app/config/initializers/mime_types.rb +5 -0
  63. data/spec/app/config/initializers/secret_token.rb +7 -0
  64. data/spec/app/config/initializers/session_store.rb +8 -0
  65. data/spec/app/config/locales/en.yml +5 -0
  66. data/spec/app/config/routes.rb +62 -0
  67. data/spec/app/db/seeds.rb +7 -0
  68. data/spec/app/lib/assets/.gitkeep +0 -0
  69. data/spec/app/lib/tasks/.gitkeep +0 -0
  70. data/spec/app/log/.gitkeep +0 -0
  71. data/spec/app/public/404.html +26 -0
  72. data/spec/app/public/422.html +26 -0
  73. data/spec/app/public/500.html +25 -0
  74. data/spec/app/public/favicon.ico +0 -0
  75. data/spec/app/public/index.html +241 -0
  76. data/spec/app/public/javascripts/application.js +9663 -0
  77. data/spec/app/public/robots.txt +5 -0
  78. data/spec/app/public/stylesheets/application.css +83 -0
  79. data/spec/app/script/rails +6 -0
  80. data/spec/app/spec/controllers/posts_controller_spec.rb +59 -0
  81. data/spec/app/spec/isolated_spec_helper.rb +6 -0
  82. data/spec/app/spec/spec_helper.rb +5 -0
  83. data/spec/app/spec/unit/controllers/posts_controller_isolated_spec.rb +63 -0
  84. data/spec/app/spec/unit/controllers/posts_controller_spec.rb +59 -0
  85. data/spec/app/test/functional/.gitkeep +0 -0
  86. data/spec/app/test/functional/posts_controller_test.rb +67 -0
  87. data/spec/app/test/isolated_test_helper.rb +7 -0
  88. data/spec/app/test/test_helper.rb +6 -0
  89. data/spec/app/test/unit/.gitkeep +0 -0
  90. data/spec/app/test/unit/controllers/posts_controller_isolated_test.rb +71 -0
  91. data/spec/app/test/unit/controllers/posts_controller_test.rb +67 -0
  92. data/spec/app/vendor/assets/javascripts/.gitkeep +0 -0
  93. data/spec/app/vendor/assets/stylesheets/.gitkeep +0 -0
  94. data/spec/app/vendor/plugins/.gitkeep +0 -0
  95. data/spec/controll/asssistant_spec.rb +5 -0
  96. data/spec/controll/command_spec.rb +56 -0
  97. data/spec/controll/commander_spec.rb +68 -0
  98. data/spec/controll/executor/notificator_spec.rb +27 -0
  99. data/spec/controll/flow_handler/control_spec.rb +159 -0
  100. data/spec/controll/flow_handler/redirect/action_spec.rb +48 -0
  101. data/spec/controll/flow_handler/redirect/mapper_spec.rb +69 -0
  102. data/spec/controll/flow_handler/redirect_spec.rb +93 -0
  103. data/spec/controll/flow_handler/render_spec.rb +110 -0
  104. data/spec/controll/helper/event_matcher_spec.rb +21 -0
  105. data/spec/controll/helper/hash_access_spec.rb +25 -0
  106. data/spec/controll/helper/notify_spec.rb +48 -0
  107. data/spec/controll/helper/params_spec.rb +28 -0
  108. data/spec/controll/helper/path_resolver_spec.rb +49 -0
  109. data/spec/controll/helper/session_spec.rb +28 -0
  110. data/spec/controll/helper_spec.rb +108 -0
  111. data/spec/controll/notify/base_spec.rb +123 -0
  112. data/spec/controll/notify/flash_spec.rb +27 -0
  113. data/spec/controll/notify/message_handler.rb +72 -0
  114. data/spec/controll/notify/typed_spec.rb +28 -0
  115. data/spec/functional_test_helper.rb +25 -0
  116. data/spec/helper.rb +33 -0
  117. data/spec/rspec_controller_class.rb +15 -0
  118. data/spec/rspec_functional_helper.rb +25 -0
  119. data/spec/rspec_helper.rb +42 -0
  120. data/spec/spec_helper.rb +11 -0
  121. data/spec/test_helper.rb +183 -0
  122. data/spec/unit/functional_test_helper_test.rb +65 -0
  123. data/spec/unit/macros_test.rb +43 -0
  124. data/spec/unit/mixin_test.rb +147 -0
  125. data/spec/unit/rspec_functional_helper.rb +42 -0
  126. data/spec/unit/rspec_helper_test.rb +91 -0
  127. data/spec/unit/test_helper_test.rb +235 -0
  128. metadata +289 -0
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "controll"
18
+ gem.homepage = "http://github.com/kristianmandrup/controll"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Utils for handling complex Controller/Business logic}
21
+ gem.description = %Q{FlowHandler, Executor, Notifier and more, all tied together tool pack}
22
+ gem.email = "kmandrup@gmail.com"
23
+ gem.authors = ["Kristian Mandrup"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "controll #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/controll.gemspec ADDED
@@ -0,0 +1,184 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "controll"
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = "2012-08-16"
13
+ s.description = "FlowHandler, Executor, Notifier and more, all tied together tool pack"
14
+ s.email = "kmandrup@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "controll.gemspec",
29
+ "lib/controll.rb",
30
+ "lib/controll/assistant.rb",
31
+ "lib/controll/command.rb",
32
+ "lib/controll/commander.rb",
33
+ "lib/controll/errors.rb",
34
+ "lib/controll/executor.rb",
35
+ "lib/controll/executor/base.rb",
36
+ "lib/controll/executor/notificator.rb",
37
+ "lib/controll/flow_handler.rb",
38
+ "lib/controll/flow_handler/base.rb",
39
+ "lib/controll/flow_handler/control.rb",
40
+ "lib/controll/flow_handler/errors.rb",
41
+ "lib/controll/flow_handler/event_helper.rb",
42
+ "lib/controll/flow_handler/redirect.rb",
43
+ "lib/controll/flow_handler/redirect/action.rb",
44
+ "lib/controll/flow_handler/redirect/mapper.rb",
45
+ "lib/controll/flow_handler/render.rb",
46
+ "lib/controll/helper.rb",
47
+ "lib/controll/helper/event_matcher.rb",
48
+ "lib/controll/helper/hash_access.rb",
49
+ "lib/controll/helper/notify.rb",
50
+ "lib/controll/helper/params.rb",
51
+ "lib/controll/helper/path_resolver.rb",
52
+ "lib/controll/helper/session.rb",
53
+ "lib/controll/notify.rb",
54
+ "lib/controll/notify/base.rb",
55
+ "lib/controll/notify/flash.rb",
56
+ "lib/controll/notify/typed.rb",
57
+ "spec/acceptance/app_test.rb",
58
+ "spec/app/.gitignore",
59
+ "spec/app/Gemfile",
60
+ "spec/app/Gemfile.lock",
61
+ "spec/app/README.rdoc",
62
+ "spec/app/Rakefile",
63
+ "spec/app/app/controllers/application_controller.rb",
64
+ "spec/app/app/controllers/posts_controller.rb",
65
+ "spec/app/app/models/.gitkeep",
66
+ "spec/app/app/models/post.rb",
67
+ "spec/app/app/views/layouts/application.html.erb",
68
+ "spec/app/app/views/posts/_form.html.erb",
69
+ "spec/app/app/views/posts/edit.html.erb",
70
+ "spec/app/app/views/posts/index.html.erb",
71
+ "spec/app/app/views/posts/new.html.erb",
72
+ "spec/app/app/views/posts/show.html.erb",
73
+ "spec/app/config.ru",
74
+ "spec/app/config/application.rb",
75
+ "spec/app/config/boot.rb",
76
+ "spec/app/config/environment.rb",
77
+ "spec/app/config/environments/development.rb",
78
+ "spec/app/config/environments/test.rb",
79
+ "spec/app/config/initializers/backtrace_silencers.rb",
80
+ "spec/app/config/initializers/inflections.rb",
81
+ "spec/app/config/initializers/mime_types.rb",
82
+ "spec/app/config/initializers/secret_token.rb",
83
+ "spec/app/config/initializers/session_store.rb",
84
+ "spec/app/config/locales/en.yml",
85
+ "spec/app/config/routes.rb",
86
+ "spec/app/db/seeds.rb",
87
+ "spec/app/lib/assets/.gitkeep",
88
+ "spec/app/lib/tasks/.gitkeep",
89
+ "spec/app/log/.gitkeep",
90
+ "spec/app/public/404.html",
91
+ "spec/app/public/422.html",
92
+ "spec/app/public/500.html",
93
+ "spec/app/public/favicon.ico",
94
+ "spec/app/public/index.html",
95
+ "spec/app/public/javascripts/application.js",
96
+ "spec/app/public/robots.txt",
97
+ "spec/app/public/stylesheets/application.css",
98
+ "spec/app/script/rails",
99
+ "spec/app/spec/controllers/posts_controller_spec.rb",
100
+ "spec/app/spec/isolated_spec_helper.rb",
101
+ "spec/app/spec/spec_helper.rb",
102
+ "spec/app/spec/unit/controllers/posts_controller_isolated_spec.rb",
103
+ "spec/app/spec/unit/controllers/posts_controller_spec.rb",
104
+ "spec/app/test/functional/.gitkeep",
105
+ "spec/app/test/functional/posts_controller_test.rb",
106
+ "spec/app/test/isolated_test_helper.rb",
107
+ "spec/app/test/test_helper.rb",
108
+ "spec/app/test/unit/.gitkeep",
109
+ "spec/app/test/unit/controllers/posts_controller_isolated_test.rb",
110
+ "spec/app/test/unit/controllers/posts_controller_test.rb",
111
+ "spec/app/vendor/assets/javascripts/.gitkeep",
112
+ "spec/app/vendor/assets/stylesheets/.gitkeep",
113
+ "spec/app/vendor/plugins/.gitkeep",
114
+ "spec/controll/asssistant_spec.rb",
115
+ "spec/controll/command_spec.rb",
116
+ "spec/controll/commander_spec.rb",
117
+ "spec/controll/executor/notificator_spec.rb",
118
+ "spec/controll/flow_handler/control_spec.rb",
119
+ "spec/controll/flow_handler/redirect/action_spec.rb",
120
+ "spec/controll/flow_handler/redirect/mapper_spec.rb",
121
+ "spec/controll/flow_handler/redirect_spec.rb",
122
+ "spec/controll/flow_handler/render_spec.rb",
123
+ "spec/controll/helper/event_matcher_spec.rb",
124
+ "spec/controll/helper/hash_access_spec.rb",
125
+ "spec/controll/helper/notify_spec.rb",
126
+ "spec/controll/helper/params_spec.rb",
127
+ "spec/controll/helper/path_resolver_spec.rb",
128
+ "spec/controll/helper/session_spec.rb",
129
+ "spec/controll/helper_spec.rb",
130
+ "spec/controll/notify/base_spec.rb",
131
+ "spec/controll/notify/flash_spec.rb",
132
+ "spec/controll/notify/message_handler.rb",
133
+ "spec/controll/notify/typed_spec.rb",
134
+ "spec/functional_test_helper.rb",
135
+ "spec/helper.rb",
136
+ "spec/rspec_controller_class.rb",
137
+ "spec/rspec_functional_helper.rb",
138
+ "spec/rspec_helper.rb",
139
+ "spec/spec_helper.rb",
140
+ "spec/test_helper.rb",
141
+ "spec/unit/functional_test_helper_test.rb",
142
+ "spec/unit/macros_test.rb",
143
+ "spec/unit/mixin_test.rb",
144
+ "spec/unit/rspec_functional_helper.rb",
145
+ "spec/unit/rspec_helper_test.rb",
146
+ "spec/unit/test_helper_test.rb"
147
+ ]
148
+ s.homepage = "http://github.com/kristianmandrup/controll"
149
+ s.licenses = ["MIT"]
150
+ s.require_paths = ["lib"]
151
+ s.rubygems_version = "1.8.24"
152
+ s.summary = "Utils for handling complex Controller/Business logic"
153
+
154
+ if s.respond_to? :specification_version then
155
+ s.specification_version = 3
156
+
157
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
158
+ s.add_runtime_dependency(%q<hashie>, ["~> 1.2"])
159
+ s.add_runtime_dependency(%q<liquid>, [">= 0"])
160
+ s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
161
+ s.add_development_dependency(%q<rdoc>, [">= 3.12"])
162
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
163
+ s.add_development_dependency(%q<jeweler>, [">= 1.8.4"])
164
+ s.add_development_dependency(%q<simplecov>, [">= 0.5"])
165
+ else
166
+ s.add_dependency(%q<hashie>, ["~> 1.2"])
167
+ s.add_dependency(%q<liquid>, [">= 0"])
168
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
169
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
170
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
171
+ s.add_dependency(%q<jeweler>, [">= 1.8.4"])
172
+ s.add_dependency(%q<simplecov>, [">= 0.5"])
173
+ end
174
+ else
175
+ s.add_dependency(%q<hashie>, ["~> 1.2"])
176
+ s.add_dependency(%q<liquid>, [">= 0"])
177
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
178
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
179
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
180
+ s.add_dependency(%q<jeweler>, [">= 1.8.4"])
181
+ s.add_dependency(%q<simplecov>, [">= 0.5"])
182
+ end
183
+ end
184
+
data/lib/controll.rb ADDED
@@ -0,0 +1,14 @@
1
+ module Controll
2
+ end
3
+
4
+ require 'hashie'
5
+ require 'imperator-ext'
6
+ require 'controll/errors'
7
+ require 'controll/executor'
8
+ require 'controll/notify'
9
+ require 'controll/flow_handler'
10
+ require 'controll/helper'
11
+ require 'controll/command'
12
+ require 'controll/commander'
13
+ require 'controll/assistant'
14
+
@@ -0,0 +1,19 @@
1
+ module Controll
2
+ class Assistant
3
+ attr_reader :controller, :options
4
+
5
+ def initialize controller, options = {}
6
+ @controller = controller
7
+ @options = options
8
+ end
9
+ end
10
+ end
11
+
12
+ module Controll
13
+ class DelegateAssistant < Assistant
14
+ def method_missing(meth, *args, &block)
15
+ controller.send(meth, *args, &block)
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,24 @@
1
+ module Controll
2
+ class Command < ::Imperator::Command
3
+ module Delegation
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ delegate :session, :params, :notify, :error, :do_redirect, :do_render, to: :initiator
8
+
9
+ alias_method :redirect, :do_redirect
10
+ alias_method :render, :do_render
11
+ end
12
+ end
13
+
14
+ include Delegation
15
+ end
16
+
17
+ if defined?(::Mongoid)
18
+ module Mongoid
19
+ class Command < ::Imperator::Mongoid::Command
20
+ include Controll::Command::Delegation
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module Controll
2
+ # used to register commands for a controller
3
+ class Commander
4
+ # makes #command_method available
5
+ extend Imperator::Command::MethodFactory
6
+
7
+ attr_reader :controller, :options
8
+
9
+ def initialize controller, options = {}
10
+ @controller = controller
11
+ @options = options
12
+ end
13
+
14
+ def command name, *args
15
+ send "#{name}_command", *args
16
+ end
17
+
18
+ def command! name, *args
19
+ command(name, *args).perform
20
+ end
21
+ alias_method :use_command, :command!
22
+ alias_method :perform_command, :command!
23
+ end
24
+ end
@@ -0,0 +1 @@
1
+ class Controll::InvalidEvent < StandardError; end
@@ -0,0 +1,6 @@
1
+ module Controll
2
+ module Executor
3
+ autoload :Base, 'controll/executor/base'
4
+ autoload :Notificator, 'controll/executor/notificator'
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ module Controll
2
+ module Executor
3
+ class Base
4
+ attr_accessor :initiator, :options
5
+
6
+ def initialize initiator, options = {}
7
+ @initiator = initiator
8
+ @options = options
9
+ end
10
+
11
+ def method_missing(meth, *args, &block)
12
+ initiator.send(meth, *args, &block)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ require 'controll/executor/base'
2
+
3
+ module Controll::Executor
4
+ class Notificator < Base
5
+
6
+ # return last notification or :success as result
7
+ # Hashie::Mash.new(name: name, type: type, options: options)
8
+ def result
9
+ main_event
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require 'controll/flow_handler/errors'
2
+
3
+ module Controll
4
+ module FlowHandler
5
+ autoload :Base, 'controll/flow_handler/base'
6
+ autoload :Control, 'controll/flow_handler/control'
7
+ autoload :Redirect, 'controll/flow_handler/redirect'
8
+ autoload :Render, 'controll/flow_handler/render'
9
+ autoload :EventHelper, 'controll/flow_handler/event_helper'
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ module Controll::FlowHandler
2
+ class Base
3
+ attr_reader :path
4
+
5
+ def initialize path
6
+ @path = path.to_s
7
+ end
8
+
9
+ def perform controller
10
+ raise NotImplementedError, 'You must implement the #perform method'
11
+ end
12
+
13
+ class << self
14
+ def action event
15
+ raise NotImplementedError, 'You must implement the #action class method'
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,85 @@
1
+ module Controll::FlowHandler
2
+ class Control
3
+ class ActionEventError < StandardError; end
4
+
5
+ attr_reader :controller, :action_handlers
6
+
7
+ def initialize controller, action_handlers = []
8
+ @controller = controller
9
+ @action_handlers = action_handlers unless action_handlers.blank?
10
+ end
11
+
12
+ def execute
13
+ use_action_handlers
14
+ use_alternatives
15
+ use_fallback if !executed?
16
+ self
17
+ end
18
+
19
+ def action_handlers
20
+ @action_handlers ||= [:redirect, :render]
21
+ end
22
+
23
+ def executed?
24
+ @executed
25
+ end
26
+
27
+ protected
28
+
29
+ delegate :command!, to: :controller
30
+
31
+ # can be used to set up control logic that fall outside what can be done
32
+ # with the basic action_handlers but can not be considered fall-back.
33
+ def use_alternatives
34
+ end
35
+
36
+ def use_fallback
37
+ end
38
+
39
+ def event
40
+ raise NotImplementedError, 'You must define an #event method that at least returns an event (Symbol). You can use an Executor for this.'
41
+ end
42
+
43
+ def fallback_action
44
+ do_redirect root_url
45
+ end
46
+
47
+ NoEventsDefinedError = Controll::FlowHandler::Render::NoEventsDefinedError
48
+ NoRedirectionFoundError = Controll::FlowHandler::Redirect::NoRedirectionFoundError
49
+
50
+ def use_action_handlers
51
+ errors = []
52
+ action_handlers.each do |action_handler|
53
+ begin
54
+ action_handler_clazz = handler_class(action_handler)
55
+ next unless action_handler_clazz
56
+ action = action_handler_clazz.action(event)
57
+ execute_with action
58
+ return if executed?
59
+ rescue NoEventsDefinedError => e
60
+ errors << e
61
+ rescue NoRedirectionFoundError => e
62
+ errors << e
63
+ end
64
+ end
65
+ raise ActionEventError, "#{errors.join ','}" unless errors.empty?
66
+ end
67
+
68
+ def handler_class action_handler
69
+ clazz = "#{self.class}::#{action_handler.to_s.camelize}"
70
+ clazz.constantize
71
+ rescue NameError
72
+ nil
73
+ end
74
+
75
+ def execute_with action
76
+ return if !action
77
+ action.perform(controller)
78
+ executed!
79
+ end
80
+
81
+ def executed!
82
+ @executed = true
83
+ end
84
+ end
85
+ end