Class: Trigger
- Inherits:
-
Object
- Object
- Trigger
- Defined in:
- lib/triggerful.rb
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
(also: #list)
Returns the value of attribute callbacks.
-
#event_name ⇒ Object
(also: #event)
Returns the value of attribute event_name.
-
#event_returns ⇒ Object
Returns the value of attribute event_returns.
Instance Method Summary collapse
- #+(*callbacks) ⇒ Object
- #-(*callbacks) ⇒ Object
- #<<(callback) ⇒ Object
-
#add(*callbacks) ⇒ Object
add callback(s) to instance.
-
#at(index) ⇒ Object
fetch a callback at an index.
- #delete(callback) ⇒ Object
-
#delete_at(index) ⇒ Object
remove a callback from a specific index.
-
#empty? ⇒ Boolean
returns true if there are no callbacks or false otherwise.
-
#fetch(index, err_log = nil) ⇒ Object
fetch a callback at an index.
-
#first(count = 1) ⇒ Object
fetch the first callback(s) on event.
-
#include?(callback) ⇒ Boolean
returns true if specified callback exists in callbacks or false otherwise.
-
#index(callback) ⇒ Object
fetch index of a callback from instance.
-
#initialize(event, event_returns = false, *callbacks) ⇒ Trigger
constructor
event is the Proc, Method, Trigger, Symbol, or String that gets called before the callbacks event_returns determines whether or not the callbacks should receive the output of the event appended in the arguments *callbacks is any number of Procs, Methods, Triggers, Symbols, or Strings that will be called after the event.
-
#insert(index, *callbacks) ⇒ Object
insert a callback at a specific index.
-
#last(count = 1) ⇒ Object
fetch the last callback(s) on event.
-
#length ⇒ Object
(also: #size, #count)
fetch the number of callbacks.
-
#remove(*callbacks) ⇒ Object
remove specified callback(s) from instance.
-
#remove_all ⇒ Object
(also: #clear)
remove all callbacks from the event.
-
#remove_first(*callbacks) ⇒ Object
remove first callback for each specified callback(s) from instance.
-
#replace(*callbacks) ⇒ Object
replaces callbacks with a new splat of callbacks.
-
#reverse ⇒ Object
returns a reversed version of callbacks.
-
#reverse! ⇒ Object
returns a reversed version of callbacks and changes the original.
- #toggle_event_returns ⇒ Object
-
#trigger(*args) ⇒ Object
(also: #call)
fires the event and all callbacks normally.
Constructor Details
#initialize(event, event_returns = false, *callbacks) ⇒ Trigger
event is the Proc, Method, Trigger, Symbol, or String that gets called before the callbacks event_returns determines whether or not the callbacks should receive the output of the event appended in the arguments *callbacks is any number of Procs, Methods, Triggers, Symbols, or Strings that will be called after the event
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/triggerful.rb', line 5 def initialize event, event_returns = false, *callbacks @event_name, @event_returns, @callbacks = event, event_returns, callbacks @event = to_method(@event_name) case @event_returns when Proc, Method, Symbol, String, Trigger @callbacks.unshift(event_returns) @event_returns = false end update_callback_procs end |
Instance Attribute Details
#callbacks ⇒ Object Also known as: list
Returns the value of attribute callbacks.
146 147 148 |
# File 'lib/triggerful.rb', line 146 def callbacks @callbacks end |
#event_name ⇒ Object Also known as: event
Returns the value of attribute event_name.
146 147 148 |
# File 'lib/triggerful.rb', line 146 def event_name @event_name end |
#event_returns ⇒ Object
Returns the value of attribute event_returns.
146 147 148 |
# File 'lib/triggerful.rb', line 146 def event_returns @event_returns end |
Instance Method Details
#+(*callbacks) ⇒ Object
138 139 140 |
# File 'lib/triggerful.rb', line 138 def +(*callbacks) @callbacks + callbacks end |
#-(*callbacks) ⇒ Object
142 143 144 |
# File 'lib/triggerful.rb', line 142 def -(*callbacks) @callbacks - callbacks end |
#<<(callback) ⇒ Object
133 134 135 136 |
# File 'lib/triggerful.rb', line 133 def <<(callback) @callbacks << callback update_callback_procs end |
#add(*callbacks) ⇒ Object
add callback(s) to instance
32 33 34 35 |
# File 'lib/triggerful.rb', line 32 def add(*callbacks) @callbacks.concat(callbacks) update_callback_procs end |
#at(index) ⇒ Object
fetch a callback at an index
82 83 84 |
# File 'lib/triggerful.rb', line 82 def at(index) @callbacks.at(index) end |
#delete(callback) ⇒ Object
59 60 61 62 |
# File 'lib/triggerful.rb', line 59 def delete(callback) @callbacks.delete(callback) update_callback_procs end |
#delete_at(index) ⇒ Object
remove a callback from a specific index
65 66 67 68 |
# File 'lib/triggerful.rb', line 65 def delete_at(index) @callbacks.delete_at(index) update_callback_procs end |
#empty? ⇒ Boolean
returns true if there are no callbacks or false otherwise
107 108 109 |
# File 'lib/triggerful.rb', line 107 def empty? @callbacks.empty? end |
#fetch(index, err_log = nil) ⇒ Object
fetch a callback at an index
87 88 89 |
# File 'lib/triggerful.rb', line 87 def fetch(index, err_log = nil) @callbacks.fetch(index, err_log) end |
#first(count = 1) ⇒ Object
fetch the first callback(s) on event
92 93 94 |
# File 'lib/triggerful.rb', line 92 def first(count = 1) @callbacks.first(count) end |
#include?(callback) ⇒ Boolean
returns true if specified callback exists in callbacks or false otherwise
112 113 114 |
# File 'lib/triggerful.rb', line 112 def include?(callback) @callbacks.include?(callback) end |
#index(callback) ⇒ Object
fetch index of a callback from instance
77 78 79 |
# File 'lib/triggerful.rb', line 77 def index(callback) @callbacks.index(callback) end |
#insert(index, *callbacks) ⇒ Object
insert a callback at a specific index
38 39 40 41 |
# File 'lib/triggerful.rb', line 38 def insert(index, *callbacks) @callbacks.insert(index, *callbacks) update_callback_procs end |
#last(count = 1) ⇒ Object
fetch the last callback(s) on event
97 98 99 |
# File 'lib/triggerful.rb', line 97 def last(count = 1) @callbacks.last(count) end |
#length ⇒ Object Also known as: size, count
fetch the number of callbacks
102 103 104 |
# File 'lib/triggerful.rb', line 102 def length @callbacks.length end |
#remove(*callbacks) ⇒ Object
remove specified callback(s) from instance
44 45 46 47 48 49 |
# File 'lib/triggerful.rb', line 44 def remove(*callbacks) callbacks.each do |callback| @callbacks.delete(callback) end update_callback_procs end |
#remove_all ⇒ Object Also known as: clear
remove all callbacks from the event
71 72 73 74 |
# File 'lib/triggerful.rb', line 71 def remove_all @callbacks = [] update_callback_procs end |
#remove_first(*callbacks) ⇒ Object
remove first callback for each specified callback(s) from instance
52 53 54 55 56 57 |
# File 'lib/triggerful.rb', line 52 def remove_first(*callbacks) callbacks.each do |callback| @callbacks.delete_at(@callbacks.index(callback) || @callbacks.length) end update_callback_procs end |
#replace(*callbacks) ⇒ Object
replaces callbacks with a new splat of callbacks
117 118 119 120 |
# File 'lib/triggerful.rb', line 117 def replace(*callbacks) @callbacks.replace(callbacks) update_callback_procs end |
#reverse ⇒ Object
returns a reversed version of callbacks
123 124 125 |
# File 'lib/triggerful.rb', line 123 def reverse @callbacks.reverse end |
#reverse! ⇒ Object
returns a reversed version of callbacks and changes the original
128 129 130 131 |
# File 'lib/triggerful.rb', line 128 def reverse! @callbacks.reverse! update_callback_procs end |
#toggle_event_returns ⇒ Object
27 28 29 |
# File 'lib/triggerful.rb', line 27 def toggle_event_returns @event_returns = !@event_returns end |
#trigger(*args) ⇒ Object Also known as: call
fires the event and all callbacks normally
19 20 21 22 23 24 25 |
# File 'lib/triggerful.rb', line 19 def trigger(*args) arguments = *args.dup event_data = @event.call(*args) arguments << event_data if @event_returns @callback_procs.each{ |c| c.call(*arguments) } event_data end |