Permalink
Browse files

even more refactoring

  • Loading branch information...
1 parent 145e06b commit cc81c3a2487fd840019104b266f84033f7a3eba8 @kristianmandrup committed Aug 20, 2012
View
@@ -1,67 +1,51 @@
module Controll
module Enabler
autoload :Macros, 'controll/enabler/macros'
+ autoload :Notify, 'controll/enabler/notify'
autoload :PathResolver, 'controll/enabler/path_resolver'
+ autoload :PathHandler, 'controll/enabler/path_handler'
+ autoload :Maps, 'controll/enabler/maps'
extend ActiveSupport::Concern
class NotIncluded < StandardError; end
- included do
- include Controll::Helper::Notify
- include Controll::Helper::Params
+ included do
+ include Controll::Helper::Params
+ include Controll::Helper::Session
+
+ include Notify
include Macros
+ include Maps
delegate :command, :command!, :use_command, to: :commander
end
- module ClassMethods
- def redirect_map map = {}
- @redirect_map ||= map
- end
-
- def render_map map = {}
- @render_map ||= map
- end
-
- def assistant_methods *names
- options = names.extract_options!
- assistant = options[:to] || :assistant
- delegate names, to: assistant
- end
- end
-
# override this for custom Controller specific fallback action
- def do_fallback event
- do_redirect root_url
+ def do_fallback action
+ do_redirect fallback_path
end
def do_redirect *args
- options = args.extract_options!
- path = path_resolver.extract_path(:redirect_map, *args)
- process_notifications
- redirect_to path, *args
+ redirecter.execute *args
end
def do_render *args
- options = args.extract_options!
- path = path_resolver.extract_path :render_paths, *args
- process_notifications
- render path, *args
- end
+ renderer.execute *args
+ end
protected
- def redirect_map
- self.class.redirect_map
+ def fallback_path
+ root_url
end
- def render_paths
- self.class.render_map
+ def renderer
+ @renderer||= PathHandler.new self, :render
end
- def path_resolver
- @path_resolver ||= PathResolver.new self
+ def redirecter
+ @redirecter ||= PathHandler.new self, :redirect
end
end
end
@@ -46,6 +46,15 @@ def notifier name, options = {}
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)
+ end
+ end
+ end
+
def assistant name, options = {}
define_method :assistant do
unless instance_variable_get("@assistant")
@@ -55,14 +64,11 @@ def assistant name, options = {}
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)
- end
- end
- end
+ def assistant_methods *names
+ options = names.extract_options!
+ assistant = options[:to] || :assistant
+ delegate names, to: assistant
+ end
end
end
end
@@ -0,0 +1,15 @@
+module Controll::Enabler
+ module Maps
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+ def redirect_map map = {}
+ @redirect_map ||= map
+ end
+
+ def render_map map = {}
+ @render_map ||= map
+ end
+ end
+ end
+end
@@ -1,5 +1,5 @@
module Controll
- module Helper
+ module Enabler
module Notify
extend ActiveSupport::Concern
@@ -16,8 +16,8 @@ module Notify
include Controll::Event::Helper
- def notify name, *args
- events << create_event(name, *args)
+ def notify event, *args
+ events << normalize(event, *args)
self # enable method chaining on controller
end
@@ -0,0 +1,49 @@
+module Controll::Enabler
+ class PathHandler
+ attr_reader :controller, :type, :options, :path
+
+ def initialize controller, type
+ @controller, @type = [controller, type]
+ end
+
+ def map
+ @map ||= controller.class.send("#{type}_map")
+ end
+
+ alias_method :control_action, :type
+
+ def self.renderer controller, map
+ self.new controller, map, :render
+ end
+
+ def self.redirecter controller, map
+ self.new controller, map, :redirect
+ end
+
+ def execute *args
+ extract! *args
+ process_notifications
+ path ? handle_path : fallback
+ end
+
+ protected
+
+ def handle_path
+ controller.send control_action, path, options
+ end
+
+ def fallback
+ controller.do_fallback action
+ end
+
+ def extract! *args
+ @options = args.extract_options!
+ action = args.first
+ @path = path_resolver(map).resolve action
+ end
+
+ def path_resolver map
+ @path_resolver ||= PathResolver.new controller, map
+ end
+ end
+end
@@ -1,46 +1,44 @@
-module Controll
- module Enabler
- class PathResolver
- class PathResolverError < StandardError; end
-
- attr_reader :caller
-
- def initialize caller
- @caller = caller
- end
-
- def extract_path map_type, path = nil
- return resolve_path(map_type) if path.nil?
-
- case path
- when Symbol
- raise "Caller must have a notice method" unless caller.respond_to? :notice
- caller.notice path
- end
- resolve_path map_type
- end
-
- def resolve_path map_type
- raise "Caller must have a #main_event method" unless caller.respond_to? :main_event
- caller.send(map_type).each do |path, events|
- return path.to_s if matches? events
- end
- raise PathResolverError, "Path could not be resolved for: #{event.name}"
- end
-
- protected
-
- def matches? events
- event_matcher.match?(events)
- end
-
- def event_matcher
- @event_matcher ||= Controll::Event::Matcher.new event
- end
-
- def event
- @event ||= caller.main_event
- end
+module Controll::Enabler
+ class PathResolver
+ attr_reader :caller, :event_map
+
+ def initialize caller, event_map
+ @caller = caller
+ @event_map = event_map
+ end
+
+ def resolve action = nil
+ @path ||= handle action
+ end
+
+ protected
+
+ alias_method :controller, :caller
+
+ delegate :notify, :main_event, to: :caller
+
+ def handle action
+ case action
+ when Controll::FlowHandler::Fallback
+ nil
+ when Controll::FlowHandler::PathAction
+ action.resolved_path
+ when Symbol, Controll::Event
+ resolve_event action
+ else
+ resolve_event
+ end
+ rescue Controll::FlowHandler::PathActionError
+ nil
+ end
+
+ def resolve_event event = nil
+ notify event if event
+ mapper.map_event
end
+
+ def mapper
+ @mapper ||= Controll::FlowHandler::Mapper.new main_event, event_map
+ end
end
-end
+end
@@ -1,11 +1,11 @@
module Controll
module Event::Helper
- def normalize event
+ def normalize event, *args
case event
when Controll::Event
event
when Symbol
- create_event event
+ create_event event, *args
when Hash, Hashie::Mash
create_event event.delete(:name), event
else
@@ -9,6 +9,7 @@ module FlowHandler
autoload :Renderer, 'controll/flow_handler/renderer'
autoload :Fallback, 'controll/flow_handler/fallback'
autoload :EventHelper, 'controll/flow_handler/event_helper'
+ autoload :Mapper, 'controll/flow_handler/mapper'
end
end
@@ -1,11 +1,14 @@
module Controll::FlowHandler
class ActionEventError < StandardError; end
+ class PathActionError < StandardError; end
+
# Redirect
- class NoRedirectionFoundError < StandardError; end
+ class NoRedirectionFoundError < PathActionError; end
# Render
- class NoEventsDefinedError < StandardError; end
- class NoDefaultPathDefinedError < StandardError; end
- class BadPathError < StandardError; end
+ class NoEventsDefinedError < PathActionError; end
+ class NoDefaultPathDefinedError < PathActionError; end
+
+ class BadPathError < PathActionError; end
end
@@ -2,13 +2,13 @@ module Controll::FlowHandler
class Fallback < Action
attr_reader :controller, :event
- def initialize controller, event
+ def initialize controller, event = nil
@controller = controller
- @event = event
+ @event = event if event
end
def perform
- error_check!
+ error_check! if event
controller.do_fallback self
end
@@ -17,7 +17,7 @@ def type
end
class << self
- def action controller, event
+ def action controller, event = nil
self.new controller, event
end
end
Oops, something went wrong.

0 comments on commit cc81c3a

Please sign in to comment.