tukey 1.0.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: d8a8ef46adf65742612a80840d3efb68edc3e3c8
4
- data.tar.gz: cbc2848c54ad1f31aea26470ef20ef96177f0f7c
3
+ metadata.gz: 983ed9bbc18f085f8b33609ff13c6794db8140e3
4
+ data.tar.gz: e08a4102017038fc28cb4205ab16cb74e14f271f
5
5
  SHA512:
6
- metadata.gz: 2ac224235943f1e3cd63575bb1d0098cd6f86dee0d1f7e6fe2bfcfe41986d404a8fd28b8a11d751f6165adf757175d3aaa51a2098f5a7580b766334c03989c4c
7
- data.tar.gz: 4a4bbaf6322b6382e09a880871be262e92e4b7dc4c6ce05ee0672e5b9f5690c0f3d9a3dabb9e1ee110cb0f13f07489aa65c3385d0ec5b9a992b3cf585792b5b6
6
+ metadata.gz: 1f2f63eed3a6779330e6e5b3d6200100df1fd1046a8956d45215073ddf84b42b187c079452ac1b96aeb7bf5842c4a0aacbaadda25bdcc006348ffd39731c0667
7
+ data.tar.gz: 537c350faa79e346cbd2668dd4e1b68f0850d72b1bc8afc4178af9076d3659451d21e4bf3879291bd4461aaa37b3629ed0e03b3d82fcd9495776e80b031ace5d
@@ -51,12 +51,25 @@ class DataSet
51
51
  parent.children.reject { |c| c == self }
52
52
  end
53
53
 
54
- def oneling?
55
- siblings.none?
54
+ def ancestors
55
+ return [] if parent.nil?
56
+ ancs = []
57
+ par = parent
58
+ require 'pry'
59
+ until par.nil?
60
+ puts par.label.name
61
+ ancs.push par
62
+ par = par.parent
63
+ end
64
+ ancs.reverse
65
+ end
66
+
67
+ def label_path
68
+ [ancestors, self].flatten.map(&:label)
56
69
  end
57
70
 
58
- def compact_onelings
59
- filter(orphan_strategy: :adopt, keep_leafs: true) { |p, d| false if d.oneling? && !d.root? }
71
+ def oneling?
72
+ siblings.none?
60
73
  end
61
74
 
62
75
  def root?
@@ -94,8 +107,27 @@ class DataSet
94
107
  children.map(&:leaf_labels).flatten.uniq
95
108
  end
96
109
 
110
+ # Filter method that returns a new (dup) data_set with certain nodes filtered out
111
+ # Filtering is done through either passing:
112
+ # 1. a 'leaf_label_id'. All matching nodes will be present in the new set
113
+ # in their original position in the tree.
114
+ #
115
+ # 2. by passing a block that returns either `true`, `nil` or `false` for a given node:
116
+ # true: A node is kept, _including its children_
117
+ # nil: The matcher is indifferent about the node and will continue recursing the tree
118
+ # a. When at some point `true` is returned for a descendant node the whole branch will be
119
+ # kept up to and including the node for which the block returned `true`.
120
+ # b. If `true` is not returned the whole branch will not be included in the filter result,
121
+ # unless the option `keep_leafs` was set to true, in which case only nodes that were cut
122
+ # off with `false` will be excluded in the result.
123
+ # false: When the block returns false for a given node that node is taken out of the results
124
+ # this inludes its children, unless the option `orphan_strategy` was set to `:adopt` in
125
+ # which case the children will be filtered using the same block and appended to first ancestor
126
+ # node that was not excluded by the filter.
127
+ #
97
128
  def filter(leaf_label_id = nil, keep_leafs: false, orphan_strategy: :destroy, &block)
98
129
  fail ArgumentError, 'No block and no leaf_label_id passed' if !block_given? && leaf_label_id.nil?
130
+
99
131
  self.data.each_with_object(DataSet.new(id: id, label: label.deep_dup)) do |set, parent_set|
100
132
  if block_given?
101
133
  condition_met = yield(parent_set, set)
@@ -103,18 +135,26 @@ class DataSet
103
135
  condition_met = set.leaf? ? (set.label.id == leaf_label_id) : nil
104
136
  end
105
137
 
138
+ set_dup = set.deep_dup
139
+
140
+ # We want to have this node and its children
106
141
  if condition_met == true
107
- parent_set.add_item(set.deep_dup)
108
- elsif condition_met.nil? && set.data_array?
109
- deep_filter_result = set.deep_dup.filter(leaf_label_id, keep_leafs: keep_leafs, orphan_strategy: orphan_strategy, &block)
110
- parent_set.add_item(deep_filter_result) if deep_filter_result.data
111
- elsif condition_met == false && set.data_array?
112
- if orphan_strategy == :adopt
113
- deep_filter_result = set.deep_dup.filter(leaf_label_id, keep_leafs: keep_leafs, orphan_strategy: orphan_strategy, &block)
114
- deep_filter_result.children.each { |c| parent_set.add_item(c) } if deep_filter_result.data
142
+ parent_set.add_item(set_dup)
143
+ # Complex looking clause, but useful for performance and DRY-ness
144
+ elsif set.data_array? && (condition_met.nil? || (condition_met == false && orphan_strategy == :adopt))
145
+ deep_filter_result = set_dup.filter(leaf_label_id, keep_leafs: keep_leafs, orphan_strategy: orphan_strategy, &block)
146
+
147
+ # Here is where either the taking along or adopting of nodes happens
148
+ if deep_filter_result.data
149
+ # Filtering underlying children and adding the potential filter result to parent.
150
+ parent_set.add_item(deep_filter_result) if condition_met.nil?
151
+
152
+ # We are losing the node, but since 'orphan_strategy' == :adopt we will adopt the orphans that match the filter
153
+ deep_filter_result.children.each { |c| parent_set.add_item(c) } if condition_met == false
115
154
  end
155
+ # We are indifferent to the match (nil), but since the node is a leaf we will keep it
116
156
  elsif condition_met.nil? && set.leaf?
117
- parent_set.add_item(set) if keep_leafs
157
+ parent_set.add_item(set_dup) if keep_leafs
118
158
  end
119
159
 
120
160
  parent_set
data/lib/tukey/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tukey
2
- VERSION = "1.0.0"
2
+ VERSION = "2.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tukey
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Achilleas Buisman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-23 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler