flor 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +14 -1
  3. data/CREDITS.md +1 -0
  4. data/LICENSE.txt +1 -1
  5. data/Makefile +6 -2
  6. data/README.md +2 -1
  7. data/flor.gemspec +12 -2
  8. data/lib/flor.rb +3 -3
  9. data/lib/flor/colours.rb +1 -1
  10. data/lib/flor/conf.rb +3 -4
  11. data/lib/flor/core/executor.rb +31 -61
  12. data/lib/flor/core/node.rb +213 -96
  13. data/lib/flor/core/procedure.rb +194 -75
  14. data/lib/flor/core/texecutor.rb +6 -7
  15. data/lib/flor/djan.rb +41 -22
  16. data/lib/flor/flor.rb +137 -42
  17. data/lib/flor/id.rb +77 -59
  18. data/lib/flor/log.rb +43 -22
  19. data/lib/flor/migrations/0001_tables.rb +7 -7
  20. data/lib/flor/parser.rb +271 -74
  21. data/lib/flor/pcore/_apply.rb +108 -0
  22. data/lib/flor/pcore/_atom.rb +2 -4
  23. data/lib/flor/pcore/_att.rb +54 -37
  24. data/lib/flor/pcore/_dmute.rb +18 -0
  25. data/lib/flor/pcore/_dol.rb +17 -0
  26. data/lib/flor/pcore/_dqs.rb +35 -0
  27. data/lib/flor/pcore/_head.rb +25 -0
  28. data/lib/flor/pcore/_obj.rb +1 -3
  29. data/lib/flor/pcore/_pat_guard.rb +1 -1
  30. data/lib/flor/pcore/_pat_obj.rb +11 -3
  31. data/lib/flor/pcore/_pat_regex.rb +16 -2
  32. data/lib/flor/pcore/_ref.rb +51 -0
  33. data/lib/flor/pcore/_rxs.rb +27 -0
  34. data/lib/flor/pcore/_val.rb +11 -6
  35. data/lib/flor/pcore/{logo.rb → andor.rb} +4 -6
  36. data/lib/flor/pcore/apply.rb +72 -2
  37. data/lib/flor/pcore/arith.rb +16 -4
  38. data/lib/flor/pcore/array_qmark.rb +100 -0
  39. data/lib/flor/pcore/break.rb +1 -2
  40. data/lib/flor/pcore/case.rb +1 -1
  41. data/lib/flor/pcore/cmp.rb +3 -2
  42. data/lib/flor/pcore/collect.rb +2 -2
  43. data/lib/flor/pcore/cond.rb +19 -1
  44. data/lib/flor/pcore/cursor.rb +12 -11
  45. data/lib/flor/pcore/define.rb +30 -4
  46. data/lib/flor/pcore/do_return.rb +3 -0
  47. data/lib/flor/pcore/flatten.rb +39 -0
  48. data/lib/flor/pcore/if.rb +15 -5
  49. data/lib/flor/pcore/includes.rb +5 -2
  50. data/lib/flor/pcore/inject.rb +1 -1
  51. data/lib/flor/pcore/iterator.rb +28 -18
  52. data/lib/flor/pcore/keys.rb +2 -2
  53. data/lib/flor/pcore/map.rb +19 -1
  54. data/lib/flor/pcore/match.rb +2 -2
  55. data/lib/flor/pcore/matchr.rb +18 -5
  56. data/lib/flor/pcore/max.rb +51 -0
  57. data/lib/flor/pcore/merge.rb +134 -0
  58. data/lib/flor/pcore/move.rb +1 -1
  59. data/lib/flor/pcore/noret.rb +1 -1
  60. data/lib/flor/pcore/not.rb +15 -1
  61. data/lib/flor/pcore/on.rb +11 -0
  62. data/lib/flor/pcore/on_cancel.rb +5 -1
  63. data/lib/flor/pcore/on_error.rb +69 -4
  64. data/lib/flor/pcore/push.rb +4 -9
  65. data/lib/flor/pcore/range.rb +5 -5
  66. data/lib/flor/pcore/reduce.rb +5 -18
  67. data/lib/flor/pcore/return.rb +26 -0
  68. data/lib/flor/pcore/reverse.rb +4 -0
  69. data/lib/flor/pcore/sequence.rb +8 -1
  70. data/lib/flor/pcore/set.rb +74 -15
  71. data/lib/flor/pcore/shuffle.rb +71 -0
  72. data/lib/flor/pcore/slice.rb +137 -0
  73. data/lib/flor/pcore/sort.rb +244 -0
  74. data/lib/flor/pcore/sort_by.rb +67 -0
  75. data/lib/flor/pcore/split.rb +39 -0
  76. data/lib/flor/pcore/stall.rb +1 -1
  77. data/lib/flor/pcore/strings.rb +123 -0
  78. data/lib/flor/pcore/timestamp.rb +34 -0
  79. data/lib/flor/pcore/to_array.rb +2 -3
  80. data/lib/flor/pcore/twig.rb +1 -1
  81. data/lib/flor/pcore/type_of.rb +37 -0
  82. data/lib/flor/pcore/until.rb +3 -3
  83. data/lib/flor/punit/cancel.rb +3 -3
  84. data/lib/flor/punit/ccollect.rb +29 -0
  85. data/lib/flor/punit/cmap.rb +76 -20
  86. data/lib/flor/punit/concurrence.rb +440 -33
  87. data/lib/flor/punit/cron.rb +1 -1
  88. data/lib/flor/punit/every.rb +1 -1
  89. data/lib/flor/punit/graft.rb +2 -3
  90. data/lib/flor/punit/on_timeout.rb +5 -1
  91. data/lib/flor/punit/part.rb +63 -0
  92. data/lib/flor/punit/schedule.rb +1 -1
  93. data/lib/flor/punit/task.rb +52 -10
  94. data/lib/flor/punit/trap.rb +4 -5
  95. data/lib/flor/tools/shell.rb +37 -18
  96. data/lib/flor/unit/caller.rb +23 -11
  97. data/lib/flor/unit/executor.rb +33 -12
  98. data/lib/flor/unit/ganger.rb +10 -1
  99. data/lib/flor/unit/hook.rb +2 -1
  100. data/lib/flor/unit/hooker.rb +13 -2
  101. data/lib/flor/unit/loader.rb +7 -7
  102. data/lib/flor/unit/logger.rb +15 -17
  103. data/lib/flor/unit/models.rb +4 -2
  104. data/lib/flor/unit/models/execution.rb +83 -38
  105. data/lib/flor/unit/models/message.rb +16 -0
  106. data/lib/flor/unit/models/pointer.rb +24 -0
  107. data/lib/flor/unit/models/timer.rb +25 -4
  108. data/lib/flor/unit/models/trace.rb +14 -0
  109. data/lib/flor/unit/models/trap.rb +39 -14
  110. data/lib/flor/unit/scheduler.rb +11 -7
  111. data/lib/flor/unit/storage.rb +55 -39
  112. data/lib/flor/unit/taskers.rb +17 -14
  113. data/lib/flor/unit/waiter.rb +4 -3
  114. metadata +40 -10
  115. data/lib/flor/changes.rb +0 -26
  116. data/lib/flor/dollar.rb +0 -224
  117. data/lib/flor/unit/hooks.rb +0 -37
@@ -90,9 +90,7 @@ module Flor
90
90
  [], # no traps
91
91
  {
92
92
  'exid' => Flor.generate_exid('eval', 'u0'),
93
- 'nodes' => {}, 'errors' => [], 'counters' => {},
94
- #'ashes' => {},
95
- 'start' => Flor.tstamp
93
+ 'nodes' => {}, 'counters' => {}, 'start' => Flor.tstamp
96
94
  })
97
95
 
98
96
  @unit.archive = {} if conf['archive']
@@ -198,9 +196,10 @@ module Flor
198
196
  if path.match(/[\r\n]/)
199
197
  path.strip
200
198
  else
201
- ls = File.readlines(path)
202
- ls.reject! { |l| l.strip[0, 1] == '#' }
203
- s = ls.join("\n").strip
199
+ File.readlines(path)
200
+ .reject { |l| l.strip[0, 1] == '#' }
201
+ .join("\n")
202
+ .strip
204
203
  end
205
204
 
206
205
  az = "#{src[0, 1]}#{src[-1, 1]}"
@@ -251,7 +250,7 @@ module Flor
251
250
  o['_path'] = path
252
251
  o['root'] ||= Flor.relativize_path(vs['root'])
253
252
  elsif o.is_a?(Array)
254
- o.each { |e| e['_path'] = path }
253
+ o.each { |ee| ee['_path'] = path }
255
254
  end
256
255
 
257
256
  o
@@ -4,25 +4,39 @@ require 'io/console'
4
4
 
5
5
  module Flor
6
6
 
7
- def self.to_djan(x, opts={}); to_d(x, opts); end
7
+ class << self
8
8
 
9
- def self.to_d(x, opts={})
9
+ def to_djan(x, opts={})
10
10
 
11
- out = StringIO.new
12
- opts[:c] = Flor.colours(opts)
11
+ out = StringIO.new
12
+ out.set_encoding('UTF-8')
13
13
 
14
- if [ :console, true ].include?(opts[:width])
15
- opts[:width] = IO.console.winsize[1] rescue 80
16
- #elsif opts[:width].is_a?(Integer)
17
- # let it go
18
- elsif mw = (opts[:mw] || opts[:maxwidth] || opts[:max_width])
19
- opts[:width] = [ (IO.console.winsize[1] rescue 80), mw ].min
14
+ opts[:c] = Flor.colours(opts)
15
+
16
+ if [ :console, true ].include?(opts[:width])
17
+ opts[:width] = IO.console.winsize[1] rescue 80
18
+ #elsif opts[:width].is_a?(Integer)
19
+ # let it go
20
+ elsif mw = (opts[:mw] || opts[:maxwidth] || opts[:max_width])
21
+ opts[:width] = [ (IO.console.winsize[1] rescue 80), mw ].min
22
+ end
23
+ opts[:indent] ||= 0 if opts[:width]
24
+
25
+ opts[:str_escape] ||= []
26
+
27
+ Djan.to_d(x, out, opts)
28
+
29
+ out.string
20
30
  end
21
- opts[:indent] ||= 0 if opts[:width]
22
31
 
23
- Djan.to_d(x, out, opts)
32
+ alias to_d to_djan
24
33
 
25
- out.string
34
+ # to_d, but without colours
35
+ #
36
+ def to_dnc(x)
37
+
38
+ to_d(x, colours: false)
39
+ end
26
40
  end
27
41
 
28
42
  module Djan
@@ -132,9 +146,9 @@ module Flor
132
146
  end
133
147
  end
134
148
 
135
- x.each_with_index do |(k, v), i|
149
+ x.each_with_index do |(k, v), ii|
136
150
 
137
- kl = string_to_d(k, out, indent(opts, first: i == 0))
151
+ kl = string_to_d(k, out, indent(opts, first: ii == 0))
138
152
  c_inf(':', out, opts)
139
153
 
140
154
  kt = key_max_len ? key_max_len - kl : nil
@@ -142,7 +156,7 @@ module Flor
142
156
 
143
157
  to_d(v, out, indent(opts, inc: 2, keytab: kt))
144
158
 
145
- if i < x.size - 1
159
+ if ii < x.size - 1
146
160
  c_inf(',', out, opts)
147
161
  newline_or_space(out, opts)
148
162
  end
@@ -190,12 +204,12 @@ module Flor
190
204
  opts[:json] ||
