triggerful 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 517ba6c637f620f99edd9a6ad33fa536ae2b7064
4
- data.tar.gz: cd8f10f45e3d1b6cf5a0d74975d87d5df35c4df1
3
+ metadata.gz: 9924e5a6de21a1d92e2447af8c77b528e7ebcfac
4
+ data.tar.gz: 50fb8c654671f3e945efaf4b4868b3ac985855ca
5
5
  SHA512:
6
- metadata.gz: e64c5a28098f8bc53dc91ec8f12e1896b5697905645727d521ff65c490a08ce6b900f814e3bc3e8af5a99504b074a62edc4b10a7119a3661f9d1e54147697487
7
- data.tar.gz: 77af13c34863c0c2f238a38f098427b6ec4f5baa278010515ff8d66abc9d5ff299f13bf340f6d79dc396329bc7daae1c193d5242daa30022d411d953dfe7350c
6
+ metadata.gz: 17fd69f8e4dca8215e521b61aaba65a77ff36c416a678dc18e9e7cdc297a6585b3b7d57f857e8f4c2f6df1b96203c685f1ea8a9ea6d2940739675663fe527925
7
+ data.tar.gz: dc8cb9db6eef6339a962a9fd941da64777331c0e865ef0535fc7da102827a79a199ef01a97acdcd368166809ebc177e1ed717a9c234683c287ae241ed47b8745
@@ -1,84 +1,43 @@
1
1
  # A more comprehensive list of methods that Triggerful supports
2
2
  require 'triggerful'
3
3
 
4
+ $errors = 0
4
5
  $foobar = 0
5
6
 
6
- def foo
7
- amount_of_change = rand(0..100)
8
- $foobar += amount_of_change
9
-
10
- # The return value will be appended to the arguments sent
11
- # to the callback.
12
- amount_of_change
7
+ def add(amount = 1)
8
+ $foobar += 1
13
9
  end
14
10
 
15
- def say_foobar(amount_of_change)
16
- puts "$foobar has increased by #{amount_of_change} and is now #{$foobar}"
11
+ def sub(amount = 1)
12
+ $foobar -= amount
17
13
  end
18
14
 
19
- # Set the second argument to true to append the return value
20
- # from the trigger to the callback.
21
- _foo = Trigger.new(:foo, true, :say_foobar)
22
- _foo.trigger
23
-
24
- # Sometimes you don't care about creating a new method the
25
- # normal way and you just want to use a Proc instead.
26
- _bar = Trigger.new(Proc.new{ $foobar += 1; 1 }, true, :say_foobar)
27
- _bar.trigger
28
-
29
- # You might even want to have a class's public method be
30
- # an event. To get a hold of that, use instance.public_method(:symbol)
31
- class FooBar
32
- def initialize amount
33
- @amount = amount
34
- end
35
-
36
- def add
37
- $foobar += @amount
38
- @amount
39
- end
40
-
41
- def subtract
42
- $foobar -= @amount
43
- @amount
44
- end
45
-
46
- def amount
47
- @amount
15
+ def err_unless(line = 'unknown line', statement = true)
16
+ if !statement
17
+ $errors += 1
18
+ puts "Incorrect value at #{line}"
48
19
  end
49
20
  end
50
21
 
