forester 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 268ad2f30fdb06a9fbc2fe57e2f044a1a234639a
4
- data.tar.gz: 008ea947763719e1d4c1ea5101a8d889979907f9
3
+ metadata.gz: a837fb2321275845b5c51622079d41896966145e
4
+ data.tar.gz: 5e1deb643c9233e5d376839948ac74a369bb5e13
5
5
  SHA512:
6
- metadata.gz: 1fa2bd80e48031e9ccc8e269e20a3b60266252f4f857c046cfa2e53bbf7599eb6f5270b5c6724f628d8cecbe66f8d198f0c96f4e2dc660999c456326b2d6d047
7
- data.tar.gz: ed777a43b80efdf961a89e7dd03010b432096e9236b6f48cf64a25b12cb96941f9eef1bfecec60664008630ecc40a7cd3098d8689956c5999d9c67471adfc618
6
+ metadata.gz: 9622068a5b9e8d56dddd96b87e7553d377390b7cfc25d14154dbe4255a30fc6f83f260cf2c8f038728aed4a77f893e161588e475b96de40dfd86f9f86e916ce8
7
+ data.tar.gz: 8b7b017668323e9c00544518944fdbf1cf7595cdb08b33986d119bf52add39da096cbf3372c73bd4c2b2f7c31631c90a330aa3e3946ce3e93930798eead421a6
@@ -1,16 +1,24 @@
1
1
  module Forester
2
2
  module Aggregators
3
3
 
4
- def own_and_descendants(options = {})
4
+ def with_ancestry(options = {})
5
5
  default_options = {
6
- field: :name,
7
- if_field_missing: -> (c) { [] },
6
+ include_root: true,
7
+ include_self: true,
8
+ descending: true
8
9
  }
9
-
10
10
  options = default_options.merge(options)
11
11
 
12
+ ancestors = self.parentage || []
13
+ ancestors = ancestors[0...-1] unless options[:include_root]
14
+ ancestors = ancestors.unshift(self) if options[:include_self]
15
+ if options[:descending] then ancestors.reverse else ancestors end
16
+ end
17
+
18
+ def own_and_descendants(field_name, &if_field_missing)
19
+ if_field_missing = -> (node) { [] } unless block_given?
12
20
  flat_map do |node|
13
- Array(node.get(options[:field], &options[:if_field_missing]))
21
+ Array(node.get(field_name, &if_field_missing))
14
22
  end
15
23
  end
16
24
 
@@ -33,7 +41,7 @@ module Forester
33
41
 
34
42
  found_nodes.flat_map do |node|
35
43
  if options[:include_descendants]
36
- node.own_and_descendants({ field: options[:values_key] })
44
+ node.own_and_descendants(options[:values_key])
37
45
  else
38
46
  node.get(options[:values_key])
39
47
  end
@@ -46,29 +54,22 @@ module Forester
46
54
  level: 1,
47
55
  group_field: 'name',
48
56
  aggregation_field: 'value',
49
- if_field_missing: -> (c) { [] },
57
+ if_field_missing: -> (node) { [] },
50
58
  include_ancestry_in_keys: false, # if false, with_root is ignored
51
59
  with_root: false,
52
60
  }
53
-
54
61
  options = default_options.merge(options)
55
62
 
56
63
  nodes_of_level(options[:level]).each_with_object({}) do |node, hash|
57
64
 
58
65
  key_nodes = if options[:include_ancestry_in_keys]
59
- node.ancestry(options[:with_root], true)
66
+ node.with_ancestry({ include_root: options[:with_root] })
60
67
  else
61
68
  node
62
69
  end
63
70
 
64
71
  key = key_nodes.map { |kn| kn.get(options[:group_field]) { |n| n.object_id } }
65
-
66
- value = node.own_and_descendants(
67
- {
68
- field: options[:aggregation_field],
69
- if_field_missing: options[:if_field_missing]
70
- }
71
- )
72
+ value = node.own_and_descendants(options[:aggregation_field], &options[:if_field_missing])
72
73
 
73
74
  hash[key] = value
74
75
  end
@@ -21,9 +21,9 @@ module Forester
21
21
 
22
22
  def method_missing(name, *args, &block)
23
23
  if @hash.has_key?(name)
24
- @hash.fetch(name)
24
+ @hash[name]
25
25
  elsif block_given?
26
- yield self
26
+ yield args.fetch(0, {yield_to: self})[:yield_to]
27
27
  else
28
28
  raise ArgumentError.new("the node \"#{self.name}\" does not have any \"#{name}\"")
29
29
  end
@@ -5,13 +5,6 @@ module Forester
5
5
  include Mutators
6
6
  include Views
7
7
 
8
- def ancestry(include_root = true, include_self = false, descending = true)
9
- ancestors = self.parentage || []
10
- ancestors = ancestors[0...-1] unless include_root
11
- ancestors = ancestors.unshift(self) if include_self
12
- if descending then ancestors.reverse else ancestors end
13
- end
14
-
15
8
  def nodes_of_level(l)
16
9
  if l.between?(0, max_level) then each_level.take(l + 1).last else [] end
17
10
  end
@@ -35,7 +28,7 @@ module Forester
35
28
  alias_method :each_node, :breadth_each
36
29
 
37
30
  def get(field, &block)
38
- content.public_send(field, &block)
31
+ content.public_send(field, { yield_to: self }, &block)
39
32
  end
40
33
 
41
34
  def field_names
@@ -1,7 +1,7 @@
1
1
  module Forester
2
2
  class Version
3
- MAJOR = 1
4
- MINOR = 1
3
+ MAJOR = 2
4
+ MINOR = 0
5
5
  PATCH = 0
6
6
  PRE = nil
7
7
 
@@ -64,9 +64,7 @@ class TestTreeNode < Minitest::Test
64
64
  assert_equal 1, found_nodes.length
65
65
 
66
66
  actual_names = found_nodes.flat_map do |node|
67
- node.own_and_descendants({
68
- field: 'strings'
69
- })
67
+ node.own_and_descendants('strings')
70
68
  end
71
69
 
72
70
  assert_equal expected_names, actual_names
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forester
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugenio Bruno