fertile_forest 1.0.1 → 1.1.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: 263e9d58729f356db7ca3d5ce532a62c5d2468a7
4
- data.tar.gz: 1fa08be22ffe24ecc0e61bfa7496810ee5c6fca3
3
+ metadata.gz: fc9b8f30c16e4ad778708dc87db3da5b24425ad5
4
+ data.tar.gz: 84f89968f7d3eaa000e664a7307c808c3b62ed42
5
5
  SHA512:
6
- metadata.gz: fc6eb47c89edff5caff3725c5786135d46dc5646b2294c216ecdd46f6abc8ba52902a37a417045ea6622ac709abf225ad4d954b960864722ab518cfb2836feb5
7
- data.tar.gz: 7f8016ba0e95f4e7d1b4564dc3594302b96edc561824055ab21cd7cb289fe024504a724507fe97bbf2b3e176ea43b8a193c7f10bd66dc878dc605720bf974e61
6
+ metadata.gz: c866ba297d69444e4a3a94f3e9fa94d0e2a88ec77a121b82ddcef0ecb2a7511d296749316f8b2a8f8e0538d37f1478bee0e8e82f5a115abb42e8b8c77be4391c
7
+ data.tar.gz: 42f61d1ac7b4a923732b7dc929bfba9dbd0839112a9d4df3847b58bac409dd43dafb32bb513861489f6b7ea27b7f30b1ba441f56020381a4dc157118f02675a4
@@ -292,17 +292,123 @@ module StewEucen
292
292
  .where(@_ff_depth => base_node.ff_depth + grand_number)
293
293
  end
294
294
 
295
+ #
296
+ # Find any kind of kinship from base node.
297
+ #
298
+ # @param base_obj [Entity|Integer] Base node|id to find.
299
+ # @param next_branch [Integer] Branch distance of finding nodes from base nodes.
300
+ # @param level_offset [Integer] Offset of kinship level of finding nodes from base nodes.
301
+ # @param columns [Array] Columns for SELECT clause.
302
+ # @return [ActiveRecord::Relation] Basic query for finding kinship nodes.
303
+ # @return [nil] No kinship nodes.
304
+ # @since 1.1.0
305
+ #
306
+ def kinships(
307
+ base_obj,
308
+ next_branch = KINSHIPS_AS_SIBLING,
309
+ level_offset = KINSHIPS_SAME_LEVEL,
310
+ columns = nil
311
+ )
312
+ base_node = ff_resolve_nodes(base_obj)
313
+ return nil if base_node.blank?
314
+
315
+ aim_queue = base_node.ff_queue
316
+ aim_depth = base_node.ff_depth
317
+ aim_grove = base_node.ff_grove # When no grove, nil
318
+
319
+ top_depth = aim_depth - next_branch
320
+
321
+ # Impossible to find.
322
+ return nil if top_depth < ROOT_DEPTH
323
+
324
+ ffqq = arel_table[@_ff_queue]
325
+ ffdd = arel_table[@_ff_depth]
326
+ ffgg = arel_table[@_ff_grove]
327
+
328
+ # create subquery
329
+ before_nodes_subquery = ff_usual_projection(aim_grove)
330
+ .project(ffqq.maximum.to_sql + " + 1 AS head_queue")
331
+ .where(ffqq.lt(aim_queue))
332
+ .where(ffdd.lt(top_depth))
333
+
334
+ after_nodes_subquery = ff_usual_projection(aim_grove)
335
+ .project(ffqq.minimum.to_sql + " - 1 AS tail_queue")
336
+ .where(ffqq.gt(aim_queue))
337
+ .where(ffdd.lt(top_depth))
338
+
339
+ func_maker = Arel::Nodes::NamedFunction
340
+
341
+ before_coalesce_condition = func_maker.new(
342
+ 'COALESCE',
343
+ [before_nodes_subquery, 0]
344
+ )
345
+
346
+ after_coalesce_condition = func_maker.new(
347
+ 'COALESCE',
348
+ [after_nodes_subquery, QUEUE_MAX_VALUE]
349
+ )
350
+
351
+ # find nodes by ancestor queues
352
+ ff_required_columns_scope()
353
+ .ff_usual_conditions_scope(aim_grove)
354
+ .ff_usual_order_scope()
355
+ .where(ffdd.eq(aim_depth + level_offset))
356
+ .where(ffqq.gteq(before_coalesce_condition))
357
+ .where(ffqq.lteq(after_coalesce_condition))
358
+ .select(ff_all_optional_columns(columns))
359
+ end
360
+
295
361
  #
296
362
  # Find sibling nodes from base node.
363
+ # Note: Results includes base node.
297
364
  #
298
365
  # @param base_obj [Entity|Integer] Base node|id to find.
299
366
  # @param columns [Array] Columns for SELECT clause.
300
367
  # @return [ActiveRecord::Relation] Basic query for finding sibling nodes.
301
368
  # @return [nil] No sibling nodes.
369
+ # @version 1.1.0 Replace to use kinships()
302
370
  #
303
371
  def siblings(base_obj, columns = nil)
