Jam_Func 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/Jam_Func.rb +253 -0
  3. metadata +72 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39e3b02e68b81c78af5de6a12f165166c988cee9
4
+ data.tar.gz: fd8c8d0b2fe46e70f2dd325cfc75f5195087bf01
5
+ SHA512:
6
+ metadata.gz: 2cc835b15ebfb05c4bd4b8b4d4043a53f58429a08fa1944bc35f4b625b4357c89206e9f1502dface212992ae50c024982e3f9cff1af9c146f0d1b8c572e8fd8b
7
+ data.tar.gz: bcd7883749f5d0f463c7f9c3f1c67bc19e8f7c66e78ef08566edfa03d0c2a9f10bf6429799fa7ee63c9738ec45af8993516ffda7737a3b6d2f22176d024b0da3
data/lib/Jam_Func.rb ADDED
@@ -0,0 +1,253 @@
1
+
2
+
3
+ class Jam_Func
4
+
5
+ module Helpers # ============================
6
+
7
+ WHITE = /\s+/
8
+
9
+ def canon_name(str)
10
+ return str.strip.gsub(WHITE, " ").upcase
11
+ end
12
+
13
+ end # === module ============================
14
+
15
+ include Helpers
16
+
17
+ def initialize *args
18
+ @ons = {}
19
+ @on_errs = {}
20
+
21
+ # generate .includes table (array)
22
+ @includes = args.flatten.uniq
23
+
24
+ # include itself in .includes
25
+ @includes.push(self)
26
+ end
27
+
28
+ def on(raw_name, func)
29
+ name = canon_name(raw_name)
30
+
31
+ if !@ons[name]
32
+ @ons[name] = {}
33
+ end
34
+
35
+ list = @ons[name]
36
+ list.push func
37
+
38
+ self
39
+ end
40
+
41
+ def on_error raw_name, func
42
+ @on_errs[canon_name(raw_name)] = func
43
+ self
44
+ end
45
+
46
+ def events_for raw_name
47
+ @ons[canon_name(raw_name)]
48
+ end
49
+
50
+ def run *args
51
+ args
52
+ .map { |v|
53
+
54
+ v.kind_of?(String) ?
55
+ self.events_for(v) :
56
+ v
57
+
58
+ }
59
+ .flatten
60
+ .each { |f| f() }
61
+
62
+ self
63
+ end
64
+
65
+ def list(raw_name, create_if_needed = false)
66
+ name = canon_name(raw_name);
67
+ if !@ons[name] && create_if_needed
68
+ @ons[name] = []
69
+ end
70
+
71
+ return @ons[name] || []
72
+ end
73
+
74
+ def entire_list_for(name)
75
+ [
76
+ list_with_includes('before ' + name),
77
+ list_with_includes(name),
78
+ list_with_includes('after ' + name)
79
+ ]
80
+ .flatten
81
+ end
82
+
83
+ def list_with_includes raw_name
84
+ @includes.map { |t|
85
+ (t == self) ?
86
+ t.list(raw_name) :
87
+ t.list_with_includes(raw_name)
88
+ }
89
+ .flatten
90
+ end
91
+
92
+ def on *args
93
+ func = args.pop
94
+ args.each { |name|
95
+ list(name, true).push(func)
96
+ }
97
+
98
+ self
99
+ end
100
+
101
+ def run_error *args
102
+ raise("Error handlers not found for: " + args[0]) if entire_list_for(args[0]).empty?
103
+ run *args
104
+ end
105
+
106
+ def run *args
107
+
108
+ funcs = args.select { |u|
109
+ u.kind_of?(String) || u.kind_of?(Proc)
110
+ }
111
+
112
+ str_funcs = funcs.select { |u|
113
+ u.kind_of? String
114
+ }
115
+
116
+ parent_run = args.detect { |u|
117
+ u.kind_of?(Jam_Func::Run)
118
+ }
119
+
120
+ non_data = [funcs, parent_run].flatten
121
+
122
+ # === grab and merge data objects ===
123
+ data = nil
124
+
125
+ args.each { |u|
126
+ if u.kind_of?(Hash)
127
+ if !data
128
+ data = u
129
+ else
130
+ data.merge!(u)
131
+ end
132
+ end
133
+ }
134
+
135
+ # ----------------------------------------------
136
+ # if run is called without any string funcs:
137
+ # Example:
138
+ # .run(parent_run, {}, func1, func2);
139
+ # .run(parent_run, func1, func2);
140
+ # ----------------------------------------------
141
+ if !str_funcs || str_funcs.empty?
142
+ t = Jam_Func.new
143
+ name = 'one-off'
144
+ funcs.each { |f|
145
+ t.on(name, f)
146
+ }
147
+
148
+ return t.run(*([parent_run, name, data].compact))
149
+ end
150
+
151
+ # === Process final results === --
152
+ jam = Jam_Func::Run.new(self, parent_run, (data || {}), funcs).run()
153
+
154
+ return jam.data[:val] unless jam.err
155
+
156
+ # === Run error if found === --
157
+ err_name = canon_name(jam.err_name)
158
+ err = jam.err
159
+ err_func = @on_errs[err_name]
160
+ return err_func.call(data || {}, err) if err_func
161
+
162
+ raise("No error handler found for: #{err_name} : #{err}")
163
+ end # === .run -----------------------
164
+
165
+ class Jam # ========================================================
166
+
167
+ attr_accessor :last
168
+ attr_reader :err_name, :err, :data, :last
169
+
170
+ def initialize data
171
+ self.last = nil
172
+ @err_name = nil
173
+ @err = nil
174
+ @data = data || {}
175
+ @val = nil
176
+ @last = nil
177
+ end
178
+
179
+ def val new_val
180
+ @val = new_val
181
+ @data[:val] = new_val
182
+ end
183
+
184
+ def _last val
185
+ @last = val
186
+ end
187
+
188
+ def error name, val
189
+ @err_name = name
190
+ @err = val
191
+ throw :jam_error
192
+ end
193
+
194
+ end # ==============================================================
195
+
196
+ # # ================================================================
197
+ # # ================== Run (private) ===============================
198
+ # # ================================================================
199
+
200
+ class Run
201
+
202
+ include Helpers
203
+
204
+ attr_reader :err, :err_name
205
+
206
+ def initialize(jam, parent_run, data, arr)
207
+
208
+ @jam = jam
209
+ @parent_run = parent_run
210
+ @data = data
211
+ @tasks = nil
212
+
213
+ @proc_list = arr.map { |n|
214
+ n.kind_of?(String) ?
215
+ canon_name(n) :
216
+ n
217
+ }
218
+
219
+ end # === initialize
220
+
221
+ def run
222
+ raise("Already running.") if @tasks
223
+
224
+ @tasks = []
225
+
226
+ @proc_list.each { |var|
227
+ if var.kind_of? String
228
+ @tasks.push(@jam.entire_list_for(var));
229
+ else
230
+ @tasks.push var
231
+ end
232
+ }
233
+
234
+ @tasks = @tasks.flatten
235
+
236
+ jam = Jam_Func::Jam.new(@data)
237
+
238
+ @tasks.detect { |func|
239
+ is_fine = catch :jam_error do
240
+ args = [jam.data, jam.last, jam].slice(0, func.arity)
241
+ jam._last func.call *args
242
+ true
243
+ end
244
+
245
+ !is_fine
246
+ }
247
+
248
+ jam
249
+ end # === def run
250
+ end # === class Run
251
+
252
+ end # === Jam_Func
253
+
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Jam_Func
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - da99
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bacon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: Bacon_Colored
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Run a group of functions with flow control.
42
+ email: da99@da99
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/Jam_Func.rb
48
+ homepage: http://www.github.com/da99/Jam_Func
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.0.7
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Not ready.
72
+ test_files: []