Permalink
Browse files

better specs

  • Loading branch information...
kristianmandrup committed Aug 20, 2012
1 parent 488e02e commit 145e06b8dac194981838a38d748c9542e98601de
@@ -1,4 +1,7 @@
module Controll
+ autoload :SessionAssistant, 'controll/assistant/session_assistant'
+ autoload :ParamAssistant, 'controll/assistant/param_assistant'
+
class Assistant
attr_reader :controller, :options
@@ -8,7 +11,7 @@ def initialize controller, options = {}
end
def self.controller_methods *names
- delegate names, to: :controller
+ delegate *names, to: :controller
end
end
end
@@ -0,0 +1,23 @@
+module Controll
+ class ParamAssistant < Assistant
+ include Controll::Helper::Params
+
+ controller_methods :params
+
+ module Helper
+ extend ActiveSupport::Concern
+
+ def param name
+ param_assistant.send(name)
+ end
+
+ module ClassMethods
+ def param_assistant clazz
+ define_method :param_assistant do
+ @param_assistant ||= clazz.new self
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,23 @@
+module Controll
+ class SessionAssistant < Assistant
+ include Controll::Helper::Session
+
+ controller_methods :session
+
+ module Helper
+ extend ActiveSupport::Concern
+
+ def sess name
+ session_assistant.send(name)
+ end
+
+ module ClassMethods
+ def session_assistant clazz
+ define_method :session_assistant do
+ @session_assistant ||= clazz.new self
+ end
+ end
+ end
+ end
+ end
+end
View
@@ -1,16 +1,19 @@
module Controll
module Enabler
- autoload :Macros, 'controll/enabler/macros'
+ autoload :Macros, 'controll/enabler/macros'
+ autoload :PathResolver, 'controll/enabler/path_resolver'
- class NotIncluded < StandardError; end
+ extend ActiveSupport::Concern
- include Controll::Helper::Notify
- include Controll::Helper::Params
- include Controll::Enabler::Macros
+ class NotIncluded < StandardError; end
- delegate :command, :command!, :use_command, to: :commander
+ included do
+ include Controll::Helper::Notify
+ include Controll::Helper::Params
+ include Macros
- extend ActiveSupport::Concern
+ delegate :command, :command!, :use_command, to: :commander
+ end
module ClassMethods
def redirect_map map = {}
@@ -1,5 +1,5 @@
module Controll
- module Helper
+ module Enabler
class PathResolver
class PathResolverError < StandardError; end
@@ -9,22 +9,23 @@ def initialize caller
@caller = caller
end
- def extract_path type, *args
- if args.first.kind_of?(Symbol)
+ 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 args.first
- resolve_path type
- else
- args.empty? ? resolve_path(type) : args.first
+ caller.notice path
end
+ resolve_path map_type
end
- def resolve_path type
+ def resolve_path map_type
raise "Caller must have a #main_event method" unless caller.respond_to? :main_event
- caller.send(type).each do |path, events|
+ 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}"
+ raise PathResolverError, "Path could not be resolved for: #{event.name}"
end
protected
View
@@ -3,9 +3,12 @@ class Event
autoload :Helper, 'controll/event/helper'
autoload :Matcher, 'controll/event/matcher'
+ class InvalidError < StandardError; end
+
attr_reader :name, :type, :options
def initialize name, *args
+ raise ArgumentError, "Event must have a name identifier" if name.blank?
@name = name.to_sym
@options = args.extract_options!
@type = (extract_type(args.first) || options[:type] || :notice).to_sym
@@ -2,6 +2,8 @@ module Controll
module Event::Helper
def normalize event
case event
+ when Controll::Event
+ event
when Symbol
create_event event
when Hash, Hashie::Mash
@@ -13,6 +13,7 @@ module FlowHandler
end
module FlowHandlers
- Base = Controll::FlowHandler::Base
- Master = Controll::FlowHandler::Master
+ Action = Controll::FlowHandler::Action
+ PathAction = Controll::FlowHandler::Action
+ Master = Controll::FlowHandler::Master
end
@@ -5,10 +5,11 @@ class Master
include Macros
- attr_reader :controller, :action
+ attr_reader :controller, :action, :options
- def initialize controller
+ def initialize controller, options = {}
@controller = controller
+ @options = options
end
# Uses Executor to execute each registered ActionHandler, such ad Renderer and Redirecter
View
@@ -3,7 +3,6 @@ module Helper
autoload :HashAccess, 'controll/helper/hash_access'
autoload :Notify, 'controll/helper/notify'
autoload :Params, 'controll/helper/params'
- autoload :PathResolver, 'controll/helper/path_resolver'
autoload :Session, 'controll/helper/session'
end
end
@@ -7,6 +7,7 @@ module Notify
Controll::Event.valid_types.each do |type|
meth = "create_#{type}"
define_method meth do |*args|
+ return create_event type, type if args.empty?
create_event args.first, type, args[1..-1]
end
alias_method type, meth
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+class ParamAssistant < Controll::ParamAssistant
+ param_methods :id
+end
+
+class MyController
+ include Controll::ParamAssistant::Helper
+
+ def params
+ {:id => 7}
+ end
+
+ param_assistant ::ParamAssistant
+end
+
+describe Controll::Assistant do
+ context 'Param assistant' do
+ subject { ParamAssistant.new controller }
+
+ let(:controller) { MyController.new }
+
+ its(:controller) { should == controller }
+ its(:params) { should == {:id => 7} }
+ its(:id) { should == 7 }
+
+ specify do
+ controller.param(:id).should == 7
+ end
+ end
+end
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+class SessionAssistant < Controll::SessionAssistant
+ session_methods :name
+end
+
+class MyController
+ include Controll::SessionAssistant::Helper
+
+ def session
+ {:name => 'kris'}
+ end
+
+ session_assistant ::SessionAssistant
+end
+
+describe Controll::Assistant do
+ context 'Session assistant' do
+ subject { SessionAssistant.new controller }
+
+ let(:controller) { MyController.new }
+
+ its(:controller) { should == controller }
+ its(:session) { should == {:name => 'kris'} }
+ its(:name) { should == 'kris' }
+
+ specify do
+ controller.sess(:name).should == 'kris'
+ end
+ end
+end
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+class MyAssistant < Controll::Assistant
+ controller_methods :params
+end
+
+class MyController
+ def params
+ {:id => 7}
+ end
+
+ def my_assistant
+ @my_assistant ||= MyAssistant.new self
+ end
+end
+
+describe Controll::Assistant do
+ context 'My assistant with params delegation' do
+ subject { MyAssistant.new controller }
+
+ let(:controller) { MyController.new }
+
+ its(:controller) { should == controller }
+ its(:params) { should == {:id => 7} }
+ end
+end
@@ -1,5 +0,0 @@
-require 'spec_helper'
-
-describe Controll::Assistant do
- pending 'TODO'
-end
@@ -0,0 +1,76 @@
+require 'spec_helper'
+
+class MyController
+ include Controll::Enabler::Macros
+
+ # Mocking!
+ def render path
+ path
+ end
+ alias_method :redirect_to, :render
+end
+
+module Executors
+ class Services < Controlled
+ end
+end
+
+module Commanders
+ class Services < Commander
+ end
+end
+
+module Notifiers
+ class Services < Typed
+ end
+end
+
+module Assistants
+ class Services < Assistant
+ end
+end
+
+module FlowHandlers
+ class Services < Master
+ end
+end
+
+describe Controll::Enabler do
+ subject { controller.new }
+ let(:controller) { MyController }
+
+ describe 'class level macros' do
+
+ describe '.commander name, options = {}' do
+ before :all do
+ controller.commander :services
+ end
+
+ its(:commander) { should be_a Commanders::Services }
+ end
+
+ describe '.commander name, options = {}' do
+ before :all do
+ controller.notifier :services
+ end
+
+ its(:notifier) { should be_a Notifiers::Services }
+ end
+
+ describe '.assistant name, options = {}' do
+ before :all do
+ controller.assistant :services
+ end
+
+ its(:assistant) { should be_a Assistants::Services }
+ end
+
+ describe '.flow_handler name, options = {}' do
+ before :all do
+ controller.flow_handler :services
+ end
+
+ its(:flow_handler) { should be_a FlowHandlers::Services }
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 145e06b

Please sign in to comment.