Class: NodeQuery::NodeRules
- Inherits:
-
Object
- Object
- NodeQuery::NodeRules
- Defined in:
- lib/node_query/node_rules.rb
Constant Summary collapse
- KEYWORDS =
%i[not_includes includes not in not_in gt gte lt lte]
Instance Method Summary collapse
-
#initialize(rules) ⇒ NodeRules
constructor
Initialize a NodeRules.
-
#match_node?(node) ⇒ Boolean
Check if the node matches the rules.
-
#query_nodes(node, options = {}) ⇒ Array<Node>
Query nodes by the rules.
Constructor Details
#initialize(rules) ⇒ NodeRules
Initialize a NodeRules.
8 9 10 |
# File 'lib/node_query/node_rules.rb', line 8 def initialize(rules) @rules = rules end |
Instance Method Details
#match_node?(node) ⇒ Boolean
Check if the node matches the rules.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/node_query/node_rules.rb', line 51 def match_node?(node) flat_hash(@rules).keys.all? do |multi_keys| last_key = multi_keys.last actual = KEYWORDS.include?(last_key) ? NodeQuery::Helper.get_target_node(node, multi_keys[0...-1].join('.')) : NodeQuery::Helper.get_target_node(node, multi_keys.join('.')) expected = expected_value(@rules, multi_keys) expected = NodeQuery::Helper.evaluate_node_value(node, expected) if expected.is_a?(String) case last_key when :includes actual.any? { |actual_value| match_value?(actual_value, expected) } when :not_includes actual.all? { |actual_value| !match_value?(actual_value, expected) } when :not !match_value?(actual, expected) when :in expected.any? { |expected_value| match_value?(actual, expected_value) } when :not_in expected.all? { |expected_value| !match_value?(actual, expected_value) } when :gt actual > expected when :gte actual >= expected when :lt actual < expected when :lte actual <= expected else match_value?(actual, expected) end end end |
#query_nodes(node, options = {}) ⇒ Array<Node>
Query nodes by the rules.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/node_query/node_rules.rb', line 19 def query_nodes(node, = {}) = { including_self: true, stop_at_first_match: false, recursive: true }.merge() if [:including_self] && ![:recursive] return match_node?(node) ? [node] : [] end matching_nodes = [] if [:including_self] && match_node?(node) matching_nodes.push(node) return matching_nodes if [:stop_at_first_match] end if [:recursive] NodeQuery::Helper.handle_recursive_child(node) do |child_node| if match_node?(child_node) matching_nodes.push(child_node) break if [:stop_at_first_match] end end else NodeQuery.adapter.get_children(node).each do |child_node| if match_node?(child_node) matching_nodes.push(child_node) break if [:stop_at_first_match] end end end matching_nodes end |