191
205
  x.match(/\A[^: \b\f\n\r\t"',()\[\]{}#\\+%\/><^!=-]+\z/) == nil ||
192
206
  x.to_i.to_s == x ||
193
- x.to_f.to_s == x
207
+ x.to_f.to_s == x ||
208
+ opts[:str_escape].include?(x)
194
209
  ) then
195
- c_inf('"', out, opts)
196
- c_str(x.inspect[1..-2], out, opts)
197
- c_inf('"', out, opts)
198
- x.inspect[1..-2].length + 2
210
+ s = x.inspect
211
+ c_str(s, out, opts)
212
+ s.length
199
213
  else
200
214
  c_str(x, out, opts)
201
215
  x.length
@@ -222,7 +236,12 @@ module Flor
222
236
  def c_nil(s, out, opts); out << opts[:c].dark_gray(s); end
223
237
  def c_tru(s, out, opts); out << opts[:c].green(s); end
224
238
  def c_fal(s, out, opts); out << opts[:c].red(s); end
225
- def c_str(s, out, opts); out << opts[:c].brown(s); end
239
+ #def c_str(s, out, opts); out << opts[:c].brown(s); end
240
+ def c_str(s, out, opts)
241
+ out << opts[:c].brown(s)
242
+ #out << opts[:c].brown(s).tap { |x| p [ x, x.encoding ] }
243
+ #out << opts[:c].brown(s).encode('UTF-8')
244
+ end
226
245
  def c_num(s, out, opts); out << opts[:c].light_blue(s); end
227
246
  end
228
247
  end
@@ -6,7 +6,8 @@ module Flor
6
6
  DOMAIN_NAME_REX = /\A#{NAME_REX}(\.#{NAME_REX})*\z/
7
7
  FLOW_NAME_REX = /\A(#{NAME_REX}(?:\.#{NAME_REX})*)\.([a-zA-Z0-9_-]+)\z/
8
8
 
9
- DOMAIN_UNIT_REX = /\A(#{NAME_REX}(?:\.#{NAME_REX})*)-(#{NAME_REX})[-\z]/
9
+ #DOMAIN_UNIT_REX = /\A(#{NAME_REX}(?:\.#{NAME_REX})*)-(#{NAME_REX})[-\z]/
10
+ DOMAIN_UNIT_REX = /\A(#{NAME_REX}(?:\.#{NAME_REX})*)-(#{NAME_REX})[-$]/
10
11
 
11
12
  SPLAT_REGEX = /\A(.*)__(_|\d+)\z/.freeze
12
13
 
@@ -38,6 +39,8 @@ module Flor
38
39
  v = ENV[k]; (v && v.match(/\A\d+\z/)) ? v.to_i : nil
39
40
  end
40
41
 
42
+ # Returns a new, complete (not shallow), copy of the target instance.
43
+ #
41
44
  def dup(o)
42
45
 
43
46
  Marshal.load(Marshal.dump(o))
@@ -45,7 +48,7 @@ module Flor
45
48
 
46
49
  def dup_and_merge(h, hh)
47
50
 
48
- self.dup(h).merge(hh)
51
+ self.dup(h).merge!(hh)
49
52
  end
50
53
  def dupm(h, hh); self.dup_and_merge(h, hh); end
51
54
 
@@ -73,7 +76,7 @@ module Flor
73
76
  def to_error(o)
74
77
 
75
78
  h = {}
76
- h['kla'] = o.class.to_s
79
+ h['kla'] = o.class == String ? 'Flor::FlorError' : o.class.to_s
77
80
 
78
81
  m, t =
79
82
  if o.is_a?(::Exception)
@@ -95,6 +98,20 @@ module Flor
95
98
  h
96
99
  end
97
100
 
101
+ def to_error_message(message, err)
102
+
103
+ m = message
104
+ .select { |k, v|
105
+ %w[ sm exid nid from payload tree er tasker ].include?(k) }
106
+
107
+ m['point'] = 'failed'
108
+ m['fpoint'] = message['point']
109
+ m['fm'] = message['m']
110
+ m['error'] = to_error(err)
111
+
112
+ m
113
+ end
114
+
98
115
  def const_lookup(s)
99
116
 
100
117
  s.split('::')
@@ -102,6 +119,11 @@ module Flor
102
119
  .inject(Kernel) { |k, sk| k.const_get(sk, k == Kernel) }
103
120
  end
104
121
 
122
+ def is_collection?(o)
123
+
124
+ o.is_a?(Array) || o.is_a?(Hash)
125
+ end
126
+
105
127
  def to_coll(o)
106
128
 
107
129
  #o.respond_to?(:to_a) ? o.to_a : [ a ]
@@ -117,18 +139,22 @@ module Flor
117
139
  path || '.'
118
140
  end
119
141
 
142
+ def is_message?(o)
143
+
144
+ o.is_a?(Hash) &&
145
+ o['point'].is_a?(String) &&
146
+ o.keys.all? { |k| k.is_a?(String) }
147
+ end
148
+
120
149
  def is_array_of_messages?(o)
121
150
 
122
151
  o.is_a?(Array) &&
123
- o.all? { |e|
124
- e.is_a?(Hash) &&
125
- e['point'].is_a?(String) &&
126
- e.keys.all? { |k| k.is_a?(String) } }
152
+ o.all? { |e| is_message?(o) }
127
153
  end
128
154
 
129
155
  def h_fetch(h, *keys)
130
156
 
131
- k = keys.find { |k| h.has_key?(k) }
157
+ k = keys.find { |kk| h.has_key?(kk) }
132
158
  k ? h[k] : nil
133
159
  end
134
160
 
@@ -136,7 +162,7 @@ module Flor
136
162
 
137
163
  default = keys.last.is_a?(String) ? [] : keys.pop
138
164
 
139
- k = keys.find { |k| h.has_key?(k) }
165
+ k = keys.find { |kk| h.has_key?(kk) }
140
166
  v = k ? h[k] : nil
141
167
 
142
168
  v_to_a(v) || default
@@ -163,10 +189,16 @@ module Flor
163
189
  def to_regex(o)
164
190
 
165
191
  s =
166
- case o
167
- when String then o
168
- when Array then o[1].to_s # hopefully regex tree
169
- else o.to_s
192
+ if o.is_a?(String)
193
+ o
194
+ elsif o.is_a?(Array)
195
+ if (o[0] == '_rxs' || o[0] == 'regex') && o[2].is_a?(Integer)
196
+ o[1].to_s
197
+ else
198
+ "/#{o[0..-2].join}/#{o[-1]}"
199
+ end
200
+ else
201
+ o.to_s
170
202
  end
171
203
 
172
204
  m = s.match(/\A\/(.*)\/([imxouesn]*)\z/)
@@ -340,12 +372,31 @@ module Flor
340
372
  t[1].is_a?(Array)
341
373
  end
342
374
 
375
+ def is_definition_tree?(t)
376
+
377
+ t.is_a?(Array) &&
378
+ Flor::Pro::Define.names.include?(t[0]) &&
379
+ t[2].is_a?(Integer) &&
380
+ t[1].is_a?(Array)
381
+ end
382
+
383
+ def is_def_tree?(t)
384
+
385
+ is_definition_tree?(t) &&
386
+ t[0] != 'define'
387
+ end
388
+
343
389
  def is_array_of_trees?(o)
344
390
 
345
391
  o.is_a?(Array) &&
346
392
  o.all? { |e| Flor.is_tree?(e) }
347
393
  end
348
394
 
395
+ def is_single_ref_tree?(t)
396
+
397
+ t.is_a?(Array) && t[0].is_a?(String) && t[0] != '_' && t[1] == []
398
+ end
399
+
349
400
  # # Array, object or atom tree
350
401
  # #
351
402
  # def is_value_tree?(o)
@@ -372,14 +423,16 @@ module Flor
372
423
  o[1].is_a?(Hash) && (o[1].keys & %w[ nid cnid fun ]).size == 3
373
424
  end
374
425
 
375
- # def is_task_tree?(o)
376
- #
377
- # o.is_a?(Array) &&
378
- # o[0] == '_task' &&
379
- # o[2].is_a?(Integer) &&
380
- # o[1].is_a?(Hash) &&
381
- # o[1]['task'].is_a?(String)
382
- # end
426
+ def is_tasker_tree?(o)
427
+
428
+ # [ '_tasker', { 'tasker' => 'alan' }, -1 ]
429
+
430
+ o.is_a?(Array) &&
431
+ o[0] == '_tasker' &&
432
+ o[2].is_a?(Integer) &&
433
+ o[1].is_a?(Hash) &&
434
+ o[1]['tasker'].is_a?(String)
435
+ end
383
436
 
384
437
  def is_regex_tree?(o)
385
438
 
@@ -397,10 +450,10 @@ module Flor
397
450
 
398
451
  return nil if t == nil
399
452
 
400
- n, i, d = nid.split('_', 3)
453
+ _, i, d = nid.split('_', 3)
401
454
 
402
455
  return [ t, nil ] if i == nil
403
- return [ t, i.to_i ] if ! d
456
+ return [ t, i.to_i ] if d == nil
404
457
  parent_tree_locate(t[1][i.to_i], [ i, d ].join('_'))
405
458
  end
406
459
 
@@ -417,36 +470,78 @@ module Flor
417
470
 
418
471
 
419
472
  #
420
- # splat
473
+ # misc
421
474
 
422
- def splat(keys, array)
475
+ def point?(s)
423
476
 
424
- ks = keys.dup
425
- a = array.dup
426
- h = {}
477
+ POINTS.include?(s)
478
+ end
427
479
 
428
- while k = ks.shift
480
+ def type(o)
429
481
 
430
- if m = SPLAT_REGEX.match(k)
431
- r, l = m[1, 2]
432
- l = (l == '_') ? a.length - ks.length : l.to_i
433
- h[r] = a.take(l) if r.length > 0
434
- a = a.drop(l)
435
- else
436
- h[k] = a.shift
437
- end
482
+ case o
483
+ when Array then :array
484
+ when Hash then :object
485
+ when String then :string
486
+ when true, false then :boolean
487
+ when Numeric then :number
488
+ when nil then :null
489
+ else nil
438
490
  end
439
-
440
- h
441
491
  end
442
492
 
443
493
 
444
494
  #
445
- # misc
495
+ # Dense paths
446
496
 
447
- def point?(s)
497
+ def path_to_s(path)
448
498
 
449
- POINTS.include?(s)
499
+ path_to_dense_path(path).to_s
500
+ end
501
+
502
+ def path_to_dense_path(path)
503
+
504
+ Dense::Path.make(path.collect { |e| path_elt_to_dense_path_elt(e) })
505
+ end
506
+
507
+ def path_elt_to_dense_path_elt(elt)
508
+
509
+ case elt
510
+ #when String then elt
511
+ #when Integer then elt
512
+ when { 'dot' => true } then :dot
513
+ when { 'star' => true } then :star
514
+ when { 'dotstar' => true } then :star
515
+ when Array then elt.collect { |e| path_elt_to_dense_path_elt(e) }
516
+ # TODO regexes
517
+ else elt
518
+ end
519
+ end
520
+
521
+ def tree_to_pp_s(t, out=StringIO.new, indent='')
522
+
523
+ out.print("#{indent}[ '#{t[0]}', ")
524
+ if t[1] == []
525
+ out.print("[]")
526
+ elsif t[1].is_a?(Array)
527
+ out.print("[\n")
528
+ t[1].each_with_index do |ct, i|
529
+ tree_to_pp_s(ct, out, indent + ' ')
530
+ if i < t[1].length - 1
531
+ out.print(",\n")
532
+ else
533
+ out.print("\n#{indent}")
534
+ end
535
+ end
536
+ out.print("]")
537
+ else
538
+ out.print(t[1].inspect)
539
+ end
540
+ out.print(", #{t[2]}")
541
+ out.print(", '#{t[3]}'") if t[3]
542
+ out.print(" ]")
543
+
544
+ indent == '' ? out.string : nil
450
545
  end
451
546
  end
452
547
  end
@@ -1,95 +1,113 @@
1
1
 
2
2
  module Flor
3
3
 
4
- #
5
- # ids
6
- #
7
- # functions about exids, nids, sub_nids, ...
4
+ class << self
8
5
 
9
- def self.split_fei(fei)
6
+ #
7
+ # ids
8
+ #
9
+ # functions about exids, nids, sub_nids, ...
10
10
 
11
- if m = fei.match(/\A([^-]+-[^-]+-\d+\.\d+\.[^-]+)-(.*)\z/)
12
- [ m[1], m[2] ]
13
- else
14
- [ nil ]
11
+ def split_fei(fei)
12
+
13
+ if m = fei.match(/\A([^-]+-[^-]+-\d+\.\d+\.[^-]+)-(.*)\z/)
14
+ [ m[1], m[2] ]
15
+ else
16
+ [ nil ]
17
+ end
15
18
  end
16
- end
17
19
 
18
- def self.exid(fei)
20
+ def exid(fei)
19
21
 
20
- split_fei(fei).first
21
- end
22
+ split_fei(fei).first
23
+ end
22
24
 
23
- def self.split_nid(nid)
25
+ def split_nid(nid)
24
26
 
25
- nid.split('-')
26
- end
27
+ nid.split('-')
28
+ end
27
29
 
28
- def self.child_id(nid)
30
+ def child_id(nid)
29
31
 
30
- nid ? nid.split('_').last.split('-').first.to_i : nil
31
- end
32
+ nid ? nid.split('_').last.split('-').first.to_i : nil
33
+ end
32
34
 
33
- def self.next_child_id(nid)
35
+ def next_child_id(nid)
34
36
 
35
- child_id(nid) + 1
36
- end
37
+ child_id(nid) + 1
38
+ end
37
39
 
38
- def self.sub_nid(nid, subid=nil)
40
+ def sub_nid(nid, subid=nil)
39
41
 
40
- if subid
41
- "#{nid.split('-').first}-#{subid}"
42
- else
43
- ss = nid.split('-')
44
- ss.length > 1 ? ss.last.to_i : 0
42
+ if subid
43
+ "#{nid.split('-').first}-#{subid}"
44
+ else
45
+ ss = nid.split('-')
46
+ ss.length > 1 ? ss.last.to_i : 0
47
+ end
45
48
  end
46
- end
47
49
 
48
- # Remove the sub_nid if any.
49
- #
50
- def self.master_nid(nid)
50
+ def same_sub?(nid0, nid1)
51
51
 
52
- nid.split('-').first
53
- end
52
+ sub_nid(nid0) == sub_nid(nid1)
53
+ end
54
54
 
55
- def self.child_nid(nid, i, sub=nil)
55
+ def same_branch?(nid0, nid1)
56
56
 
57
- nid, subnid = nid.split('-')
58
- subnid = sub if sub && sub > 0
57
+ return false unless same_sub?(nid0, nid1)
59
58
 
60
- "#{nid}_#{i}#{subnid ? "-#{subnid}" : ''}"
61
- end
59
+ n0, n1 = [ nid0, nid1 ].collect { |i| Flor.master_nid(i) }.sort
60
+ n = n1[0, n0.length]
62
61
 
63
- def self.parent_id(nid)
62
+ n == n0
63
+ end
64
64
 
65
- if i = nid.rindex('_')
66
- nid[0, i]
67
- else
68
- nil
65
+ # Remove the sub_nid if any.
66
+ #
67
+ def master_nid(nid)
68
+
69
+ nid.split('-').first
69
70
  end
70
- end
71
71
 
72
- def self.parent_nid(nid, remove_subnid=false)
72
+ def child_nid(nid, i, sub=nil)
73
73
 
74
- _, sub = nid.split('-')
75
- i = nid.rindex('_')
74
+ nid, subnid = nid.split('-')
75
+ subnid = sub if sub && sub > 0
76
76
 
77
- return nil unless i
78
- "#{nid[0, i]}#{remove_subnid || sub.nil? ? nil : "-#{sub}"}"
79
- end
77
+ "#{nid}_#{i}#{subnid ? "-#{subnid}" : ''}"
78
+ end
80
79
 
81
- def self.is_nid?(s)
80
+ def parent_id(nid)
82
81
 
83
- !! (s.is_a?(String) && s.match(/\A[0-9]+(?:_[0-9]+)*(?:-[0-9]+)?\z/))
84
- end
82
+ if i = nid.rindex('_')
83
+ nid[0, i]
84
+ else
85
+ nil
86
+ end
87
+ end
88
+
89
+ def parent_nid(nid, remove_subnid=false)
85
90
 
86
- # Returns [ exid, nid ]
87
- #
88
- def self.extract_exid_and_nid(s)
91
+ _, sub = nid.split('-')
92
+ i = nid.rindex('_')
89
93
 
90
- m = s.match(/(\d{8}\.\d{4}\.[a-z]+)-(\d+(?:_\d+)*)(-\d+)?/)
94
+ return nil unless i
95
+ "#{nid[0, i]}#{remove_subnid || sub.nil? ? nil : "-#{sub}"}"
96
+ end
97
+
98
+ def is_nid?(s)
99
+
100
+ !! (s.is_a?(String) && s.match(/\A[0-9]+(?:_[0-9]+)*(?:-[0-9]+)?\z/))
101
+ end
91
102
 
92
- m ? [ m[1], [ m[2], m[3] ].compact.join ] : nil
103
+ # Returns [ exid, nid ]
104
+ #
105
+ def extract_exid_and_nid(s)
106
+
107
+ m = s.match(/(\d{8}\.\d{4}\.[a-z]+)-(\d+(?:_\d+)*)(-\d+)?/)
108
+
109
+ m ? [ m[1], [ m[2], m[3] ].compact.join ] : nil
110
+ end
93
111
  end
94
112
  end
95
113