Sha256: 34a67b696f1fceae4f11acaf77156edcb5047c9869a6b886b8de9eb9a05e33b6
Contents?: true
Size: 1.67 KB
Versions: 19
Compression:
Stored size: 1.67 KB
Contents
# encoding: utf-8 module Synvert::Core # Condition checks if rules matches. class Rewriter::Condition # Initialize a condition. # # @param instance [Synvert::Core::Rewriter::Instance] # @param rules [Hash] # @param block [Block] # @return [Synvert::Core::Rewriter::Condition] def initialize(instance, rules, &block) @instance = instance @rules = rules @block = block end # If condition matches, run the block code. def process @instance.instance_eval &@block if match? end end # IfExistCondition checks if matching node exists in the node children. class Rewriter::IfExistCondition < Rewriter::Condition # check if any child node matches the rules. def match? match = false @instance.current_node.recursive_children do |child_node| match = match || (child_node && child_node.match?(@rules)) end match end end # UnlessExistCondition checks if matching node doesn't exist in the node children. class Rewriter::UnlessExistCondition < Rewriter::Condition # check if none of child node matches the rules. def match? match = false @instance.current_node.recursive_children do |child_node| match = match || (child_node && child_node.match?(@rules)) end !match end end # IfExistCondition checks if node has only one child node and the child node matches rules. class Rewriter::IfOnlyExistCondition < Rewriter::Condition # check if only have one child node and the child node matches rules. def match? @instance.current_node.body.size == 1 && @instance.current_node.body.first.match?(@rules) end end end
Version data entries
19 entries across 19 versions & 1 rubygems