flor 0.9.5 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +10 -0
- data/Makefile +13 -5
- data/README.md +0 -35
- data/flor.gemspec +1 -0
- data/lib/flor.rb +6 -24
- data/lib/flor/changes.rb +26 -0
- data/lib/flor/colours.rb +65 -31
- data/lib/flor/conf.rb +84 -54
- data/lib/flor/core.rb +0 -23
- data/lib/flor/core/executor.rb +12 -42
- data/lib/flor/core/node.rb +19 -24
- data/lib/flor/core/procedure.rb +13 -24
- data/lib/flor/core/texecutor.rb +10 -28
- data/lib/flor/deep.rb +152 -0
- data/lib/flor/djan.rb +200 -0
- data/lib/flor/dollar.rb +0 -24
- data/lib/flor/errors.rb +0 -24
- data/lib/flor/flor.rb +43 -296
- data/lib/flor/id.rb +90 -0
- data/lib/flor/log.rb +12 -35
- data/lib/flor/migrations/0002_cunit_and_munit.rb +86 -0
- data/lib/flor/parser.rb +40 -46
- data/lib/flor/pcore/_arr.rb +0 -24
- data/lib/flor/pcore/_atom.rb +0 -24
- data/lib/flor/pcore/_att.rb +3 -25
- data/lib/flor/pcore/_dump.rb +0 -24
- data/lib/flor/pcore/_err.rb +0 -24
- data/lib/flor/pcore/_happly.rb +0 -24
- data/lib/flor/pcore/_obj.rb +0 -24
- data/lib/flor/pcore/_skip.rb +0 -24
- data/lib/flor/pcore/arith.rb +0 -24
- data/lib/flor/pcore/break.rb +0 -24
- data/lib/flor/pcore/case.rb +127 -0
- data/lib/flor/pcore/cmp.rb +0 -24
- data/lib/flor/pcore/cond.rb +24 -24
- data/lib/flor/pcore/cursor.rb +0 -24
- data/lib/flor/pcore/define.rb +0 -24
- data/lib/flor/pcore/fail.rb +0 -24
- data/lib/flor/pcore/if.rb +39 -0
- data/lib/flor/pcore/loop.rb +0 -24
- data/lib/flor/pcore/map.rb +0 -24
- data/lib/flor/pcore/match.rb +0 -24
- data/lib/flor/pcore/move.rb +0 -24
- data/lib/flor/pcore/noeval.rb +0 -24
- data/lib/flor/pcore/noret.rb +0 -24
- data/lib/flor/pcore/push.rb +1 -25
- data/lib/flor/pcore/rand.rb +59 -0
- data/lib/flor/pcore/sequence.rb +0 -24
- data/lib/flor/pcore/set.rb +0 -24
- data/lib/flor/pcore/stall.rb +0 -24
- data/lib/flor/pcore/until.rb +0 -24
- data/lib/flor/pcore/val.rb +0 -24
- data/lib/flor/punit/cancel.rb +0 -24
- data/lib/flor/punit/cmap.rb +0 -24
- data/lib/flor/punit/concurrence.rb +54 -24
- data/lib/flor/punit/every.rb +0 -24
- data/lib/flor/punit/graft.rb +41 -0
- data/lib/flor/punit/on.rb +0 -24
- data/lib/flor/punit/schedule.rb +0 -24
- data/lib/flor/punit/signal.rb +0 -24
- data/lib/flor/punit/sleep.rb +0 -24
- data/lib/flor/punit/task.rb +0 -26
- data/lib/flor/punit/trace.rb +0 -24
- data/lib/flor/punit/trap.rb +0 -24
- data/lib/flor/to_string.rb +4 -25
- data/lib/flor/tools/env.rb +0 -23
- data/lib/flor/tools/shell.rb +810 -0
- data/lib/flor/unit.rb +0 -23
- data/lib/flor/unit/executor.rb +35 -31
- data/lib/flor/unit/ganger.rb +9 -34
- data/lib/flor/unit/hooker.rb +5 -25
- data/lib/flor/unit/journal.rb +0 -23
- data/lib/flor/unit/loader.rb +63 -94
- data/lib/flor/unit/logger.rb +8 -27
- data/lib/flor/unit/models.rb +0 -24
- data/lib/flor/unit/models/execution.rb +13 -24
- data/lib/flor/unit/models/pointer.rb +0 -24
- data/lib/flor/unit/models/timer.rb +0 -24
- data/lib/flor/unit/models/trace.rb +0 -24
- data/lib/flor/unit/models/trap.rb +0 -24
- data/lib/flor/unit/scheduler.rb +157 -128
- data/lib/flor/unit/storage.rb +224 -167
- data/lib/flor/unit/taskers.rb +38 -25
- data/lib/flor/unit/waiter.rb +7 -26
- data/lib/flor/unit/wlist.rb +8 -24
- metadata +28 -7
- data/fail.txt +0 -16
- data/intercepted.txt +0 -123
- data/lib/flor/pcore/ife.rb +0 -56
- data/lib/flor/tools/repl.rb +0 -231
- data/out.txt +0 -206
data/lib/flor/dollar.rb
CHANGED
@@ -1,27 +1,3 @@
|
|
1
|
-
#--
|
2
|
-
# Copyright (c) 2015-2017, John Mettraux, jmettraux+flor@gmail.com
|
3
|
-
#
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
-
# of this software and associated documentation files (the "Software"), to deal
|
6
|
-
# in the Software without restriction, including without limitation the rights
|
7
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
-
# copies of the Software, and to permit persons to whom the Software is
|
9
|
-
# furnished to do so, subject to the following conditions:
|
10
|
-
#
|
11
|
-
# The above copyright notice and this permission notice shall be included in
|
12
|
-
# all copies or substantial portions of the Software.
|
13
|
-
#
|
14
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
-
# THE SOFTWARE.
|
21
|
-
#
|
22
|
-
# Made in Japan.
|
23
|
-
#++
|
24
|
-
|
25
1
|
|
26
2
|
module Flor
|
27
3
|
|
data/lib/flor/errors.rb
CHANGED
@@ -1,27 +1,3 @@
|
|
1
|
-
#--
|
2
|
-
# Copyright (c) 2015-2017, John Mettraux, jmettraux+flor@gmail.com
|
3
|
-
#
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
-
# of this software and associated documentation files (the "Software"), to deal
|
6
|
-
# in the Software without restriction, including without limitation the rights
|
7
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
-
# copies of the Software, and to permit persons to whom the Software is
|
9
|
-
# furnished to do so, subject to the following conditions:
|
10
|
-
#
|
11
|
-
# The above copyright notice and this permission notice shall be included in
|
12
|
-
# all copies or substantial portions of the Software.
|
13
|
-
#
|
14
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
-
# THE SOFTWARE.
|
21
|
-
#
|
22
|
-
# Made in Japan.
|
23
|
-
#++
|
24
|
-
|
25
1
|
|
26
2
|
class Flor::FlorError < StandardError
|
27
3
|
|
data/lib/flor/flor.rb
CHANGED
@@ -1,302 +1,11 @@
|
|
1
|
-
#--
|
2
|
-
# Copyright (c) 2015-2017, John Mettraux, jmettraux+flor@gmail.com
|
3
|
-
#
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
-
# of this software and associated documentation files (the "Software"), to deal
|
6
|
-
# in the Software without restriction, including without limitation the rights
|
7
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
-
# copies of the Software, and to permit persons to whom the Software is
|
9
|
-
# furnished to do so, subject to the following conditions:
|
10
|
-
#
|
11
|
-
# The above copyright notice and this permission notice shall be included in
|
12
|
-
# all copies or substantial portions of the Software.
|
13
|
-
#
|
14
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
-
# THE SOFTWARE.
|
21
|
-
#
|
22
|
-
# Made in Japan.
|
23
|
-
#++
|
24
|
-
|
25
1
|
|
26
2
|
module Flor
|
27
3
|
|
28
|
-
#
|
29
|
-
# deep
|
30
|
-
#
|
31
|
-
# functions for deep getting/setting in structures
|
32
|
-
|
33
|
-
def self.to_index(s)
|
34
|
-
|
35
|
-
return 0 if s == 'first'
|
36
|
-
return -1 if s == 'last'
|
37
|
-
|
38
|
-
i = s.to_i
|
39
|
-
return nil if i.to_s != s
|
40
|
-
|
41
|
-
i
|
42
|
-
end
|
43
|
-
|
44
4
|
def self.to_a(o)
|
45
5
|
|
46
6
|
o.nil? ? nil : Array(o)
|
47
7
|
end
|
48
8
|
|
49
|
-
def self.deep_get(o, k) # --> success(boolean), value
|
50
|
-
|
51
|
-
return [ true, o ] unless k
|
52
|
-
|
53
|
-
val = o
|
54
|
-
ks = k.split('.')
|
55
|
-
|
56
|
-
loop do
|
57
|
-
|
58
|
-
break unless kk = ks.shift
|
59
|
-
|
60
|
-
case val
|
61
|
-
when Array
|
62
|
-
i = to_index(kk)
|
63
|
-
return [ false, nil ] unless i
|
64
|
-
val = val[i]
|
65
|
-
when Hash
|
66
|
-
val = val[kk]
|
67
|
-
else
|
68
|
-
return [ false, nil ]
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
[ true, val ]
|
73
|
-
end
|
74
|
-
|
75
|
-
def self.deep_set(o, k, v) # --> [ success(boolean), value ]
|
76
|
-
|
77
|
-
lastdot = k.rindex('.')
|
78
|
-
path = lastdot && k[0..lastdot - 1]
|
79
|
-
key = lastdot ? k[lastdot + 1..-1] : k
|
80
|
-
|
81
|
-
b, col = deep_get(o, path)
|
82
|
-
|
83
|
-
return [ false, v ] unless b
|
84
|
-
|
85
|
-
case col
|
86
|
-
when Array
|
87
|
-
i = to_index(key)
|
88
|
-
return [ false, v ] unless i
|
89
|
-
col[i] = v
|
90
|
-
when Hash
|
91
|
-
col[key] = v
|
92
|
-
else
|
93
|
-
return [ false, v ]
|
94
|
-
end
|
95
|
-
|
96
|
-
[ true, v ]
|
97
|
-
end
|
98
|
-
|
99
|
-
def self.deep_has_key?(o, k)
|
100
|
-
|
101
|
-
val = o
|
102
|
-
ks = k.split('.')
|
103
|
-
|
104
|
-
loop do
|
105
|
-
|
106
|
-
kk = ks.shift
|
107
|
-
|
108
|
-
case val
|
109
|
-
when Array
|
110
|
-
i = to_index(kk)
|
111
|
-
return false unless i
|
112
|
-
return (i < 0 ? -i < val.length : i < val.length) if ks.empty?
|
113
|
-
val = val[i]
|
114
|
-
when Hash
|
115
|
-
return val.has_key?(kk) if ks.empty?
|
116
|
-
val = val[kk]
|
117
|
-
else
|
118
|
-
return false
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
#
|
124
|
-
# djan
|
125
|
-
#
|
126
|
-
# functions about the "djan" silly version of JSON
|
127
|
-
|
128
|
-
def self.to_djan(x, opts={})
|
129
|
-
|
130
|
-
opts[:cl] = opts[:color] || opts[:colour]
|
131
|
-
|
132
|
-
r =
|
133
|
-
case x
|
134
|
-
when nil then 'null'
|
135
|
-
when String then string_to_d(x, opts)
|
136
|
-
when Hash then object_to_d(x, opts)
|
137
|
-
when Array then array_to_d(x, opts)
|
138
|
-
when TrueClass then c_tru(x.to_s, opts)
|
139
|
-
when FalseClass then c_tru(x.to_s, opts)
|
140
|
-
else c_num(x.to_s, opts)
|
141
|
-
end
|
142
|
-
if opts[:inner]
|
143
|
-
opts.delete(:inner)
|
144
|
-
r = r[1..-2] if r[0, 1] == '[' || r[0, 1] == '{'
|
145
|
-
end
|
146
|
-
r
|
147
|
-
end
|
148
|
-
|
149
|
-
def self.s_to_d(x, opts={})
|
150
|
-
|
151
|
-
x.is_a?(String) ? x : to_djan(x, opts)
|
152
|
-
end
|
153
|
-
|
154
|
-
class << self
|
155
|
-
|
156
|
-
alias to_d to_djan
|
157
|
-
|
158
|
-
protected # somehow
|
159
|
-
|
160
|
-
# Black 0;30 Dark Gray 1;30
|
161
|
-
# Blue 0;34 Light Blue 1;34
|
162
|
-
# Green 0;32 Light Green 1;32
|
163
|
-
# Cyan 0;36 Light Cyan 1;36
|
164
|
-
# Red 0;31 Light Red 1;31
|
165
|
-
# Purple 0;35 Light Purple 1;35
|
166
|
-
# Brown 0;33 Yellow 1;33
|
167
|
-
# Light Gray 0;37 White 1;37
|
168
|
-
|
169
|
-
def c_inf(s, opts); opts[:cl] ? "[1;30m#{s}[0;0m" : s; end
|
170
|
-
def c_str(s, opts); opts[:cl] ? "[1;33m#{s}[0;0m" : s; end
|
171
|
-
def c_tru(s, opts); opts[:cl] ? "[0;32m#{s}[0;0m" : s; end
|
172
|
-
def c_fal(s, opts); opts[:cl] ? "[0;31m#{s}[0;0m" : s; end
|
173
|
-
def c_num(s, opts); opts[:cl] ? "[1;34m#{s}[0;0m" : s; end
|
174
|
-
|
175
|
-
def c_key(s, opts)
|
176
|
-
|
177
|
-
return s unless opts[:cl]
|
178
|
-
|
179
|
-
s.match(/\A".*"\z/) ?
|
180
|
-
"#{c_inf('"', opts)}[0;33m#{s[1..-2]}#{c_inf('"', opts)}" :
|
181
|
-
"[0;33m#{s}[0;0m"
|
182
|
-
end
|
183
|
-
|
184
|
-
def string_to_d(x, opts)
|
185
|
-
|
186
|
-
if (
|
187
|
-
x.match(/\A[^: \b\f\n\r\t"',()\[\]{}#\\]+\z/) == nil ||
|
188
|
-
x.to_i.to_s == x ||
|
189
|
-
x.to_f.to_s == x
|
190
|
-
)
|
191
|
-
"#{c_inf('"', opts)}#{c_str(x.inspect[1..-2], opts)}#{c_inf('"', opts)}"
|
192
|
-
else
|
193
|
-
c_str(x, opts)
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
def object_to_d(x, opts)
|
198
|
-
|
199
|
-
a = [ '{ ', ': ', ', ', ' }' ]
|
200
|
-
a = a.collect(&:strip) if x.empty? || opts[:compact]
|
201
|
-
a = a.collect { |s| c_inf(s, opts) }
|
202
|
-
a, b, c, d = a
|
203
|
-
|
204
|
-
a +
|
205
|
-
x.collect { |k, v|
|
206
|
-
"#{c_key(to_djan(k, {}), opts)}#{b}#{to_djan(v, opts)}"
|
207
|
-
}.join(c) +
|
208
|
-
d
|
209
|
-
end
|
210
|
-
|
211
|
-
def array_to_d(x, opts)
|
212
|
-
|
213
|
-
a = [ '[ ', ', ', ' ]' ]
|
214
|
-
a = a.collect(&:strip) if x.empty? || opts[:compact]
|
215
|
-
a = a.collect { |s| c_inf(s, opts) }
|
216
|
-
a, b, c = a
|
217
|
-
|
218
|
-
a + x.collect { |e| to_djan(e, opts) }.join(b) + c
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
|
223
|
-
#
|
224
|
-
# ids
|
225
|
-
#
|
226
|
-
# functions about exids, nids, sub_nids, ...
|
227
|
-
|
228
|
-
def self.split_fei(fei)
|
229
|
-
|
230
|
-
if m = fei.match(/\A([^-]+-[^-]+-\d+\.\d+\.[^-]+)-(.*)\z/)
|
231
|
-
[ m[1], m[2] ]
|
232
|
-
else
|
233
|
-
[ nil ]
|
234
|
-
end
|
235
|
-
end
|
236
|
-
|
237
|
-
def self.exid(fei)
|
238
|
-
|
239
|
-
split_fei(fei).first
|
240
|
-
end
|
241
|
-
|
242
|
-
def self.split_nid(nid)
|
243
|
-
|
244
|
-
nid.split('-')
|
245
|
-
end
|
246
|
-
|
247
|
-
def self.child_id(nid)
|
248
|
-
|
249
|
-
nid ? nid.split('_').last.split('-').first.to_i : nil
|
250
|
-
end
|
251
|
-
|
252
|
-
def self.next_child_id(nid)
|
253
|
-
|
254
|
-
child_id(nid) + 1
|
255
|
-
end
|
256
|
-
|
257
|
-
def self.sub_nid(nid, subid)
|
258
|
-
|
259
|
-
"#{nid.split('-').first}-#{subid}"
|
260
|
-
end
|
261
|
-
|
262
|
-
# Remove the sub_nid if any.
|
263
|
-
#
|
264
|
-
def self.master_nid(nid)
|
265
|
-
|
266
|
-
nid.split('-').first
|
267
|
-
end
|
268
|
-
|
269
|
-
def self.child_nid(nid, i, sub=nil)
|
270
|
-
|
271
|
-
nid, subnid = nid.split('-')
|
272
|
-
subnid = sub if sub && sub > 0
|
273
|
-
|
274
|
-
"#{nid}_#{i}#{subnid ? "-#{subnid}" : ''}"
|
275
|
-
end
|
276
|
-
|
277
|
-
def self.parent_id(nid)
|
278
|
-
|
279
|
-
if i = nid.rindex('_')
|
280
|
-
nid[0, i]
|
281
|
-
else
|
282
|
-
nil
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
def self.parent_nid(nid, remove_subnid=false)
|
287
|
-
|
288
|
-
_, sub = nid.split('-')
|
289
|
-
i = nid.rindex('_')
|
290
|
-
|
291
|
-
return nil unless i
|
292
|
-
"#{nid[0, i]}#{remove_subnid || sub.nil? ? nil : "-#{sub}"}"
|
293
|
-
end
|
294
|
-
|
295
|
-
def self.is_nid?(s)
|
296
|
-
|
297
|
-
!! (s.is_a?(String) && s.match(/\A[0-9]+(?:_[0-9]+)*(?:-[0-9]+)?\z/))
|
298
|
-
end
|
299
|
-
|
300
9
|
#
|
301
10
|
# misc
|
302
11
|
#
|
@@ -374,11 +83,6 @@ module Flor
|
|
374
83
|
Array(o)
|
375
84
|
end
|
376
85
|
|
377
|
-
def self.truncate(s, l)
|
378
|
-
|
379
|
-
s.length < l ? s : s[0, l] + '...'
|
380
|
-
end
|
381
|
-
|
382
86
|
def self.relativize_path(path, from=Dir.getwd)
|
383
87
|
|
384
88
|
path = File.absolute_path(path)
|
@@ -388,6 +92,15 @@ module Flor
|
|
388
92
|
path
|
389
93
|
end
|
390
94
|
|
95
|
+
def self.is_array_of_messages?(o)
|
96
|
+
|
97
|
+
o.is_a?(Array) &&
|
98
|
+
o.all? { |e|
|
99
|
+
e.is_a?(Hash) &&
|
100
|
+
e['point'].is_a?(String) &&
|
101
|
+
e.keys.all? { |k| k.is_a?(String) } }
|
102
|
+
end
|
103
|
+
|
391
104
|
#
|
392
105
|
# functions about time
|
393
106
|
|
@@ -528,6 +241,15 @@ module Flor
|
|
528
241
|
o.is_a?(Array) && o.all? { |e| Flor.is_tree?(e) }
|
529
242
|
end
|
530
243
|
|
244
|
+
# # Array, object or atom tree
|
245
|
+
# #
|
246
|
+
# def self.is_value_tree?(o)
|
247
|
+
#
|
248
|
+
# o.is_a?(Array) &&
|
249
|
+
# %w[ _num _boo _sqs _dqs _rxs _nul _arr _obj ].include?(o[0]) &&
|
250
|
+
# o[2].is_a?(Integer)
|
251
|
+
# end
|
252
|
+
|
531
253
|
def self.is_proc_tree?(o)
|
532
254
|
|
533
255
|
o.is_a?(Array) &&
|
@@ -561,5 +283,30 @@ module Flor
|
|
561
283
|
Flor.is_array_of_trees?(o[1]) &&
|
562
284
|
o[2].is_a?(Integer)
|
563
285
|
end
|
286
|
+
|
287
|
+
# Returns [ st, i ], the parent subtree for the final i index of the nid
|
288
|
+
# Used when inserting updated subtrees.
|
289
|
+
#
|
290
|
+
def self.parent_tree_locate(t, nid)
|
291
|
+
|
292
|
+
return nil if t == nil
|
293
|
+
|
294
|
+
n, i, d = nid.split('_', 3)
|
295
|
+
|
296
|
+
return [ t, nil ] if i == nil
|
297
|
+
return [ t, i.to_i ] if ! d
|
298
|
+
parent_tree_locate(t[1][i.to_i], [ i, d ].join('_'))
|
299
|
+
end
|
300
|
+
|
301
|
+
# Returns the subtree down at the given nid
|
302
|
+
#
|
303
|
+
def self.tree_locate(t, nid)
|
304
|
+
|
305
|
+
st, i = parent_tree_locate(t, nid)
|
306
|
+
|
307
|
+
return nil if st == nil
|
308
|
+
return st if i == nil
|
309
|
+
st[1][i]
|
310
|
+
end
|
564
311
|
end
|
565
312
|
|
data/lib/flor/id.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
|
2
|
+
module Flor
|
3
|
+
|
4
|
+
#
|
5
|
+
# ids
|
6
|
+
#
|
7
|
+
# functions about exids, nids, sub_nids, ...
|
8
|
+
|
9
|
+
def self.split_fei(fei)
|
10
|
+
|
11
|
+
if m = fei.match(/\A([^-]+-[^-]+-\d+\.\d+\.[^-]+)-(.*)\z/)
|
12
|
+
[ m[1], m[2] ]
|
13
|
+
else
|
14
|
+
[ nil ]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.exid(fei)
|
19
|
+
|
20
|
+
split_fei(fei).first
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.split_nid(nid)
|
24
|
+
|
25
|
+
nid.split('-')
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.child_id(nid)
|
29
|
+
|
30
|
+
nid ? nid.split('_').last.split('-').first.to_i : nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.next_child_id(nid)
|
34
|
+
|
35
|
+
child_id(nid) + 1
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.sub_nid(nid, subid)
|
39
|
+
|
40
|
+
"#{nid.split('-').first}-#{subid}"
|
41
|
+
end
|
42
|
+
|
43
|
+
# Remove the sub_nid if any.
|
44
|
+
#
|
45
|
+
def self.master_nid(nid)
|
46
|
+
|
47
|
+
nid.split('-').first
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.child_nid(nid, i, sub=nil)
|
51
|
+
|
52
|
+
nid, subnid = nid.split('-')
|
53
|
+
subnid = sub if sub && sub > 0
|
54
|
+
|
55
|
+
"#{nid}_#{i}#{subnid ? "-#{subnid}" : ''}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.parent_id(nid)
|
59
|
+
|
60
|
+
if i = nid.rindex('_')
|
61
|
+
nid[0, i]
|
62
|
+
else
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.parent_nid(nid, remove_subnid=false)
|
68
|
+
|
69
|
+
_, sub = nid.split('-')
|
70
|
+
i = nid.rindex('_')
|
71
|
+
|
72
|
+
return nil unless i
|
73
|
+
"#{nid[0, i]}#{remove_subnid || sub.nil? ? nil : "-#{sub}"}"
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.is_nid?(s)
|
77
|
+
|
78
|
+
!! (s.is_a?(String) && s.match(/\A[0-9]+(?:_[0-9]+)*(?:-[0-9]+)?\z/))
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns [ exid, nid ]
|
82
|
+
#
|
83
|
+
def self.extract_exid_and_nid(s)
|
84
|
+
|
85
|
+
m = s.match(/(\d{8}\.\d{4}\.[a-z]+)-(\d+(?:_\d+)*)(-\d+)?/)
|
86
|
+
|
87
|
+
m ? [ m[1], [ m[2], m[3] ].compact.join ] : nil
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|