304
- parent_node = genitor(base_obj)
305
- children(parent_node, columns)
372
+ kinships(base_obj, KINSHIPS_AS_SIBLING, KINSHIPS_SAME_LEVEL, columns)
373
+ end
374
+
375
+ #
376
+ # Find cousin nodes from base node.
377
+ # Note: Results includes siblngs nodes.
378
+ #
379
+ # @param base_obj [Entity|Integer] Base node|id to find.
380
+ # @param columns [Array] Columns for SELECT clause.
381
+ # @return [ActiveRecord::Relation] Basic query for finding cousin nodes.
382
+ # @return [nil] No cousin nodes.
383
+ # @since 1.1.0
384
+ def cousins(base_obj, columns = nil)
385
+ kinships(base_obj, KINSHIPS_AS_COUSIN, KINSHIPS_SAME_LEVEL, columns)
386
+ end
387
+
388
+ #
389
+ # Find aunt|uncle nodes from base node.
390
+ # Note: Results includes siblngs nodes.
391
+ # @param base_obj [Entity|Integer] Base node|id to find.
392
+ # @param columns [Array] Columns for SELECT clause.
393
+ # @return [ActiveRecord::Relation] Basic query for finding aunt|uncle nodes.
394
+ # @return [nil] No aunt|uncle nodes.
395
+ # @since 1.1.0
396
+ #
397
+ def piblings(base_obj, columns = nil)
398
+ kinships(base_obj, KINSHIPS_AS_COUSIN, KINSHIPS_PARENT_LEVEL, columns)
399
+ end
400
+
401
+ #
402
+ # Find nibling nodes from base node.
403
+ # Note: Results includes siblngs nodes.
404
+ # @param base_obj [Entity|Integer] Base node|id to find.
405
+ # @param columns [Array] Columns for SELECT clause.
406
+ # @return [ActiveRecord::Relation] Basic query for finding nibling nodes.
407
+ # @return [nil] No nibling nodes.
408
+ # @since 1.1.0
409
+ #
410
+ def niblings(base_obj, columns = nil)
411
+ kinships(base_obj, KINSHIPS_AS_SIBLING, KINSHIPS_CHILD_LEVEL, columns)
306
412
  end
307
413
 
308
414
  #
@@ -729,6 +835,18 @@ module StewEucen
729
835
  :ff_queue_sorted_nodes,
730
836
 
731
837
  :ff_sort_with_queue!
838
+
839
+ alias superiors ancestors
840
+ alias forebears ancestors
841
+ alias inferiors descendants
842
+ alias afterbears descendants
843
+ alias externals leaves
844
+ alias terminals leaves
845
+ alias nephews niblings
846
+ alias nieces niblings
847
+ alias auncles piblings
848
+ alias aunts piblings
849
+ alias uncles piblings
732
850
  end
733
851
  end
734
852
  end
@@ -444,7 +444,7 @@ module StewEucen
444
444
  aim_node = ff_resolve_nodes(node_obj)
445
445
  return false if aim_node.blank?
446
446
 
447
- siblings_query = siblings_of(aim_node, [@_id])
447
+ siblings_query = siblings(aim_node, [@_id])
448
448
  return false if siblings_query.blank?
449
449
  sibling_nodes = siblings_query.all
450
450
 
@@ -25,7 +25,7 @@ module StewEucen
25
25
 
26
26
  # get id hash by nested information
27
27
  eldest_node = sibling_nodes.values.first
28
- full_sibling_nodes = siblings_of(eldest_node, [@_id]).all
28
+ full_sibling_nodes = siblings(eldest_node, [@_id]).all
29
29
 
30
30
  child_hash = {}
31
31
  bingo_count = sibling_nodes.length
@@ -37,12 +37,19 @@ module StewEucen
37
37
  DESCENDANTS_ALL = 0
38
38
  DESCENDANTS_ONLY_CHILD = 1
39
39
 
40
- PRUNE_DESCENDANTS_ONLY = false
41
- PRUNE_WITH_TOP_NODE = true
42
-
43
40
  SUBTREE_WITHOUT_TOP_NODE = false
44
41
  SUBTREE_WITH_TOP_NODE = true
45
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().
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.)
49
+
50
+ PRUNE_DESCENDANTS_ONLY = false
51
+ PRUNE_WITH_TOP_NODE = true
52
+
46
53
  ORDER_BY_QUEUE_INDEX = false
47
54
  ORDER_BY_DEPTH_INDEX = true
48
55
 
@@ -1,3 +1,3 @@
1
1
  module FertileForest
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.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.0.1
4
+ version: 1.1.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-10-12 00:00:00.000000000 Z
11
+ date: 2015-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -25,9 +25,8 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.2'
27
27
  description: Fertile Forest is the new model to store hierarchical data in a database.
28
- Conventional models are "adjacency list", "route enumeration", "nested set" and
29
- "closure table". Fertile Forest has some excellent features than each conventional
30
- model.
28
+ Conventional models are "adjacency list", "path enumeration", "nested set" and "closure
29
+ table". Fertile Forest has some excellent features than each conventional model.
31
30
  email:
32
31
  - stew.eucen@gmail.com
33
32
  executables: []
@@ -70,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
69
  version: '0'
71
70
  requirements: []
72
71
  rubyforge_project:
73
- rubygems_version: 2.2.2
72
+ rubygems_version: 2.4.8
74
73
  signing_key:
75
74
  specification_version: 4
76
75
  summary: The new model to store hierarchical data in a database.