Sha256: 3ffd61ca6063f1ab75f7fc280a7262b8604b08a745f88764a507ae913465a443

Contents?: true

Size: 1.97 KB

Versions: 27

Compression:

Stored size: 1.97 KB

Contents

# typed: false

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.com/terms.html

require 'sqreen/condition_evaluator'

module Sqreen
  # A module that will dynamically had preconditions to the pre/post/failing
  # callbacks
  module Conditionable
    # Hook the necessary callback function
    #
    # @param conditions [Hash] hash of callback names => conditions
    def condition_callbacks(conditions)
      conditions = {} if conditions.nil?
      base = self.class
      %w(pre post failing).each do |cb|
        conds = conditions[cb]
        next unless base.method_defined?(cb)
        send("#{cb}_conditions=", ConditionEvaluator.new(conds)) unless conds.nil?
        defd = base.instance_variable_defined?("@conditional_hooked_#{cb}")
        next if defd && base.instance_variable_get("@conditional_hooked_#{cb}")
        base.send(:alias_method, "#{cb}_without_conditions", cb)
        base.send(:alias_method, cb, "#{cb}_with_conditions")
        base.instance_variable_set("@conditional_hooked_#{cb}", true)
      end
    end

    def pre_with_conditions(inst, args, budget = nil, &block)
      eargs = [nil, framework, inst, args, @data, nil]
      return nil if !pre_conditions.nil? && !pre_conditions.evaluate(*eargs)
      pre_without_conditions(inst, args, budget, &block)
    end

    def post_with_conditions(rv, inst, args, budget = nil, &block)
      eargs = [nil, framework, inst, args, @data, rv]
      return nil if !post_conditions.nil? && !post_conditions.evaluate(*eargs)
      post_without_conditions(rv, inst, args, budget, &block)
    end

    def failing_with_conditions(rv, inst, args, budget = nil, &block)
      eargs = [nil, framework, inst, args, @data, rv]
      return nil if !failing_conditions.nil? && !failing_conditions.evaluate(*eargs)
      failing_without_conditions(rv, inst, args, budget, &block)
    end

    protected

    attr_accessor :pre_conditions, :post_conditions, :failing_conditions
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
sqreen-1.22.0-java lib/sqreen/conditionable.rb
sqreen-1.22.0 lib/sqreen/conditionable.rb
sqreen-1.21.1-java lib/sqreen/conditionable.rb
sqreen-1.21.1 lib/sqreen/conditionable.rb
sqreen-1.21.0-java lib/sqreen/conditionable.rb
sqreen-1.21.0 lib/sqreen/conditionable.rb
sqreen-1.20.4-java lib/sqreen/conditionable.rb
sqreen-1.20.4 lib/sqreen/conditionable.rb
sqreen-1.20.4.beta1 lib/sqreen/conditionable.rb
sqreen-1.19.4 lib/sqreen/conditionable.rb
sqreen-1.20.3 lib/sqreen/conditionable.rb
sqreen-1.20.2 lib/sqreen/conditionable.rb
sqreen-1.21.0.beta2 lib/sqreen/conditionable.rb
sqreen-1.21.0.beta1 lib/sqreen/conditionable.rb
sqreen-1.20.1-java lib/sqreen/conditionable.rb
sqreen-1.20.1 lib/sqreen/conditionable.rb
sqreen-1.20.0-java lib/sqreen/conditionable.rb
sqreen-1.20.0 lib/sqreen/conditionable.rb
sqreen-1.19.3-java lib/sqreen/conditionable.rb
sqreen-1.19.3 lib/sqreen/conditionable.rb