triggerful 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/example/game-logic.rb +2 -3
- data/example/methods.rb +6 -4
- data/lib/triggerful.rb +92 -57
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 517ba6c637f620f99edd9a6ad33fa536ae2b7064
|
4
|
+
data.tar.gz: cd8f10f45e3d1b6cf5a0d74975d87d5df35c4df1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e64c5a28098f8bc53dc91ec8f12e1896b5697905645727d521ff65c490a08ce6b900f814e3bc3e8af5a99504b074a62edc4b10a7119a3661f9d1e54147697487
|
7
|
+
data.tar.gz: 77af13c34863c0c2f238a38f098427b6ec4f5baa278010515ff8d66abc9d5ff299f13bf340f6d79dc396329bc7daae1c193d5242daa30022d411d953dfe7350c
|
data/example/game-logic.rb
CHANGED
@@ -5,7 +5,7 @@ class Player
|
|
5
5
|
def initialize name, power, toughness, crit
|
6
6
|
@name, @power, @toughness, @crit = name, power, toughness, crit
|
7
7
|
@health = 100
|
8
|
-
@gear
|
8
|
+
@gear = []
|
9
9
|
end
|
10
10
|
|
11
11
|
def name(*set)
|
@@ -93,8 +93,7 @@ def display_stats(*players)
|
|
93
93
|
end
|
94
94
|
|
95
95
|
attacked_event = Trigger.new(:attacked, true, :barbed_shield, fred.public_method(:to_s), will.public_method(:to_s))
|
96
|
-
attack_event
|
97
|
-
|
96
|
+
attack_event = Trigger.new(:attack, true, attacked_event)
|
98
97
|
|
99
98
|
# Arbitrarily equip fred with barbed_shield.
|
100
99
|
# Also, do not use equipment like this. You should really use classes.
|
data/example/methods.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# A more comprehensive list of methods that Triggerful supports
|
2
|
-
require '
|
2
|
+
require 'triggerful'
|
3
3
|
|
4
4
|
$foobar = 0
|
5
5
|
|
@@ -53,7 +53,7 @@ twenty = FooBar.new(20)
|
|
53
53
|
# Note that FooBar.subtract has a return value, but you don't
|
54
54
|
# need to turn progression on to use the callback.
|
55
55
|
_twenty_add = Trigger.new(twenty.public_method(:add), true, :say_foobar)
|
56
|
-
_twenty_subtract = Trigger.new(twenty.public_method(:
|
56
|
+
_twenty_subtract = Trigger.new(twenty.public_method(:subtract), Proc.new{ puts "$foobar has decreased by #{twenty.amount} and is now #{$foobar}" })
|
57
57
|
|
58
58
|
_twenty_add.trigger
|
59
59
|
_twenty_subtract.trigger
|
@@ -74,9 +74,11 @@ _twenty_subtract.delete_at(_twenty_subtract.index(:foo))
|
|
74
74
|
puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}\n----"
|
75
75
|
|
76
76
|
_twenty_subtract.remove_all
|
77
|
-
puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}
|
77
|
+
puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}"
|
78
78
|
|
79
79
|
# If you're ever unsure of what your event is called,
|
80
80
|
# such as when you're using a Proc, then you may call
|
81
81
|
# the Trigger.event to fetch that data
|
82
|
-
puts "Proc
|
82
|
+
puts "Proc event: #{_bar.event}"
|
83
|
+
puts "Symbol event: #{_foo.event}"
|
84
|
+
puts "Public Method event: #{_twenty_add.event}"
|
data/lib/triggerful.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
class Trigger
|
2
2
|
def initialize event, *callbacks
|
3
|
-
@callbacks = callbacks
|
4
|
-
@event = event
|
3
|
+
@event, @callbacks = event, callbacks
|
5
4
|
|
6
|
-
if
|
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
|
7
9
|
@progression = true
|
8
10
|
@callbacks.delete_at(0)
|
9
|
-
|
11
|
+
when FalseClass
|
10
12
|
@progression = false
|
11
13
|
@callbacks.delete_at(0)
|
12
14
|
else
|
@@ -14,58 +16,30 @@ class Trigger
|
|
14
16
|
end
|
15
17
|
end
|
16
18
|
|
19
|
+
# fires the event and all callbacks normally
|
17
20
|
def trigger(*args)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
event_data = @event.call
|
21
|
+
arguments = *args.dup
|
22
|
+
event_data = case @event
|
23
|
+
when Proc, Method
|
24
|
+
@event.call(*args)
|
23
25
|
else
|
24
|
-
|
25
|
-
end
|
26
|
-
@callbacks.each do |callback|
|
27
|
-
if callback.instance_of? Trigger
|
28
|
-
if @progression
|
29
|
-
callback.trigger(*args, event_data)
|
30
|
-
else
|
31
|
-
callback.trigger(*args)
|
32
|
-
end
|
33
|
-
else
|
34
|
-
case callback
|
35
|
-
when Proc
|
36
|
-
if @progression
|
37
|
-
callback.call(*args, event_data)
|
38
|
-
else
|
39
|
-
callback.call(*args)
|
40
|
-
end
|
41
|
-
when Method
|
42
|
-
if @progression
|
43
|
-
callback.call(*args, event_data)
|
44
|
-
else
|
45
|
-
callback.call(*args)
|
46
|
-
end
|
47
|
-
else
|
48
|
-
if @progression
|
49
|
-
method(callback).call(*args, event_data)
|
50
|
-
else
|
51
|
-
method(callback).call(*args)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
26
|
+
method(@event).call(*args)
|
55
27
|
end
|
28
|
+
|
29
|
+
arguments << event_data if @progression
|
30
|
+
|
31
|
+
silent_trigger(*arguments)
|
56
32
|
end
|
57
33
|
|
58
|
-
#triggers the callbacks without executing the original
|
34
|
+
# triggers the callbacks without executing the original event
|
59
35
|
def silent_trigger(*args)
|
60
36
|
@callbacks.each do |callback|
|
61
37
|
if callback.instance_of? Trigger
|
62
38
|
callback.trigger(*args)
|
63
39
|
else
|
64
40
|
case callback
|
65
|
-
when Proc
|
66
|
-
callback.call
|
67
|
-
when Method
|
68
|
-
callback.call
|
41
|
+
when Proc, Method
|
42
|
+
callback.call(*args)
|
69
43
|
else
|
70
44
|
method(callback).call(*args)
|
71
45
|
end
|
@@ -75,9 +49,10 @@ class Trigger
|
|
75
49
|
|
76
50
|
# add callback(s) to instance
|
77
51
|
def add(*callbacks)
|
78
|
-
@callbacks.concat
|
52
|
+
@callbacks.concat(callbacks)
|
79
53
|
end
|
80
54
|
|
55
|
+
# insert a callback at a specific index
|
81
56
|
def insert(index, *callbacks)
|
82
57
|
@callbacks.insert(index, callbacks)
|
83
58
|
end
|
@@ -89,42 +64,102 @@ class Trigger
|
|
89
64
|
end
|
90
65
|
end
|
91
66
|
|
67
|
+
def delete(callback)
|
68
|
+
@callbacks.delete(callback)
|
69
|
+
end
|
70
|
+
|
71
|
+
# remove a callback from a specific index
|
92
72
|
def delete_at(index)
|
93
73
|
@callbacks.delete_at(index)
|
94
74
|
end
|
95
75
|
|
76
|
+
# remove all callbacks from the event
|
96
77
|
def remove_all
|
97
78
|
@callbacks = []
|
98
79
|
end
|
99
80
|
|
100
|
-
# fetch
|
81
|
+
# fetch index of a callback from instance
|
101
82
|
def index(callback)
|
102
83
|
@callbacks.index(callback)
|
103
84
|
end
|
104
85
|
|
86
|
+
# fetch a callback at an index
|
87
|
+
def at(index)
|
88
|
+
@callbacks.at(index)
|
89
|
+
end
|
90
|
+
|
91
|
+
# fetch a callback at an index
|
92
|
+
def fetch(index, default = nil)
|
93
|
+
@callbacks.fetch(index, default)
|
94
|
+
end
|
95
|
+
|
96
|
+
# fetch the first callback(s) on event
|
97
|
+
def first(count = 1)
|
98
|
+
@callbacks.first(count)
|
99
|
+
end
|
100
|
+
|
101
|
+
# fetch the last callback(s) on event
|
102
|
+
def last(count = 1)
|
103
|
+
@callbacks.last(count)
|
104
|
+
end
|
105
|
+
|
106
|
+
# fetch the event, which may be a Proc, Symbol, String, or Method
|
105
107
|
def event
|
106
108
|
@event
|
107
109
|
end
|
108
110
|
|
111
|
+
# fetch the number of callbacks
|
109
112
|
def length
|
110
113
|
@callbacks.length
|
111
114
|
end
|
112
115
|
|
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
|
113
127
|
def list
|
114
128
|
@callbacks
|
115
129
|
end
|
116
130
|
|
131
|
+
# alias for list
|
117
132
|
def callbacks
|
118
133
|
@callbacks
|
119
134
|
end
|
135
|
+
|
136
|
+
# remove all callbacks from the event
|
137
|
+
def clear
|
138
|
+
@callbacks = []
|
139
|
+
end
|
140
|
+
|
141
|
+
# returns true if there are no callbacks or false otherwise
|
142
|
+
def empty?
|
143
|
+
@callbacks.empty?
|
144
|
+
end
|
145
|
+
|
146
|
+
# returns true if specified callback exists in callbacks or false otherwise
|
147
|
+
def include?(callback)
|
148
|
+
@callbacks.include?(callback)
|
149
|
+
end
|
150
|
+
|
151
|
+
# replaces callbacks with a new array of callbacks
|
152
|
+
def replace(callbacks)
|
153
|
+
@callbacks.replace(callbacks)
|
154
|
+
end
|
155
|
+
|
156
|
+
# returns a reversed version of callbacks
|
157
|
+
def reverse
|
158
|
+
@callbacks.reverse
|
159
|
+
end
|
160
|
+
|
161
|
+
# returns a reversed version of callbacks and changes the original
|
162
|
+
def reverse!
|
163
|
+
@callbacks.reverse!
|
164
|
+
end
|
120
165
|
end
|
121
|
-
|
122
|
-
=begin
|
123
|
-
A short scrib of notes for humans
|
124
|
-
|
125
|
-
Semantically speaking, caller, event, and trigger are synonymous.
|
126
|
-
The trigger that is triggered is similar to a javascript event
|
127
|
-
that is listened to. The callbacks would be called from the
|
128
|
-
caller, ie the event/trigger.
|
129
|
-
|
130
|
-
=end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: triggerful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
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-
|
11
|
+
date: 2013-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Create callbacks to methods that you can easily and dynamically create/remove.
|
14
14
|
Quite useful for lightweight event based programming. Even the medic agrees.
|