Sha256: 87f6c25f74ab472405a87048deb91a35bb3ff37b9fec2cd18a68c5c4033e7216

Contents?: true

Size: 552 Bytes

Versions: 2

Compression:

Stored size: 552 Bytes

Contents

module Mongoid
  module Haystack
    class Count
      include Mongoid::Document

      field(:name, :type => String)
      field(:value, :type => Integer, :default => 0)

      index({:name => 1}, {:unique => true})
      index({:value => 1})

      def Count.for(name)
        Haystack.find_or_create(
          ->{ where(:name => name.to_s).first },
          ->{ create!(:name => name.to_s) }
        )
      end

      def Count.[](name)
        Count.for(name)
      end

      def inc(n = 1)
        super(:value, n)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-haystack-1.1.0 lib/mongoid-haystack/count.rb
mongoid-haystack-1.0.0 lib/mongoid-haystack/count.rb