Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for passing a list of queries to count. Version bump to…

… 0.6.0.
  • Loading branch information...
commit 0a294377373e5078d2262cf44a4a2a7c51af7263 1 parent d36eedc
@rymohr rymohr authored
View
2  Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- headcount (0.0.1)
+ headcount (0.6.0)
rails (~> 3.2.2)
GEM
View
7 README.md
@@ -33,6 +33,9 @@ Headcount.configure do |config|
# you can specify the key yourself if you'd like to override the default
count User.active, :as => :active_members
+
+ # if you're fine with using default keys you can pass a list of queries instead
+ count User, Account
end
```
@@ -61,8 +64,8 @@ Your options are open as far as scheduling goes. If you like [crontab](http://c
You have two options for triggering a headcount.
-1. Call `Headcount.count!` directly
-2. Use `rake headcount`
+1. Call it directly: `Headcount.count!`
+2. Use the rake task: `rake headcount`
In the future I may write a daemon to simplify the scheduling process with an upstart script to boot, but for now you're on your own.
View
39 lib/headcount/configuration.rb
@@ -34,17 +34,38 @@ def evaluate(&block)
instance_exec(self, &block)
end
- def count(query, options = {}, &block)
- raise UnsupportedQuery, 'query does not respond to :count' unless query.respond_to?(:count)
+ def count(*arguments, &block)
+ if list_given?(*arguments)
+ count_each(arguments.flatten)
+ else
+ query = arguments[0]
+ options = arguments[1] || {}
+
+ if query.respond_to?(:count)
+ key = options[:as] || Headcount::Support.key_for(query)
- key = options[:as] || Headcount::Support.key_for(query)
+ # just ignore blocks for now
+ # not really finding a need for nested declarations
+ #if block_given?
+ # evaluate(&block)
+ #end
- # just ignore blocks for now
- #if block_given?
- # evaluate(&block)
- #end
-
- Headcount.register(key, query)
+ Headcount.register(key, query)
+ else
+ raise UnsupportedQuery, 'query does not respond to :count'
+ end
+ end
+ end
+
+ private
+ def list_given?(*arguments)
+ arguments[0].is_a?(Array) || (arguments.length > 1 && !arguments[1].is_a?(Hash))
+ end
+
+ def count_each(queries)
+ queries.each do |query|
+ count(query)
+ end
end
end
end
View
2  lib/headcount/version.rb
@@ -1,3 +1,3 @@
module Headcount
- VERSION = "0.5.0"
+ VERSION = "0.6.0"
end
View
2  spec/dummy/app/models/account.rb
@@ -0,0 +1,2 @@
+class Account < ActiveRecord::Base
+end
View
8 spec/dummy/db/migrate/20120406225757_add_accounts.rb
@@ -0,0 +1,8 @@
+class AddAccounts < ActiveRecord::Migration
+ def change
+ create_table :accounts, :force => true do |t|
+ t.references :user
+ t.timestamps
+ end
+ end
+end
View
8 spec/dummy/db/schema.rb
@@ -11,7 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120406005419) do
+ActiveRecord::Schema.define(:version => 20120406225757) do
+
+ create_table "accounts", :force => true do |t|
+ t.integer "user_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
create_table "users", :force => true do |t|
t.datetime "last_request_at"
View
44 spec/models/configuration_spec.rb
@@ -20,24 +20,42 @@
end
describe '#count' do
- it 'should return the registration key' do
- config.count(User).should eq(:users)
- end
-
- it 'should add the given query to the registry' do
- Headcount.find(config.count(User)).should eq(User)
+ context 'when given a single query' do
+ it 'should return the registration key' do
+ config.count(User).should eq(:users)
+ end
+
+ it 'should add the given query to the registry' do
+ Headcount.find(config.count(User)).should eq(User)
+ end
+
+ context 'when key is given' do
+ it 'should override the default key' do
+ config.count(User, :as => :members)
+ Headcount.find(:members).should eq(User)
+ end
+ end
+
+ context 'when a bad query is given' do
+ it 'throws a Headcount::UnsupportedQuery error' do
+ lambda { config.count(Object) }.should raise_error(Headcount::UnsupportedQuery)
+ end
+ end
end
- context 'when key is given' do
- it 'should override the default key' do
- config.count(User, :as => :members)
- Headcount.find(:members).should eq(User)
+ context 'when given an explicit array' do
+ it 'should register each query using the implied key' do
+ config.count([User, Account])
+ Headcount.find(:users).should eq(User)
+ Headcount.find(:accounts).should eq(Account)
end
end
- context 'when a bad query is given' do
- it 'throws a Headcount::UnsupportedQuery error' do
- lambda { config.count(Object) }.should raise_error(Headcount::UnsupportedQuery)
+ context 'when given an implicit array' do
+ it 'should register each query using the implied key' do
+ config.count(User, Account)
+ Headcount.find(:users).should eq(User)
+ Headcount.find(:accounts).should eq(Account)
end
end
end
View
6 spec/tmp/headcount.json
@@ -1,3 +1,3 @@
-{"timestamp":"2012-04-06 11:00:16","users":0}
-{"timestamp":"2012-04-06 11:00:16","users":0}
-{"timestamp":"2012-04-06 11:00:16","users":0}
+{"timestamp":"2012-04-06 13:30:14","users":0,"accounts":0}
+{"timestamp":"2012-04-06 13:30:14","users":0,"accounts":0}
+{"timestamp":"2012-04-06 13:30:14","users":0,"accounts":0}
Please sign in to comment.
Something went wrong with that request. Please try again.