fertile_forest 1.2.0 → 1.3.0

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: 7f605105d5db7a7769a308ee81c2f2643840f9ef
4
- data.tar.gz: 915263e1161b69364dc04b0e31ff3399d5dab13a
3
+ metadata.gz: 2959088e37d7bebc2c2491b4a430a74954592e8c
4
+ data.tar.gz: 96c1da1805df91e0f9560543280a8e73af9b1b9b
5
5
  SHA512:
6
- metadata.gz: f8eb476e94442c5525097baac81878594cfe7a280bfe99e12b656d816db113641d907421880600e96038dc6888ddc91a8b4fe4bc82857336a21f6f58fbec4255
7
- data.tar.gz: 0f7fa37321ebc48a02837f450fa0749412cadfa4e50a1dd50fb72b5cd464b89929e3acaaae7d2ceceee8ed4442c2b3325818f82988fd159511abfda0fd88e0b2
6
+ metadata.gz: d29d0e9e2932973c5055f9115a8f772bbb67173d454196e15725312bd2935faca5ebf994631a16238e9635a13a0f6e8bab4496ef21c05ff340deef42b6cd8f0e
7
+ data.tar.gz: a12224e5b58ed393438e93f0293b83d1b0e7c29f833d3fda51124f232107b1b585b284ee3568015d0db0ccff89777ccfb0576742869fc450e203d18670b4d1ce
@@ -357,8 +357,8 @@ module StewEucen
357
357
  # Find any kind of kinship from base node.
358
358
  #
359
359
  # @param base_obj [Entity|Integer] Base node|id to find.
360
- # @param next_branch [Integer] Branch distance of finding nodes from base nodes.
361
- # @param level_offset [Integer] Offset of kinship level of finding nodes from base nodes.
360
+ # @param branch_level [Integer] Branch distance of finding nodes from base nodes.
361
+ # @param depth_offset [Integer] Offset of kinship level of finding nodes from base nodes.
362
362
  # @param columns [Array] Columns for SELECT clause.
363
363
  # @return [ActiveRecord::Relation] Basic query for finding kinship nodes.
364
364
  # @return [nil] No kinship nodes.
@@ -366,8 +366,8 @@ module StewEucen
366
366
  #
367
367
  def kinships(
368
368
  base_obj,
369
- next_branch = KINSHIPS_AS_SIBLING,
370
- level_offset = KINSHIPS_SAME_LEVEL,
369
+ branch_level = KINSHIPS_BRANCH_LEVEL_ONE,
370
+ depth_offset = KINSHIPS_SAME_DEPTH,
371
371
  columns = nil
372
372
  )
373
373
  base_node = ff_resolve_nodes(base_obj)
@@ -377,11 +377,13 @@ module StewEucen
377
377
  aim_depth = base_node.ff_depth
378
378
  aim_grove = base_node.ff_grove # When no grove, nil
379
379
 
380
- top_depth = aim_depth - next_branch
380
+ top_depth = aim_depth - branch_level
381
381
 
382
382
  # Impossible to find.
383
383
  return nil if top_depth < ROOT_DEPTH
384
384
 
385
+ return nil if branch_level + depth_offset < 0
386
+
385
387
  ffqq = arel_table[@_ff_queue]
386
388
  ffdd = arel_table[@_ff_depth]
387
389
  ffgg = arel_table[@_ff_grove]
@@ -390,12 +392,12 @@ module StewEucen
390
392
  before_nodes_subquery = ff_usual_projection(aim_grove)
391
393
  .project(ffqq.maximum.to_sql + " + 1 AS head_queue")
392
394
  .where(ffqq.lt(aim_queue))
393
- .where(ffdd.lt(top_depth))
395
+ .where(ffdd.lteq(top_depth))
394
396
 
395
397
  after_nodes_subquery = ff_usual_projection(aim_grove)
396
398
  .project(ffqq.minimum.to_sql + " - 1 AS tail_queue")
397
399
  .where(ffqq.gt(aim_queue))
