Sha256: 3f54c26a5b314ccdb631949dc5f9a860550c4e758b5f6372df24091dc75955a3

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require 'singleton'

module PaymentTest
  class State

    include Singleton

    def initialize
      reset_configuration
    end

    def configure_always_throw(methods=nil)
      configure_methods(methods)
      @always_throw = true
      log_current_state
    end

    def configure_return_nil(methods=nil)
      configure_methods(methods)
      @always_return_nil = true
      log_current_state
    end

    def configure_sleep_time(sleep_time_sec, methods=nil)
      configure_methods(methods)
      @sleep_time_sec = sleep_time_sec
      log_current_state
    end

    def reset_configuration
      @always_throw = false
      @always_return_nil = false
      @sleep_time_sec = nil
      @methods = nil
      log_current_state
    end

    def always_throw(method)
      @always_throw && is_for_method(method)
    end

    def always_return_nil(method)
      @always_return_nil && is_for_method(method)
    end

    def sleep_time_sec(method)
      return @sleep_time_sec if @sleep_time_sec && is_for_method(method)
      nil
    end

    def is_clear
      return !@always_throw && !@always_return_nil && @sleep_time_sec.nil?
    end

    def log_current_state
      puts "PaymentTest:State : @always_throw = #{@always_throw}, @always_return_nil = #{@always_return_nil}, @sleep_time_sec = #{@sleep_time_sec}, @methods=#{@methods}"
    end

    private

    def configure_methods(methods=nil)
      @methods = methods.split(",")
    end

    def is_for_method(method)
      # If no methods were configured, this apply to all of them
      return true if @methods.nil?

      @methods.include? method.to_s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
killbill-payment-test-3.2.0 lib/payment_test/state.rb
killbill-payment-test-3.1.0 lib/payment_test/state.rb