Sha256: e0e8cc57f73e9c97cfc020624e71554c84665cbf247ec1a00996638a10268604

Contents?: true

Size: 1.15 KB

Versions: 108

Compression:

Stored size: 1.15 KB

Contents

module Pageflow
  class Quota
    class ExhaustedError < RuntimeError
      attr_reader :quota

      def initialize(quota)
        @quota = quota
      end
    end

    class ExceededError < ExhaustedError; end

    attr_reader :name, :account

    def initialize(name, account)
      @name = name
      @account = account
    end

    def state
      raise(NotImplementedError, 'Quota#state must be implemented and return either "available", "exhausted" or "exceeded".')
    end

    def state_description
      raise(NotImplementedError, 'Quota#state_description must be implemented.')
    end

    def available?
      state == 'available'
    end

    def exhausted?
      !available?
    end

    def exceeded?
      state == 'exceeded'
    end

    def verify_available!
      raise(ExhaustedError.new(self), "Quota '#{name}' exhausted.") unless available?
    end

    def verify_not_exceeded!
      raise(ExceededError.new(self), "Quota '#{name}' exceeded.") if exceeded?
    end

    def assume(assumptions)
      self
    end

    class Unlimited < Quota
      def state
        'available'
      end

      def state_description
        nil
      end
    end
  end
end

Version data entries

108 entries across 108 versions & 1 rubygems

Version Path
pageflow-17.0.4 lib/pageflow/quota.rb
pageflow-17.0.3 lib/pageflow/quota.rb
pageflow-17.0.2 lib/pageflow/quota.rb
pageflow-17.0.1 lib/pageflow/quota.rb
pageflow-17.0.0 lib/pageflow/quota.rb
pageflow-16.2.0 lib/pageflow/quota.rb
pageflow-16.1.0 lib/pageflow/quota.rb
pageflow-16.0.0 lib/pageflow/quota.rb
pageflow-15.8.0 lib/pageflow/quota.rb
pageflow-14.5.2 lib/pageflow/quota.rb
pageflow-15.7.1 lib/pageflow/quota.rb
pageflow-15.7.0 lib/pageflow/quota.rb
pageflow-15.6.1 lib/pageflow/quota.rb
pageflow-15.6.0 lib/pageflow/quota.rb
pageflow-15.5.0 lib/pageflow/quota.rb
pageflow-15.4.0 lib/pageflow/quota.rb
pageflow-15.3.0 lib/pageflow/quota.rb
pageflow-15.2.2 lib/pageflow/quota.rb
pageflow-15.2.1 lib/pageflow/quota.rb
pageflow-15.2.0 lib/pageflow/quota.rb