398
- .where(ffdd.lt(top_depth))
400
+ .where(ffdd.lteq(top_depth))
399
401
 
400
402
  func_maker = Arel::Nodes::NamedFunction
401
403
 
@@ -409,14 +411,80 @@ module StewEucen
409
411
  [after_nodes_subquery, QUEUE_MAX_VALUE]
410
412
  )
411
413
 
414
+ #
415
+ # create column of ff_branch_level
416
+ #
417
+ increment_branch_level_case = ff_create_case_expression(
418
+ [ffqq.eq(aim_queue).to_sql, 0],
419
+ 1
420
+ )
421
+
422
+ branch_for_level_subqueries = [increment_branch_level_case]
423
+ ((top_depth + 1) .. (aim_depth - 1)).each do |d|
424
+ before_nodes_branch_for_lavel_subquery = ff_usual_projection(aim_grove)
425
+ .project("#{ffqq.maximum.to_sql} AS head_queue_#{d}")
426
+ .where(ffqq.lt(aim_queue))
427
+ .where(ffdd.lteq(d))
428
+
429
+ after_nodes_branch_far_level_subquery = ff_usual_projection(aim_grove)
430
+ .project("#{ffqq.minimum.to_sql} - 1 AS tail_queue_#{d}")
431
+ .where(ffqq.gt(aim_queue))
432
+ .where(ffdd.lteq(d))
433
+
434
+ before_condition = func_maker.new(
435
+ 'COALESCE',
436
+ [before_nodes_branch_for_lavel_subquery, 0]
437
+ )
438
+ after_condition = func_maker.new(
439
+ 'COALESCE',
440
+ [after_nodes_branch_far_level_subquery, QUEUE_MAX_VALUE]
441
+ )
442
+
443
+ branch_for_level_subqueries << ff_create_case_expression(
444
+ [ffqq.lt(before_condition).to_sql, 1],
445
+ [ffqq.gt(after_condition).to_sql, 1],
446
+ 0
447
+ )
448
+ end
449
+
450
+ ff_branch_level = branch_for_level_subqueries.join(' + ') + " AS #{@_ff_branch_level}";
451
+
452
+ #
453
+ # create columns
454
+ #
455
+ base_columns = ff_all_optional_columns(columns);
456
+ base_columns << ff_branch_level
457
+
412
458
  # find nodes by ancestor queues
413
459
  ff_required_columns_scope()
414
460
  .ff_usual_conditions_scope(aim_grove)
415
461
  .ff_usual_order_scope()
416
- .where(ffdd.eq(aim_depth + level_offset))
462
+ .where(ffdd.eq(aim_depth + depth_offset))
417
463
  .where(ffqq.gteq(before_coalesce_condition))
418
464
  .where(ffqq.lteq(after_coalesce_condition))
419
- .select(ff_all_optional_columns(columns))
465
+ .select(base_columns)
466
+ end
467
+
468
+ #
469
+ # create case Expression
470
+ # @param *cases [Array] Cases as:
471
+ # [[when, then], [when, then], ..., else]
472
+ #
473
+ def ff_create_case_expression(*cases)
474
+ when_hash = ['CASE']
475
+ cases.each do |the_case|
476
+ if the_case.instance_of?(Array)
477
+ when_hash << 'WHEN'
478
+ when_hash << the_case.shift
479
+ when_hash << 'THEN'
480
+ when_hash << the_case.shift
481
+ else
482
+ when_hash << "ELSE #{the_case}"
483
+ end
484
+ end
485
+ when_hash << 'END'
486
+
487
+ when_hash.join(' ')
420
488
  end
421
489
 
422
490
  #
@@ -430,7 +498,7 @@ module StewEucen
430
498
  # @version 1.1.0 Replace to use kinships()
431
499
  #
432
500
  def siblings(base_obj, columns = nil)
433
- kinships(base_obj, KINSHIPS_AS_SIBLING, KINSHIPS_SAME_LEVEL, columns)
501
+ kinships(base_obj, KINSHIPS_BRANCH_LEVEL_ONE, KINSHIPS_SAME_DEPTH, columns)
434
502
  end
