module Mutant::ClassMethods
Public Instance Methods
required_attr(*attrs)
click to toggle source
# File lib/ruby-mutant/base.rb, line 16 def required_attr(*attrs) puts 'Mutant::required_attr' # This defines a method called required_attr that will # return an array of symbols for all the properties of the # mutation that we should have defined. define_method(:required_attr) { attrs ||= [] } end
run(args = {})
click to toggle source
The entry point, main method that will be execute any mutation logic
Parameters:¶ ↑
- args
-
A hash of inputs suppplied when the user runs the mutation. i.e. MyMutation.run(name: 'jon', house: 'stark')
Returns:¶ ↑
An instance of `Mutant::Output`. This object defines all errors, metadata and success definition of the mutation
# File lib/ruby-mutant/base.rb, line 34 def run(args = {}) unless args.has_key?(:raise_on_error) args[:raise_on_error] = true end args[:raise_on_error].freeze puts 'Mutant::self.run(*args) ' obj = new(args) # Ensure the mutation has the correct method unless obj.respond_to?(:execute) raise MutationSetupException.new(msg='Missing execute method') end # 1. We want to run the validators first, then determine if we should continue errs = obj.send(:validate) obj.output.errors = obj.output.errors + errs if errs.length > 0 if args[:raise_on_error] raise MutationSetupException.new(msg='Validation failed') end end # 2. Check to see the mutation has the corresponding inst vars args.each do |k, val| puts "Mutant::var check '#{k}', responds? #{obj.respond_to? k.to_sym}" # First make sure this mutation obj has the correct vars, # if not, then proceeed to create them unless obj.respond_to? k.to_sym # create the attr_accessor for the missing vars obj.class.send(:define_method, "#{k}=".to_sym) do |value| instance_variable_set("@" + k.to_s, value) end obj.class.send(:define_method, k.to_sym) do instance_variable_get("@" + k.to_s) end end # 3. Propagate the values from the mutation props to the class obj.send("#{k}=".to_sym, val) end # 3 If this instance defines :required_attr if obj.respond_to? :required_attr required_attr_errors = obj.send(:check_required_attrs) unless required_attr_errors.length == 0 # We need to handle any errors we get back from our # required_attr validator obj.output.errors += required_attr_errors if args[:raise_on_error] raise required_attr_errors[0] end end end # 4. Run execute method to run mutation logic obj.execute(args) # Return out Output obj, with all meta data regarding the ran mutation obj.output end