Module: Mutant::ClassMethods
- Defined in:
- lib/ruby-mutant/base.rb
Instance Method Summary collapse
- #required_attr(*attrs) ⇒ Object
-
#run(args = {}) ⇒ Object
The entry point, main method that will be execute any mutation logic.
Instance Method Details
#required_attr(*attrs) ⇒ Object
16 17 18 19 20 21 22 |
# 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 = {}) ⇒ Object
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, and success definition of the mutation
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 84 85 86 87 88 89 90 91 92 93 94 |
# 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 |