51
- twenty = FooBar.new(20)
52
-
53
- # Note that FooBar.subtract has a return value, but you don't
54
- # need to turn progression on to use the callback.
55
- _twenty_add = Trigger.new(twenty.public_method(:add), true, :say_foobar)
56
- _twenty_subtract = Trigger.new(twenty.public_method(:subtract), Proc.new{ puts "$foobar has decreased by #{twenty.amount} and is now #{$foobar}" })
57
-
58
- _twenty_add.trigger
59
- _twenty_subtract.trigger
60
-
61
- # Triggers support a few methods that regular arrays support,
62
- # such as remove, delete_at, length, and index(symbol). If you
63
- # want an actual array of callbacks, then call Trigger.list
64
- _twenty_subtract.add(:foo)
65
- puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}\n----"
66
-
67
- _twenty_subtract.insert(1, _twenty_add)
68
- puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}\n----"
69
-
70
- _twenty_subtract.delete_at(0)
71
- puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}\n----"
72
-
73
- _twenty_subtract.delete_at(_twenty_subtract.index(:foo))
74
- puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}\n----"
75
-
76
- _twenty_subtract.remove_all
77
- puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}"
78
-
79
- # If you're ever unsure of what your event is called,
80
- # such as when you're using a Proc, then you may call
81
- # the Trigger.event to fetch that data
82
- puts "Proc event: #{_bar.event}"
83
- puts "Symbol event: #{_foo.event}"
84
- puts "Public Method event: #{_twenty_add.event}"
22
+ event = Trigger.new(:add, true, :sub)
23
+ err_unless __LINE__, event.trigger == 1
24
+ err_unless __LINE__, (event.add(:sub,:sub).is_a? Array)
25
+ err_unless __LINE__, event.trigger == 1
26
+ err_unless __LINE__, $foobar == -2
27
+ err_unless __LINE__, (event.remove_all == [])
28
+ err_unless __LINE__, event.insert(0, ->{ $foobar -= $foobar }).size == 1
29
+ err_unless __LINE__, (proc_callback = event.list[0]; proc_callback.is_a? Proc)
30
+ err_unless __LINE__, event.remove(proc_callback).empty?
31
+ err_unless __LINE__, event.toggle_event_returns == false
32
+ err_unless __LINE__, (event << :sub).length == 1
33
+ err_unless __LINE__, event.include?(:sub)
34
+ err_unless __LINE__, (event << ->{ $foobar -= $foobar }).count == 2
35
+ # this may be a bug within Ruby itself
36
+ err_unless __LINE__, event.first == [:sub]
37
+ err_unless __LINE__, event.reverse!.last == :sub
38
+
39
+ if $errors > 0
40
+ puts "Total errors: #{$errors}"
41
+ else
42
+ puts "No errors! Looking all right to me."
43
+ end
@@ -1,4 +1,3 @@
1
- # Triggerful usage
2
1
  require 'triggerful'
3
2
 
4
3
  $foobar = 0
@@ -11,24 +10,17 @@ def bar
11
10
  puts $foobar
12
11
  end
13
12
 
14
- # create Trigger and callback, then trigger the Trigger
13
+ # create Trigger with an event and callback, then trigger it
15
14
  _foo = Trigger.new(:foo, :bar)
16
15
  _foo.trigger
17
16
 
18
17
  # create a method to determine if bar has been called before
19
- $bar_called? = false
20
- def bar_called
21
- $bar_called = true
22
- end
23
-
24
- # create a callback for the callback
25
- _bar = Trigger.new(:bar, :bar_called)
18
+ $bar_called = false
19
+ _bar = Trigger.new(:bar, Proc.new{ $bar_called = true })
26
20
 
27
- # replace old callback with new one (note that no symbol was used for the callback)
28
- _foo.remove(:bar)
29
- _foo.add(_bar)
21
+ # replace old callback with new one
22
+ _foo.replace(_bar)
30
23
 
31
- # methods are not called when added to Trigger callbacks, only when triggered
32
- puts $bar_called?
24
+ puts $bar_called
33
25
  _foo.trigger
34
- puts $bar_called?
26
+ puts $bar_called
@@ -1,81 +1,76 @@
1
1
  class Trigger
2
- def initialize event, *callbacks
3
- @event, @callbacks = event, callbacks
2
+ # event is the Proc, Method, Trigger, Symbol, or String that gets called before the callbacks
3
+ # event_returns determines whether or not the callbacks should receive the output of the event appended in the arguments
4
+ # *callbacks is any number of Procs, Methods, Triggers, Symbols, or Strings that will be called after the event
5
+ def initialize event, event_returns = false, *callbacks
6
+ @event_name, @event_returns, @callbacks = event, event_returns, callbacks
7
+ @event = to_method(@event_name)
4
8
 
