Permalink
Browse files

event class added

  • Loading branch information...
1 parent b506676 commit f5712932cea3c12d8957470aacb5e132fec3b176 @kristianmandrup committed Aug 17, 2012
View
@@ -4,6 +4,7 @@ module Controll
require 'hashie'
require 'imperator-ext'
require 'controll/errors'
+require 'controll/event'
require 'controll/executor'
require 'controll/notify'
require 'controll/flow_handler'
View
@@ -0,0 +1,48 @@
+module Controll
+ class Event
+ autoload :Helper, 'controll/event/helper'
+ autoload :Matcher, 'controll/event/matcher'
+
+ attr_reader :name, :type, :options
+
+ 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
+ @options.delete(:type) if options[:type] == @type
+ end
+
+ def self.valid_types
+ @valid_types ||= %w{notice error warning success}
+ end
+
+ valid_types.each do |type|
+ define_method :"#{type}?" do
+ self.type == type.to_sym
+ end
+ end
+
+ class << self
+ attr_writer :valid_types
+
+ def valid_type? type
+ valid_types.map(&:to_sym).include? type.to_sym
+ end
+
+ def add_valid_types *types
+ @valid_types += types if types.all? {|type| type.kind_of? Symbol}
+ end
+ end
+
+ protected
+
+ def extract_type arg
+ arg.to_sym if type? arg
+ end
+
+ def type? arg
+ arg.kind_of?(String) || arg.kind_of?(Symbol)
+ end
+ end
+end
@@ -1,18 +1,22 @@
-module Controll::FlowHandler
- module EventHelper
+module Controll
+ module Event::Helper
def types
@types ||= [:notice, :error]
end
def normalize event
case event
when Symbol
- Hashie::Mash.new name: event, type: :notice
+ create_event event
when Hash, Hashie::Mash
- event
+ create_event event.delete(:name), event
else
raise Controll::InvalidEvent, "Event: #{event} could not be normalized, must be a Hash or Symbol"
end
end
+
+ def create_event name, *args
+ Controll::Event.new name, *args
+ end
end
-end
+end
@@ -1,5 +1,5 @@
-module Controll::Helper
- class EventMatcher
+module Controll
+ class Event::Matcher
attr_reader :event
def initialize event
@@ -12,7 +12,7 @@ def match? events
protected
- include Controll::FlowHandler::EventHelper
+ include Controll::Event::Helper
def normalized events
[events].flatten.map(&:to_sym)
@@ -4,7 +4,7 @@ module Controll::Executor
class Notificator < Delegator
# return last notification or :success as result
- # Hashie::Mash.new(name: name, type: type, options: options)
+ # return <Event>
def result
main_event
end
@@ -5,7 +5,7 @@ class Action
NoRedirectionFoundError = Controll::FlowHandler::NoRedirectionFoundError
- # event is a Hashie::Mash or simply a Symbol (default notice event)
+ # event <Event>
def initialize event, redirections, types = []
raise ArgumentError, "Must take :event option, was: #{event}" if event.blank?
raise ArgumentError, "Must take non-empty :redirections option, was: #{redirections}" if redirections.blank?
@@ -29,6 +29,8 @@ def notify name, type = nil, options = {}
protected
+ include Controll::Event::Helper
+
def error name = :error, options = {}
notify name, :error, options
end
@@ -40,7 +42,7 @@ def success name = :success, options = {}
def create_notification name, type = nil, options = {}
type ||= :notice
raise ArgumentError, "Not a valid notification type: #{type}, must be one of: #{valid_notification_types}" unless valid_notification_type?(type)
- Hashie::Mash.new(name: name, type: type, options: options)
+ create_event name, type, options
end
alias_method :create_event, :create_notification
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+class Container
+ include Controll::Event::Helper
+end
+
+describe Controll::Event::Helper do
+ subject { Container.new }
+
+ let(:events) { %w{sign_in sign_out} }
+ let(:bad_events) { %w{bad stuff} }
+
+ let(:event) { 'sign_in' }
+ let(:bad_event) { 'unknown' }
+
+ describe '.initialize event' do
+ its(:event) { should == 'sign_in' }
+ end
+
+ describe '.match? events' do
+ specify { subject.match?(events).should be_true }
+
+ specify { subject.match?(bad_events).should be_false }
+ end
+end
@@ -1,7 +1,7 @@
require 'spec_helper'
-describe Controll::Helper::EventMatcher do
- subject { Controll::Helper::EventMatcher.new event }
+describe Controll::Event::Matcher do
+ subject { Controll::Event::Matcher.new event }
let(:events) { %w{sign_in sign_out} }
let(:bad_events) { %w{bad stuff} }
@@ -0,0 +1,66 @@
+require 'spec_helper'
+
+describe Controll::Event do
+ let(:clazz) { Controll::Event }
+
+ describe '.initialize name, *args' do
+ context 'implicit notice' do
+ subject { Controll::Event.new 'sign_in' }
+
+ its(:name) { should == :sign_in }
+ its(:type) { should == :notice }
+ its(:options) { should be_blank }
+ end
+
+ context 'explicit notice' do
+ subject { Controll::Event.new 'sign_in', :notice }
+
+ its(:name) { should == :sign_in }
+ its(:type) { should == :notice }
+ its(:options) { should be_blank }
+ end
+
+ context 'explicit error and options' do
+ subject { Controll::Event.new 'sign_in', :error, {:a => 7} }
+
+ its(:name) { should == :sign_in }
+ its(:type) { should == :error }
+ its(:options) { should == {:a => 7} }
+ end
+
+ context 'explicit warning type in options' do
+ subject { Controll::Event.new 'sign_in', {:type => :warning, :a => 7} }
+
+ its(:name) { should == :sign_in }
+ its(:type) { should == :warning }
+ its(:options) { should == {:a => 7} }
+
+ describe '.warning?' do
+ its(:warning?) { should be_true }
+ its(:notice?) { should be_false }
+ end
+ end
+ end
+
+ context 'class methods' do
+ subject { clazz }
+
+ describe '.valid_types' do
+ its(:valid_types) { should = %w{notice error warning success} }
+ end
+
+ describe '.valid_types=' do
+ before do
+ clazz.valid_types = [:notice]
+ end
+ its(:valid_types) { should = [:notice] }
+ end
+
+ describe '.add_valid_type' do
+ before do
+ clazz.add_valid_types :remote, :invalid
+ end
+ its(:valid_types) { should include(:remote, :invalid) }
+ end
+ end
+end

0 comments on commit f571293

Please sign in to comment.