Sha256: cef250a052043a8da7006b981856695e99778739a2e90184ea63e3c8e1ccbe22

Contents?: true

Size: 1.34 KB

Versions: 53

Compression:

Stored size: 1.34 KB

Contents

module Resque
  # The stat subsystem. Used to keep track of integer counts.
  #
  #   Get a stat:  Stat[name]
  #   Incr a stat: Stat.incr(name)
  #   Decr a stat: Stat.decr(name)
  #   Kill a stat: Stat.clear(name)
  module Stat
    extend self
    extend Helpers

    # Returns the int value of a stat, given a string stat name.
    def get(stat)
      res = mongo_stats.find_one(:stat => stat)
      return 0 unless res
      res['value'].to_i
    end

    # Alias of `get`
    def [](stat)
      get(stat)
    end

    # For a string stat name, increments the stat by one.
    #
    # Can optionally accept a second int parameter. The stat is then
    # incremented by that amount.
    def incr(stat, by = 1)
      mongo_stats.update({:stat => stat}, {'$inc' => {:value => by}}, :upsert => true)
    end

    # Increments a stat by one.
    def <<(stat)
      incr stat
    end

    # For a string stat name, decrements the stat by one.
    #
    # Can optionally accept a second int parameter. The stat is then
    # decremented by that amount.
    def decr(stat, by = 1)
      mongo_stats.update({:stat => stat}, {'$inc' => {:value => -by}})
    end

    # Decrements a stat by one.
    def >>(stat)
      decr stat
    end

    # Removes a stat from Redis, effectively setting it to 0.
    def clear(stat)
      mongo_stats.remove(:stat => stat)
    end
  end
end

Version data entries

53 entries across 53 versions & 3 rubygems

Version Path
classiccms-0.5.13 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.12 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.11 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.10 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.9 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.8 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.7 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.6 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.5 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.2 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.1 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.5.0 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.4.2 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.4.1 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.4.0 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.3.9 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.3.8 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.3.7 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.3.6 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb
classiccms-0.3.5 vendor/bundle/gems/resque-mongo-1.9.8.1/lib/resque/stat.rb