From ac6502bc599ca7f9e14df272ce5e9c4c04600c55 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Fri, 21 Jun 2013 15:02:47 +0200 Subject: [PATCH] Add child nameing helpers --- lib/mutant/mutator/node.rb | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/lib/mutant/mutator/node.rb b/lib/mutant/mutator/node.rb index e7f3b59c..2254e435 100644 --- a/lib/mutant/mutator/node.rb +++ b/lib/mutant/mutator/node.rb @@ -17,6 +17,59 @@ module Mutant Unparser.unparse(node) end + # Define named child + # + # @param [Symbol] name + # @param [Fixnum] index + # + # @return [undefined] + # + # @api private + # + def self.define_named_child(name, index) + define_method("#{name}") do + children[index] + end + + define_method("emit_#{name}_mutations") do + mutate_child(index) + end + + define_method("emit_#{name}") do |node| + emit_child_update(index, node) + end + end + private_class_method :define_named_child + + # Define remaining children + # + # @param [Fixnum] from_index + # + # @return [undefined] + # + # @api private + # + def self.define_remaining_children(from_index) + define_method(:remaining_children) do + children[from_index..-1] + end + end + private_class_method :define_remaining_children + + # Create name helpers + # + # @return [undefined] + # + # @api private + # + def self.children(*names) + names.each_with_index do |name, index| + define_named_child(name, index) + end + define_remaining_children(names.length) + end + private_class_method :children + private # Return mutated node