435
503
 
436
504
  #
@@ -443,7 +511,7 @@ module StewEucen
443
511
  # @return [nil] No cousin nodes.
444
512
  # @since 1.1.0
445
513
  def cousins(base_obj, columns = nil)
446
- kinships(base_obj, KINSHIPS_AS_COUSIN, KINSHIPS_SAME_LEVEL, columns)
514
+ kinships(base_obj, KINSHIPS_BRANCH_LEVEL_TWO, KINSHIPS_SAME_DEPTH, columns)
447
515
  end
448
516
 
449
517
  #
@@ -456,7 +524,7 @@ module StewEucen
456
524
  # @since 1.1.0
457
525
  #
458
526
  def piblings(base_obj, columns = nil)
459
- kinships(base_obj, KINSHIPS_AS_COUSIN, KINSHIPS_PARENT_LEVEL, columns)
527
+ kinships(base_obj, KINSHIPS_BRANCH_LEVEL_TWO, KINSHIPS_PARENT_DEPTH, columns)
460
528
  end
461
529
 
462
530
  #
@@ -469,7 +537,7 @@ module StewEucen
469
537
  # @since 1.1.0
470
538
  #
471
539
  def niblings(base_obj, columns = nil)
472
- kinships(base_obj, KINSHIPS_AS_SIBLING, KINSHIPS_CHILD_LEVEL, columns)
540
+ kinships(base_obj, KINSHIPS_BRANCH_LEVEL_ONE, KINSHIPS_CHILD_DEPTH, columns)
473
541
  end
474
542
 
475
543
  #
@@ -895,7 +963,8 @@ module StewEucen
895
963
  :ff_get_previous_queue,
896
964
  :ff_queue_sorted_nodes,
897
965
 
898
- :ff_sort_with_queue!
966
+ :ff_sort_with_queue!,
967
+ :ff_create_case_expression
899
968
 
900
969
  alias superiors ancestors
901
970
  alias forebears ancestors
@@ -40,12 +40,12 @@ module StewEucen
40
40
  SUBTREE_WITHOUT_TOP_NODE = false
41
41
  SUBTREE_WITH_TOP_NODE = true
42
42
 
43
- KINSHIPS_AS_SIBLING = 0 # Distance for finding sibling nodes In kinships().
44
- KINSHIPS_AS_COUSIN = 1 # Distance for finding cousin nodes In kinships().
43
+ KINSHIPS_BRANCH_LEVEL_ONE = 1 # Distance for finding sibling nodes In kinships().
44
+ KINSHIPS_BRANCH_LEVEL_TWO = 2 # Distance for finding cousin nodes In kinships().
45
45
 
46
- KINSHIPS_SAME_LEVEL = 0 # Same level to find kinships. (such as siblings, cousins.)
47
- KINSHIPS_PARENT_LEVEL = -1 # Upper level to find kinships. (such as aunts|uncles)
48
- KINSHIPS_CHILD_LEVEL = 1 # Lower level to find kinships. (such as niblings.)
46
+ KINSHIPS_SAME_DEPTH = 0 # Same level to find kinships. (such as siblings, cousins.)
47
+ KINSHIPS_PARENT_DEPTH = -1 # Upper level to find kinships. (such as aunts|uncles)
48
+ KINSHIPS_CHILD_DEPTH = 1 # Lower level to find kinships. (such as niblings.)
49
49
 
50
50
  PRUNE_DESCENDANTS_ONLY = false
51
51
  PRUNE_WITH_TOP_NODE = true
@@ -144,6 +144,7 @@ module StewEucen
144
144
  'ff_depth',
145
145
  'ff_queue',
146
146
  'ff_soft_delete',
147
+ 'ff_branch_level',
147
148
  ]
148
149
 
149
150
  ff_required_columns.each do |key|
@@ -1,3 +1,3 @@
1
1
  module FertileForest
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fertile_forest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stew Eucen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-07 00:00:00.000000000 Z
11
+ date: 2015-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails