Sha256: 206510c3fb2253ad0e4fabdcc0022ac763b3b5a117951f484c3b18f579133d77

Contents?: true

Size: 988 Bytes

Versions: 1

Compression:

Stored size: 988 Bytes

Contents

module God
  module Conditions
    class Tries < PollCondition
      attr_accessor :times, :within

      def prepare
        @timeline = Timeline.new(times)
      end

      def reset
        @timeline.clear
      end

      def valid?
        valid = true
        valid &= complain("Attribute 'times' must be specified", self) if times.nil?
        valid
      end

      def test
        @timeline << Time.now

        consensus = (@timeline.size == times)
        duration = within.nil? || (@timeline.last - @timeline.first) < within

        history = if within
                    "[#{@timeline.size}/#{times} within #{(@timeline.last - @timeline.first).to_i}s]"
                  else
                    "[#{@timeline.size}/#{times}]"
                  end

        if consensus && duration
          self.info = "tries exceeded #{history}"
          true
        else
          self.info = "tries within bounds #{history}"
          false
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resurrected_god-1.0.0 lib/god/conditions/tries.rb