Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

all specs pass :)

  • Loading branch information...
commit 5c0f9e9b3af02673ea396677a90f618b54075e93 1 parent 11310b7
@kristianmandrup authored
View
13 lib/controll/event.rb
@@ -12,12 +12,20 @@ def initialize name, *args
@name = name.to_sym
@options = args.extract_options!
@type = (extract_type(args.first) || options[:type] || :notice).to_sym
- raise ArgumentError, "Invalid type: #{@type}" unless self.class.valid_type? @type
+ raise ArgumentError, "Invalid type: #{@type} must be one of: #{self.class.valid_types}" unless self.class.valid_type? @type
@options.delete(:type) if options[:type] == @type
end
+
+ def self.default_valid_types
+ [:notice, :error, :warning, :success]
+ end
def self.valid_types
- @valid_types ||= [:notice, :error, :warning, :success]
+ @valid_types ||= default_valid_types
+ end
+
+ def self.reset_types
+ @valid_types = default_valid_types
end
valid_types.each do |type|
@@ -41,6 +49,7 @@ def add_valid_types *types
protected
def extract_type arg
+ return arg.type if arg.respond_to? :type
arg.to_sym if type? arg
end
View
5 lib/controll/events.rb
@@ -1,11 +1,12 @@
module Controll
class Events
include Enumerable
+ include Controll::Event::Helper
attr_reader :events
- def initialize events = []
- @events = events.select{|event| event.kind_of? Controll::Event }
+ def initialize *events
+ @events = events.flatten.map{|event| normalize event }
end
def each
View
2  lib/controll/flow/action_mapper/base.rb
@@ -2,7 +2,7 @@ module Controll::Flow::ActionMapper
class Base
class << self
def path_action_class
- Controll::Flow::PathAction
+ Controll::Flow::Action::PathAction
end
def action controller, event
View
3  lib/controll/flow/action_mapper/complex.rb
@@ -15,7 +15,7 @@ def inherited base
def action controller, event
action_types ||= types
- path = path_finder(event).map
+ path = path_finder(event).path
path_action_class.new controller, path unless path.blank?
end
@@ -40,6 +40,7 @@ def event_map *args, &block
protected
def path_finder event
+ raise Controll::Flow::NoMappingFoundError, "No event maps defined" if event_maps.blank?
path_finder_class.new event, event_maps, types
end
View
8 lib/controll/flow/event_mapper/path_finder.rb
@@ -7,17 +7,17 @@ class PathFinder
# event <Event>
def initialize event, maps, types = []
- raise ArgumentError, "Must take :event option, was: #{event}" if event.blank?
- raise ArgumentError, "Must take non-empty :maps option, was: #{maps}" if maps.blank?
+ raise ArgumentError, "Event argument must not be empty" if event.blank?
+ raise ArgumentError, "Maps argument must not be empty" if maps.blank?
@event = normalize event
@types = types unless types.blank?
@maps = maps
end
def path
- @path ||= mapper(event_map).map_event
+ @path ||= mapper.map_event
rescue StandardError => e
- raise NoMappingFoundError, "No event mapping could be found for: #{event} in: #{maps}. Cause: #{e}"
+ raise NoMappingFoundError, "No event mapping could be found for: #{event.inspect} in: #{maps}. Cause: #{e}"
end
protected
View
4 lib/controll/flow/event_mapper/util.rb
@@ -32,10 +32,10 @@ def valid_map? event_map
end
def valid? events
- matcher(event).match?(events)
+ matcher.match?(events)
end
- def matcher event
+ def matcher
@matcher ||= Controll::Event::Matcher.new event
end
end
View
35 spec/controll/enabler/path_resolver_spec.rb
@@ -20,24 +20,29 @@ def perfect
Action = Controll::Flow::Action
class Redirecter < ActionMapper::Complex
+ event_map 'inperfect' => [:cool, :sweet], 'away' => [:success]
end
class Renderer < ActionMapper::Simple
+ events %w{perfect}
end
describe Controll::Enabler::PathResolver do
+ def notice name
+ Hashie::Mash.new(name: name.to_sym, type: :notice)
+ end
+
+ def error name
+ Hashie::Mash.new(name: name.to_sym, type: :error)
+ end
+
let(:controller) { MySweetController.new }
let(:render_map) { {'perfect' => [:cool, :sweet], 'home' => [:success] }}
- let(:redirect_map) { {'inperfect' => [:cool, :sweet], 'away' => [:success] }}
+ let(:redirect_map) { {'inperfect' => [:cool, :sweet], 'away' => [:success] } }
- let(:redirect_action) { ActionMapper::Simple.action controller, redirect_path }
- let(:render_action) { ActionMapper::Simple.action controller, render_path }
let(:fallback_action) { Action::Fallback.new controller }
- let(:render_path) { 'perfect' }
- let(:redirect_path) { 'away' }
-
context 'initialize controller, render_map' do
subject { Controll::Enabler::PathResolver.new controller, render_map }
@@ -56,6 +61,12 @@ class Renderer < ActionMapper::Simple
end
end
+ describe '.resolve sweet' do
+ specify do
+ subject.resolve(:sweet).should == 'perfect'
+ end
+ end
+
describe '.resolve :uncool' do
specify do
subject.resolve(:uncool).should_not == 'perfect'
@@ -71,18 +82,6 @@ class Renderer < ActionMapper::Simple
subject.resolve(fallback_action).should == nil
end
end
-
- describe '.resolve Render Action' do
- specify do
- subject.resolve(render_action).should == render_path
- end
- end
-
- describe '.resolve Redirect Action' do
- specify do
- subject.resolve(redirect_action).should == redirect_path
- end
- end
end
end
end
View
6 spec/controll/events_spec.rb
@@ -6,8 +6,8 @@
include Controll::Event::Helper
- let(:a_success) { success }
- let(:a_warning) { warning }
+ let(:a_success) { create_event :success }
+ let(:a_warning) { create_event :warning }
describe 'class methods' do
subject { clazz }
@@ -16,7 +16,7 @@
end
describe 'last' do
- subject { Controll::Event.new a_success, a_warning }
+ subject { Controll::Events.new a_success, a_warning }
its(:last) { should == a_warning }
end
View
37 spec/controll/flow/action_mapper/complex_spec.rb
@@ -8,16 +8,21 @@ class ErrorBadRedirect < Controll::Flow::ActionMapper::Complex
event_map :error, bad: ['bad_payment', 'wrong_payment']
end
-def notification name
- Hashie::Mash.new(name: name.to_sym, type: :notice)
-end
-
-def error name
- Hashie::Mash.new(name: name.to_sym, type: :error)
+class TheController
end
describe Controll::Flow::ActionMapper::Complex do
+ def notification name
+ Hashie::Mash.new(name: name.to_sym, type: :notice)
+ end
+
+ def error name
+ Hashie::Mash.new(name: name.to_sym, type: :error)
+ end
+
+ let(:controller) { TheController.new }
+
describe 'class macros' do
before :all do
ErrorBadRedirect.event_map :error, crap: ['bad_payment', 'wrong_payment']
@@ -35,13 +40,13 @@ def error name
context 'use directly without subclassing' do
subject { clazz.new '/' }
- let(:clazz) { Controll::Flow::Redirect }
+ let(:clazz) { Controll::Flow::ActionMapper::Complex }
let(:hello) { notification :hello }
describe '.action event' do
specify do
- expect { clazz.action(:hello) }.to raise_error(Controll::Flow::Redirect::NoRedirectionFoundError)
+ expect { clazz.action(controller, :hello) }.to raise_error(Controll::Flow::NoMappingFoundError)
end
end
end
@@ -54,15 +59,15 @@ def error name
context 'has redirections' do
describe '.action event' do
specify do
- expect { clazz.action(:hello) }.to_not raise_error(Controll::Flow::Redirect::NoRedirectionFoundError)
+ expect { clazz.action(controller, :hello) }.to_not raise_error(Controll::Flow::NoMappingFoundError)
end
specify do
- clazz.action(:hello).should be_a HelloToWelcomeRedirect
+ clazz.action(controller, :hello).should be_a Controll::Flow::Action::PathAction
end
specify do
- clazz.action(:hi).path.should == 'welcome'
+ clazz.action(controller, :hi).path.should == 'welcome'
end
end
end
@@ -73,18 +78,22 @@ def error name
let(:clazz) { ErrorBadRedirect }
+ before do
+ Controll::Event.reset_types
+ end
+
context 'has error redirections' do
describe '.action event' do
specify do
- expect { clazz.action(:hello) }.to raise_error(Controll::Flow::NoRedirectionFoundError)
+ expect { clazz.action(controller, :hello) }.to raise_error(Controll::Flow::NoMappingFoundError)
end
specify do
- clazz.action(error :bad_payment).should be_a ErrorBadRedirect
+ clazz.action(controller, error(:bad_payment)).should be_a Controll::Flow::Action::PathAction
end
specify do
- clazz.action(error :wrong_payment).path.should == 'crap'
+ clazz.action(controller, error(:wrong_payment)).path.should == 'crap'
end
end
end
View
2  spec/controll/flow/action_mapper/fallback_spec.rb
@@ -14,7 +14,7 @@ class BasicFallbackAction < Controll::Flow::Action::Fallback
subject { clazz.new controller, event }
let(:controller) { FallbackController.new }
- let(:clazz) { BasicFallback }
+ let(:clazz) { BasicFallbackAction }
let(:event) { create_event :unknown, :notice }
include Controll::Event::Helper
View
35 spec/controll/flow/action_mapper/simple_spec.rb
@@ -17,22 +17,29 @@ class HelloRender < Controll::Flow::ActionMapper::Simple
default_path '/default'
end
+class NiceController
+end
+
def notification name
- Hashie::Mash.new(name: name.to_sym, type: :notification)
+ Hashie::Mash.new(name: name.to_sym, type: :notice)
end
describe Controll::Flow::ActionMapper::Simple do
+ Simple = Controll::Flow::ActionMapper::Simple
+ Complex = Controll::Flow::ActionMapper::Complex
+
+ let(:controller) { NiceController }
+ let(:hello) { notification :hello }
+
context 'use directly without sublclassing' do
subject { clazz.new '/' }
- let(:clazz) { Controll::Flow::Render }
-
- let(:hello) { notification :hello }
+ let(:clazz) { Simple }
describe '.action event' do
specify do
- expect { clazz.action(:hello) }.to raise_error(Controll::Flow::NoEventsDefinedError)
+ expect { clazz.action(controller, hello) }.to raise_error(Controll::Flow::NoEventsDefinedError)
end
end
end
@@ -45,7 +52,7 @@ def notification name
context 'empty events' do
describe '.action event' do
specify do
- expect { clazz.action(:hello) }.to raise_error(Controll::Flow::NoEventsDefinedError)
+ expect { clazz.action(controller, hello) }.to raise_error(Controll::Flow::NoEventsDefinedError)
end
end
end
@@ -62,22 +69,18 @@ def notification name
describe 'does not respond to hello' do
specify do
- expect { clazz.action(:hello) }.to_not raise_error(Controll::Flow::Render::NoEventsDefinedError)
- end
-
- specify do
- clazz.action(event).should == nil
+ expect { clazz.action(controller, event) }.to_not raise_error(Controll::Flow::NoEventsDefinedError)
end
specify do
- clazz.action(:hello).should == nil
+ expect { clazz.action(controller, event) }.to raise_error Controll::Flow::NoDefaultPathDefinedError
end
end
describe 'responds to hi' do
# default_path not implemented!
specify do
- expect { clazz.action(:hi) }.to raise_error NotImplementedError
+ expect { clazz.action(controller, :hi) }.to raise_error Controll::Flow::NoDefaultPathDefinedError
end
end
end
@@ -94,15 +97,15 @@ def notification name
context 'has events and default_path' do
describe '.action event' do
specify do
- clazz.action(event).should be_a HelloRender
+ clazz.action(controller, event).should be_a Controll::Flow::Action::PathAction
end
specify do
- clazz.action(event).path.should == default_path
+ clazz.action(controller, event).path.should == default_path
end
specify do
- clazz.action(event, 'other_path').path.should == 'other_path'
+ clazz.action(controller, event, 'other_path').path.should == 'other_path'
end
end
end
View
18 spec/controll/flow/event_mapper/path_finder_spec.rb
@@ -1,14 +1,14 @@
require 'spec_helper'
-def notice name
- Hashie::Mash.new(name: name.to_sym, type: :notice)
-end
+describe Controll::Flow::EventMapper::PathFinder do
+ def notice name
+ Hashie::Mash.new(name: name.to_sym, type: :notice)
+ end
-def error name
- Hashie::Mash.new(name: name.to_sym, type: :error)
-end
+ def error name
+ Hashie::Mash.new(name: name.to_sym, type: :error)
+ end
-describe Controll::Flow::EventMapper::PathFinder do
let(:redirections) do
{
:error => error_map, :notice => notice_map
@@ -37,11 +37,11 @@ def error name
subject { clazz.new hello, redirections, types }
specify do
- expect { subject.map }.to_not raise_error(Controll::Flow::NoRedirectionFoundError)
+ expect { subject.map }.to_not raise_error(Controll::Flow::NoMappingFoundError)
end
specify do
- subject.map.should == 'welcome'
+ subject.path.should == 'welcome'
end
end
end
View
14 spec/controll/flow/event_mapper/util_spec.rb
@@ -36,23 +36,23 @@ def error name
let(:bad_payment) { error :bad_payment }
describe '.initialize' do
- its(:event) { should == hello }
- its(:redirect_map) { should == notice_map }
+ its(:event) { should be_a Controll::Event }
+ its(:event_map) { should == notice_map }
end
- describe '.map' do
+ describe '.map_event' do
specify do
- subject.map.should == 'welcome'
+ subject.map_event.should == 'welcome'
end
end
- describe '.matcher event' do
+ describe '.matcher' do
specify do
- subject.send(:matcher, hello).should be_a Controll::Event::Matcher
+ subject.send(:matcher).should be_a Controll::Event::Matcher
end
specify do
- subject.send(:matcher, hello).event.should == hello
+ subject.send(:matcher).event.should be_a Controll::Event
end
end
View
11 spec/controll/flow/master_spec.rb
@@ -48,16 +48,9 @@ def event
end
end
-# ActionMapper = Controll::Flow::ActionMapper
-# Action = Controll::Flow::Action
-
-# PathAction = Action::PathAction
-# Fallback = Action::Fallback
-
-# Simple = ActionMapper::Simple
-# Complex = ActionMapper::Complex
-
describe Controll::Flow::Master do
+ Fallback = Controll::Flow::Action::Fallback
+
context 'use directly without sublclassing' do
subject { flow.new controller }
Please sign in to comment.
Something went wrong with that request. Please try again.