Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

master spec passes

  • Loading branch information...
commit 6b1a2e8f43c858f929853a7442a2adb91b036fbd 1 parent e166cfe
@kristianmandrup authored
View
24 lib/controll/flow_handler/action_mapper/complex.rb
@@ -4,7 +4,7 @@ class Complex < PathAction
class << self
attr_writer :action_clazz
- attr_reader :types
+ attr_reader :types, :event_maps
# this method could be generated whenever a class inherits from ActionHandler class?
def inherited base
@@ -13,33 +13,33 @@ def inherited base
end
end
- def action controller, event, maps, action_types = nil
+ def action controller, event
action_types ||= types
- path = path_finder(event, maps, action_types).map
+ path = path_finder(event).map
self.new controller, path unless path.blank?
end
# reader
- def map_for type = :notice
- @maps ||= {}
- @maps[type.to_sym] || {}
+ def event_map_for type = :notice
+ @event_maps ||= {}
+ event_maps[type.to_sym] || {}
end
# writer
# also auto-adds type to types
- def maps *args, &block
- @maps ||= {}
- return @maps if args.empty? && !block_given?
+ def event_map *args, &block
+ @event_maps ||= {}
+ return @event_maps if args.empty? && !block_given?
type = args.first.kind_of?(Symbol) ? args.shift : :notice
- @maps[type.to_sym] = block_given? ? yield : args.first
+ event_maps[type.to_sym] = block_given? ? instance_eval(&block) : args.first
@types << type unless types.include?(type)
end
protected
- def path_finder event, maps, types
- path_finder_class.new event, maps, types
+ def path_finder event
+ path_finder_class.new event, event_maps, types
end
def path_finder_class
View
11 lib/controll/flow_handler/action_mapper/path_action.rb
@@ -10,6 +10,17 @@ def resolved_path
controller.send(path)
end
+ protected
+
+ # useful for path helpers used in event maps
+ def method_missing(method_name, *args, &block)
+ if controller.respond_to? method_name
+ controller.send method_name, *args, &block
+ else
+ super
+ end
+ end
+
class << self
# do sth useful here?
def inherited base
View
2  lib/controll/flow_handler/errors.rb
@@ -1,5 +1,7 @@
module Controll::FlowHandler
class ActionEventError < StandardError; end
+
+ class EventNotImplementedError < StandardError; end
class PathActionError < StandardError; end
View
33 lib/controll/flow_handler/master.rb
@@ -22,30 +22,35 @@ def execute
end
def executor
- @executor ||= Executor.new controller, event: event, action_handlers: action_handlers
+ @executor ||= Executor.new controller, executor_options
+ end
+
+ def executor_options
+ {event: event, action_handlers: action_handlers}
end
class << self
def action_handlers
@action_handlers ||= []
-
- def valid_handler? handler_type
- valid_handlers.include? handler_type.to_sym
- end
-
- def valid_handlers
- [:renderer, :redirecter]
- end
-
- def mapper_types
- [:simple, :complex]
- end
end
def add_action_handler name
@action_handlers ||= []
@action_handlers << name.to_s.underscore.to_sym
end
+
+ def valid_handler? handler_type
+ raise ArgumentError, "Must be a String or Symbol, was: #{handler_type}" if handler_type.blank?
+ valid_handlers.include? handler_type.to_sym
+ end
+
+ def valid_handlers
+ [:renderer, :redirecter]
+ end
+
+ def mapper_types
+ [:simple, :complex]
+ end
end
protected
@@ -59,7 +64,7 @@ def action_handlers
end
def event
- raise NotImplementedError, 'You must define an #event method that at least returns an event (Symbol). You can use an Executor for this.'
+ raise Controll::FlowHandler::EventNotImplementedError, 'You must define an #event method that returns an event (or Symbol). You can use an Executor for this.'
end
end
end
View
8 lib/controll/flow_handler/master/executor.rb
@@ -3,8 +3,8 @@ class Master
class Executor < Controll::Executor::Base
FlowHandler = Controll::FlowHandler
- NoEventsDefinedError = FlowHandler::NoEventsDefinedError
- NoRedirectionFoundError = FlowHandler::NoRedirectionFoundError
+ NoEventsDefinedError = FlowHandler::NoEventsDefinedError
+ NoMappingFoundError = FlowHandler::NoMappingFoundError
def initialize initiator, options = {}
super
@@ -15,10 +15,10 @@ def execute
begin
action_handler_clazz = handler_class(action_handler)
next unless action_handler_clazz
- return action_handler_clazz.action(event)
+ return action_handler_clazz.action(controller, event)
rescue NoEventsDefinedError => e
errors << e
- rescue NoRedirectionFoundError => e
+ rescue NoMappingFoundError => e
errors << e
end
end
View
12 lib/controll/flow_handler/master/macros.rb
@@ -5,8 +5,8 @@ module Macros
module ClassMethods
def handler options = {}, &block
- mapper_type = :simple if options.delete(:simple)
- mapper_type ||= :complex if options(:complex)
+ mapper_type = :simple if options[:simple]
+ mapper_type ||= :complex if options[:complex]
master_clazz = Controll::FlowHandler::Master
@@ -14,13 +14,13 @@ def handler options = {}, &block
raise ArgumentError, "You must specify mapper type, one of: #{master_clazz.mapper_types} in: #{options}"
end
- handler_type = options[mapper_type]
+ handler_type = options.delete(mapper_type)
unless master_clazz.valid_handler? handler_type
raise ArgumentError, "Must one of: #{master_clazz.valid_handlers} was: #{handler_type}"
end
- parent = "Controll::FlowHandler::ActionMapper::#{mapper_type}".constantize
+ parent = "Controll::FlowHandler::ActionMapper::#{mapper_type.to_s.camelize}".constantize
clazz_name = handler_type.to_s.camelize
context = self.kind_of?(Class) ? self : self.class
@@ -37,11 +37,11 @@ def handler options = {}, &block
clazz
end
- def renderer mapper_type, options = {}, &block
+ def renderer mapper_type = :simple, options = {}, &block
handler options.merge(mapper_type => :renderer), &block
end
- def redirecter options = {}, &block
+ def redirecter mapper_type = :complex, options = {}, &block
handler options.merge(mapper_type => :redirecter), &block
end
View
9 spec/controll/flow_handler/master_spec.rb
@@ -30,7 +30,7 @@ def event
:update
end
- renderer do
+ renderer :simple do
events :update
default_path 'default'
end
@@ -41,7 +41,7 @@ def event
:update
end
- renderer do
+ renderer :simple do
events :create
default_path '/default'
end
@@ -65,7 +65,7 @@ def event
describe '.execute' do
specify do
- expect { subject.execute }.to_not raise_error
+ expect { subject.execute }.to raise_error(Controll::FlowHandler::EventNotImplementedError)
end
end
end
@@ -133,8 +133,7 @@ def event
end
specify do
- subject.execute
- subject.executed?.should be_true
+ subject.execute.should_not be_nil
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.