Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

nice refactor and bug/error fixes

  • Loading branch information...
commit d8a80dde3560261ac1910532db7845a62248824a 1 parent b2e3332
@kristianmandrup authored
View
2  lib/controll.rb
@@ -6,6 +6,7 @@ module Controll
require 'controll/errors'
require 'controll/event'
require 'controll/events'
+require 'controll/assistant'
require 'controll/executor'
require 'controll/notify'
require 'controll/flow_handler'
@@ -13,7 +14,6 @@ module Controll
require 'controll/enabler'
require 'controll/command'
require 'controll/commander'
-require 'controll/assistant'
require 'controll/macros'
require 'controll/engine' if defined?(::Rails::Engine)
View
12 lib/controll/enabler.rb
@@ -1,15 +1,11 @@
module Controll
module Enabler
- autoload :Notify, 'controll/helper/notify'
- autoload :HashAccess, 'controll/helper/hash_access'
- autoload :Params, 'controll/helper/params'
- autoload :Session, 'controll/helper/session'
- autoload :PathResolver, 'controll/helper/path_resolver'
- autoload :EventMatcher, 'controll/helper/event_matcher'
+ autoload :Macros, 'controll/enabler/macros'
+
+ class NotIncluded < StandardError; end
include Controll::Helper::Notify
- include Controll::Helper::Params
-
+ include Controll::Helper::Params
include Controll::Enabler::Macros
delegate :command, :command!, :use_command, to: :commander
View
92 lib/controll/enabler/macros.rb
@@ -1,64 +1,66 @@
module Controll
- module Macros
- extend ActiveSupport::Concern
+ module Enabler
+ module Macros
+ extend ActiveSupport::Concern
- module ClassMethods
+ module ClassMethods
- # on Controller:
- # - fx Admin::ServicesController -> :services
- # On Focused Controller action:
- # - fx Admin::ServicesController::Update -> :update_services
- def controll *artifacts
- def name_of clazz
- clazz = clazz.to_s
- if clazz =~ /Controller$/
- clazz.sub(/Controller$/, '').split('::').last.underscore
- else
- parts = clazz.sub(/Controller/, '').split('::')
- (parts.pop << parts.last).underscore
+ # on Controller:
+ # - fx Admin::ServicesController -> :services
+ # On Focused Controller action:
+ # - fx Admin::ServicesController::Update -> :update_services
+ def controll *artifacts
+ def name_of clazz
+ clazz = clazz.to_s
+ if clazz =~ /Controller$/
+ clazz.sub(/Controller$/, '').split('::').last.underscore
+ else
+ parts = clazz.sub(/Controller/, '').split('::')
+ (parts.pop << parts.last).underscore
+ end
end
- end
- options = artifacts.extract_options!
- name = name_of self.class
- artifacts.each do |artifact|
- send(artifact, name, options)
+ options = artifacts.extract_options!
+ name = name_of self.class
+ artifacts.each do |artifact|
+ send(artifact, name, options)
+ end
end
- end
- # TODO: refactor - all use exactly the same pattern - can be generated!
- def commander name, options = {}
- define_method :commander do
- instance_variable_get("@commander") || begin
- clazz = "Commanders::#{name.to_s.camelize}".constantize
- instance_variable_set "@commander", clazz.new(self, options)
+ # TODO: refactor - all use exactly the same pattern - can be generated!
+ def commander name, options = {}
+ define_method :commander do
+ instance_variable_get("@commander") || begin
+ clazz = "Commanders::#{name.to_s.camelize}".constantize
+ instance_variable_set "@commander", clazz.new(self, options)
+ end
end
end
- end
- def notifier name, options = {}
- define_method :notifier do
- instance_variable_get("@notifier") || begin
- clazz = "Notifiers::#{name.to_s.camelize}".constantize
- instance_variable_set "@notifier", clazz.new(self, options)
+ def notifier name, options = {}
+ define_method :notifier do
+ instance_variable_get("@notifier") || begin
+ clazz = "Notifiers::#{name.to_s.camelize}".constantize
+ instance_variable_set "@notifier", clazz.new(self, options)
+ end
end
end
- end
- def assistant name, options = {}
- define_method :assistant do
- unless instance_variable_get("@assistant")
- clazz = "Assistants::#{name.to_s.camelize}".constantize
- instance_variable_set "@assistant", clazz.new(self, options)
+ def assistant name, options = {}
+ define_method :assistant do
+ unless instance_variable_get("@assistant")
+ clazz = "Assistants::#{name.to_s.camelize}".constantize
+ instance_variable_set "@assistant", clazz.new(self, options)
+ end
end
end
- end
- def flow_handler name, options = {}
- define_method :flow_handler do
- unless instance_variable_get("@flow_handler")
- clazz = "FlowHandlers::#{name.to_s.camelize}".constantize
- instance_variable_set "@flow_handler", clazz.new(self, options)
+ def flow_handler name, options = {}
+ define_method :flow_handler do
+ unless instance_variable_get("@flow_handler")
+ clazz = "FlowHandlers::#{name.to_s.camelize}".constantize
+ instance_variable_set "@flow_handler", clazz.new(self, options)
+ end
end
end
end
View
4 lib/controll/executor.rb
@@ -1,13 +1,13 @@
module Controll
module Executor
autoload :Base, 'controll/executor/base'
+ autoload :Controlled, 'controll/executor/controlled'
autoload :Delegator, 'controll/executor/delegator'
- autoload :Notificator, 'controll/executor/notificator'
end
end
module Executors
Base = Controll::Executor::Base
Delegator = Controll::Executor::Delegator
- Notificator = Controll::Executor::Notificator
+ Controlled = Controll::Executor::Controlled
end
View
8 lib/controll/flow_handler/action_handler.rb
@@ -2,12 +2,8 @@ module Controll::FlowHandler
class ActionHandler < Base
def perform
raise BadPathError, "Bad path: #{path}" if path.blank?
- raise Controll::FlowHandler::ControllNotEnabled, "Controller is not enabled with Controll. Missing #{controller_action} method" unless controller.respond_to? controller_action
- controller.send controller_action, resolved_path
- end
-
- def controller_action
- :do_render
+ error_check!
+ controller.send controller_action, self
end
def resolved_path
View
14 lib/controll/flow_handler/base.rb
@@ -8,9 +8,21 @@ def initialize controller, path
end
class << self
- def action event
+ def action controller, event
raise NotImplementedError, 'You must implement the #action class method'
end
end
+
+ protected
+
+ def controller_action
+ @controller_action ||= "do_#{type}"
+ end
+
+ def error_check!
+ unless controller.respond_to? controller_action
+ raise Controll::Enabler::NotIncluded, "Controll::Enabler has not been included in #{controller.class}. Missing #{controller_action} method"
+ end
+ end
end
end
View
16 lib/controll/flow_handler/fallback.rb
@@ -7,9 +7,19 @@ def initialize controller, event
@event = event
end
- def perform
- raise Controll::NotEnabled, "Controller is not enabled with Controll. Missing #do_fallback method" unless controller.respond_to :do_fallback
- controller.do_fallback(event) if
+ def perform
+ error_check!
+ controller.do_fallback self
+ end
+
+ def type
+ :fallback
+ end
+
+ class << self
+ def action controller, event
+ self.new controller, event
+ end
end
end
end
View
10 lib/controll/flow_handler/master/executor.rb
@@ -1,8 +1,10 @@
module Controll::FlowHandler
- class Control
+ class Master
class Executor < Controll::Executor::Base
- NoEventsDefinedError = Controll::FlowHandler::NoEventsDefinedError
- NoRedirectionFoundError = Controll::FlowHandler::NoRedirectionFoundError
+ FlowHandler = Controll::FlowHandler
+
+ NoEventsDefinedError = FlowHandler::NoEventsDefinedError
+ NoRedirectionFoundError = FlowHandler::NoRedirectionFoundError
def initialize initiator, options = {}
super
@@ -35,7 +37,7 @@ def event
end
def fallback
- Fallback.new controller, event
+ FlowHandler::Fallback.new controller, event
end
def action_handlers
View
2  lib/controll/flow_handler/master/macros.rb
@@ -1,5 +1,5 @@
module Controll::FlowHandler
- class Control
+ class Master
module Macros
extend ActiveSupport::Concern
View
8 lib/controll/flow_handler/redirecter.rb
@@ -5,9 +5,9 @@ class Redirecter < ActionHandler
autoload :Action, 'controll/flow_handler/redirect/action'
autoload :Mapper, 'controll/flow_handler/redirect/mapper'
- def controller_action
- :do_redirect
- end
+ def type
+ :redirect
+ end
class << self
attr_writer :action_clazz
@@ -20,7 +20,7 @@ def inherited base
end
end
- def action event
+ def action controller, event
path = action_clazz.new(event, redirections, types).map
self.new controller, path unless path.blank?
end
View
9 lib/controll/flow_handler/renderer.rb
@@ -2,12 +2,11 @@
module Controll::FlowHandler
class Renderer < ActionHandler
- BadPathError = Controll::FlowHandler::BadPathError
NoEventsDefinedError = Controll::FlowHandler::NoEventsDefinedError
NoDefaultPathDefinedError = Controll::FlowHandler::NoDefaultPathDefinedError
- def controller_action
- :do_render
+ def type
+ :render
end
class << self
@@ -17,10 +16,10 @@ def inherited base
end
end
- def action event, path = nil
+ def action controller, event, path = nil
check!
event = normalize event
- self.new(path || default_path) if events.include? event.name
+ self.new(controller, path || default_path) if events.include? event.name
end
# http://bugs.ruby-lang.org/issues/1082
View
25 spec/controll/flow_handler/fallback_spec.rb
@@ -1,26 +1,33 @@
require 'spec_helper'
-class BasicFallback < Controll::FlowHandler::Fallback
- def perform
- 'hello'
+class FallbackController
+ def do_fallback fallback
+ "did a nice fallback :) - #{fallback.event.name}"
end
end
+class BasicFallback < Controll::FlowHandler::Fallback
+end
+
describe Controll::FlowHandler::Fallback do
- context 'override perform' do
- subject { clazz.new '/' }
+ context 'Basic default fallback' do
+ subject { clazz.new controller, event }
+
+ let(:controller) { FallbackController.new }
+ let(:clazz) { BasicFallback }
+ let(:event) { create_event :unknown, :notice }
- let(:clazz) { BasicFallback }
+ include Controll::Event::Helper
- describe '.action event' do
+ describe '.action controller, event' do
specify do
- clazz.action(:hello).should be_a Controll::FlowHandler::Fallback
+ clazz.action(controller, event).should be_a Controll::FlowHandler::Fallback
end
end
describe '.perform' do
specify do
- clazz.action(:hello).perform.should == 'hello'
+ clazz.action(controller, event).perform.should == "did a nice fallback :) - #{event.name}"
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.