5
- # check to see if the second argument is either true or false
6
- # if not, then it must be a callback
7
- case @callbacks[0]
8
- when TrueClass
9
- @progression = true
10
- @callbacks.delete_at(0)
11
- when FalseClass
12
- @progression = false
13
- @callbacks.delete_at(0)
14
- else
15
- @progression = false
9
+ case @event_returns
10
+ when Proc, Method, Symbol, String, Trigger
11
+ @callbacks.unshift(event_returns)
12
+ @event_returns = false
16
13
  end
14
+
15
+ update_callback_procs
17
16
  end
18
17
 
19
18
  # fires the event and all callbacks normally
20
19
  def trigger(*args)
21
20
  arguments = *args.dup
22
- event_data = case @event
23
- when Proc, Method
24
- @event.call(*args)
25
- else
26
- method(@event).call(*args)
27
- end
28
-
29
- arguments << event_data if @progression
30
-
31
- silent_trigger(*arguments)
32
- end
33
-
34
- # triggers the callbacks without executing the original event
35
- def silent_trigger(*args)
36
- @callbacks.each do |callback|
37
- if callback.instance_of? Trigger
38
- callback.trigger(*args)
39
- else
40
- case callback
41
- when Proc, Method
42
- callback.call(*args)
43
- else
44
- method(callback).call(*args)
45
- end
46
- end
47
- end
21
+ event_data = @event.call(*args)
22
+ arguments << event_data if @event_returns
23
+ @callback_procs.each{ |c| c.call(*arguments) }
24
+ event_data
25
+ end
26
+
27
+ def toggle_event_returns
28
+ @event_returns = !@event_returns
48
29
  end
49
30
 
50
31
  # add callback(s) to instance
51
32
  def add(*callbacks)
52
33
  @callbacks.concat(callbacks)
34
+ update_callback_procs
53
35
  end
54
36
 
55
37
  # insert a callback at a specific index
56
38
  def insert(index, *callbacks)
57
- @callbacks.insert(index, callbacks)
39
+ @callbacks.insert(index, *callbacks)
40
+ update_callback_procs
58
41
  end
59
42
 
60
- # remove callback(s) from instance
43
+ # remove specified callback(s) from instance
61
44
  def remove(*callbacks)
45
+ callbacks.each do |callback|
46
+ @callbacks.delete(callback)
47
+ end
48
+ update_callback_procs
49
+ end
50
+
51
+ # remove first callback for each specified callback(s) from instance
52
+ def remove_first(*callbacks)
62
53
  callbacks.each do |callback|
63
54
  @callbacks.delete_at(@callbacks.index(callback) || @callbacks.length)
64
55
  end
56
+ update_callback_procs
65
57
  end
66
58
 
67
59
  def delete(callback)
68
60
  @callbacks.delete(callback)
61
+ update_callback_procs
69
62
  end
70
63
 
71
64
  # remove a callback from a specific index
72
65
  def delete_at(index)
73
66
  @callbacks.delete_at(index)
67
+ update_callback_procs
74
68
  end
75
69
 
76
70
  # remove all callbacks from the event
77
71
  def remove_all
78
72
  @callbacks = []
73
+ update_callback_procs
79
74
  end
80
75
 
81
76
  # fetch index of a callback from instance
@@ -89,8 +84,8 @@ class Trigger
89
84
  end
90
85
 
91
86
  # fetch a callback at an index
92
- def fetch(index, default = nil)
93
- @callbacks.fetch(index, default)
87
+ def fetch(index, err_log = nil)
88
+ @callbacks.fetch(index, err_log)
94
89
  end
95
90
 
96
91
  # fetch the first callback(s) on event
@@ -103,41 +98,11 @@ class Trigger
103
98
  @callbacks.last(count)
104
99
  end
105
100
 
