Sha256: 1496a4ceb87595d6a47bc12db63b12033d6e44161514ea7bcc3570a71a5392e4
Contents?: true
Size: 1.43 KB
Versions: 3
Compression:
Stored size: 1.43 KB
Contents
# frozen_string_literal: true # # Copyright (c) 2018-present, Blue Marble Payroll, LLC # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. # module TreeBranch # This class understands how to take a tree, digest it given a context, set of comparators, and a # block, then returns a new tree structure. class Processor def process(node, context: nil, comparators: [], &block) return nil if at_least_one_comparator_returns_false?(node.data, context, comparators) valid_children = process_children(node.children, context, comparators, &block) if block_given? yield(node.data, valid_children, context) else ::TreeBranch::Node.new(node.data) .add(valid_children) end end private def at_least_one_comparator_returns_false?(data, context, comparators) Array(comparators).any? { |c| execute_comparator(c, data, context) == false } end def process_children(children, context, comparators, &block) children.map do |node| process(node, context: context, comparators: comparators, &block) end.compact end def execute_comparator(comparator, data, context) if comparator.is_a?(Proc) comparator.call(OpenStruct.new(data), OpenStruct.new(context)) else comparator.new(data: data, context: context).valid? end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
tree_branch-1.1.1 | lib/tree_branch/processor.rb |
tree_branch-1.1.0 | lib/tree_branch/processor.rb |
tree_branch-1.0.0 | lib/tree_branch/processor.rb |