class SelfDestruct::Agent

Constants

DEFAULT_FAILURE_FUNC
DEFAULT_GRACE_PERIOD
DEFAULT_MAX_COUNT
DEFAULT_MIN_ATTEMPTS
DEFAULT_RATIO_THRESHOLD

Public Class Methods

new(opts = {}) click to toggle source
# File lib/self_destruct.rb, line 11
def initialize(opts = {})
  @success_count   = 0
  @failure_count   = 0
  @start_time      = nil
  @ratio_threshold = opts[:ratio_threshold] || DEFAULT_RATIO_THRESHOLD
  @grace_period    = opts[:grace_period]    || DEFAULT_GRACE_PERIOD
  @min_attempts    = opts[:min_attempts]    || DEFAULT_MIN_ATTEMPTS
  @failure_func    = opts[:failure_func]    || DEFAULT_FAILURE_FUNC
  @max_count       = opts[:max_count]       || DEFAULT_MAX_COUNT
end

Public Instance Methods

get_total() click to toggle source
# File lib/self_destruct.rb, line 38
def get_total
  @success_count + @failure_count
end
inc_failures() click to toggle source
# File lib/self_destruct.rb, line 27
def inc_failures
  @failure_count += 1
  check_ratio!
end
inc_successes() click to toggle source
# File lib/self_destruct.rb, line 22
def inc_successes
  @success_count += 1
  check_ratio!
end
reset!() click to toggle source
# File lib/self_destruct.rb, line 32
def reset!
  @success_count = 0 
  @failure_count = 0
  @start_time    = nil
end

Private Instance Methods

check_ratio!() click to toggle source
# File lib/self_destruct.rb, line 48
def check_ratio!
  ratio = get_ratio
  if (ratio < @ratio_threshold) and grace_period_has_passed? and minimum_attempts_reached?
    do_failure
  end
  check_total_num
end
check_total_num() click to toggle source
# File lib/self_destruct.rb, line 56
def check_total_num
  reset! if get_total > @max_count
end
do_failure() click to toggle source
# File lib/self_destruct.rb, line 73
def do_failure
  @failure_func.call(@success_count, @failure_count)
end
get_ratio() click to toggle source
# File lib/self_destruct.rb, line 69
def get_ratio
  @success_count.to_f / @failure_count.to_f
end
grace_period_has_passed?() click to toggle source
# File lib/self_destruct.rb, line 64
def grace_period_has_passed?
  set_start_time
  (Time.now - @start_time) > @grace_period
end
minimum_attempts_reached?() click to toggle source
# File lib/self_destruct.rb, line 60
def minimum_attempts_reached?
  get_total >= @min_attempts 
end
set_start_time() click to toggle source
# File lib/self_destruct.rb, line 44
def set_start_time
  @start_time ||= Time.now
end