106
- # fetch the event, which may be a Proc, Symbol, String, or Method
107
- def event
108
- @event
109
- end
110
-
111
101
  # fetch the number of callbacks
112
102
  def length
113
103
  @callbacks.length
114
104
  end
115
105
 
116
- # fetch the number of callbacks
117
- def count
118
- @callbacks.count
119
- end
120
-
121
- # fetch the number of callbacks
122
- def size
123
- @callbacks.size
124
- end
125
-
126
- # returns an array object of callbacks
127
- def list
128
- @callbacks
129
- end
130
-
131
- # alias for list
132
- def callbacks
133
- @callbacks
134
- end
135
-
136
- # remove all callbacks from the event
137
- def clear
138
- @callbacks = []
139
- end
140
-
141
106
  # returns true if there are no callbacks or false otherwise
142
107
  def empty?
143
108
  @callbacks.empty?
@@ -148,9 +113,10 @@ class Trigger
148
113
  @callbacks.include?(callback)
149
114
  end
150
115
 
151
- # replaces callbacks with a new array of callbacks
152
- def replace(callbacks)
116
+ # replaces callbacks with a new splat of callbacks
117
+ def replace(*callbacks)
153
118
  @callbacks.replace(callbacks)
119
+ update_callback_procs
154
120
  end
155
121
 
156
122
  # returns a reversed version of callbacks
@@ -161,5 +127,44 @@ class Trigger
161
127
  # returns a reversed version of callbacks and changes the original
162
128
  def reverse!
163
129
  @callbacks.reverse!
130
+ update_callback_procs
131
+ end
132
+
133
+ def <<(callback)
134
+ @callbacks << callback
135
+ update_callback_procs
136
+ end
137
+
138
+ def +(*callbacks)
139
+ @callbacks + callbacks
140
+ end
141
+
142
+ def -(*callbacks)
143
+ @callbacks - callbacks
144
+ end
145
+
146
+ attr_accessor :event_name, :event_returns, :callbacks
147
+ alias :list :callbacks
148
+ alias :event :event_name
149
+
150
+ alias_method :call, :trigger
151
+ alias_method :size, :length
152
+ alias_method :count, :length
153
+ alias_method :clear, :remove_all
154
+
155
+ private
156
+
157
+ # turns the array of Procs, Methods, Triggers, Symbols, and Strings into a callable array
158
+ def update_callback_procs
159
+ @callback_procs = @callbacks.map{ |c| to_method(c) }
160
+ @callbacks
161
+ end
162
+
163
+ # converts Triggers, Strings, and Symbols into objects that can be used with the .call method
164
+ def to_method(event)
165
+ case event
166
+ when Proc, Method, Trigger then event
167
+ else self.class.superclass.method(event)
168
+ end
164
169
  end
165
170
  end
metadata CHANGED
@@ -1,17 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: triggerful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Bob Garrett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-12 00:00:00.000000000 Z
11
+ date: 2013-10-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Create callbacks to methods that you can easily and dynamically create/remove.
14
- Quite useful for lightweight event based programming. Even the medic agrees.
13
+ description: |
14
+ Create callbacks to methods that you can easily and dynamically
15
+ create/remove. Quite useful for lightweight event based programming. Even
16
+ the medic agrees. If there's any kind of feature you think this gem is
17
+ missing or if there is a bug, feel free to email me at andrew@karbon.tk
18
+ any time. Special thanks to: My brother Matt, m_x
19
+ (http://codereview.stackexchange.com/users/24548/m-x), and Cary Swoveland
20
+ (http://codereview.stackexchange.com/users/30157/cary-swoveland)'
15
21
  email: andrew@karbon.tk
16
22
  executables: []
17
23
  extensions: []
@@ -33,7 +39,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
39
  requirements:
34
40
  - - '>='
35
41
  - !ruby/object:Gem::Version
36
- version: '0'
42
+ version: 2.0.0
37
43
  required_rubygems_version: !ruby/object:Gem::Requirement
38
44
  requirements:
39
45
  - - '>='