Permalink
Browse files

First commit

  • Loading branch information...
winton committed Jun 9, 2010
1 parent 27ecb71 commit 4b41800e35a3ecdd4cce99724f6010a653223d77
View
@@ -1,36 +1,43 @@
-GemTemplate
-===========
+PeriodicCounter
+===============
-A gem template for new projects.
+Maintains period fields on any counter column in your database
Requirements
------------
<pre>
-sudo gem install stencil
+sudo gem install periodic_template
</pre>
-Setup the template
-------------------
+Create columns
+--------------
-You only have to do this once.
+For every counter column (X), you should also have a <code>X\_computed\_at</code> datetime column.
-<pre>
-git clone git@github.com:winton/gem_template.git
-cd gem_template
-stencil
-</pre>
+To define period columns, use this format: <code>X\_last\_1\_week</code> or <code>X\_last\_6\_hours</code>.
+
+The name the period column should follow the format of ActiveSupport's time extensions:
+
+http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Numeric/Time.html
-Setup a new project
--------------------
+Create configuration
+--------------------
-Do this for every new project.
+Create a file, <code>config/counters.yml</code>:
<pre>
-mkdir my_project
-git init
-stencil gem_template
-rake rename
+my_table_name:
+ - my_counter
</pre>
-The last command does a find-replace (gem\_template -> my\_project) on files and filenames.
+The plugin assumes that the <code>config</code> directory also houses your <code>database.yml</code> file.
+
+Create cron entry
+-----------------
+
+Add the following command to your crontab at a period of your choosing:
+
+<pre>
+cd /path/to/your/app && RAILS_ENV=production periodic_counter
+</pre>
View
@@ -1,4 +0,0 @@
-#!/usr/bin/env ruby
-
-require File.expand_path("#{File.dirname(__FILE__)}/../require")
-Require.bin!
View
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby
+
+require File.expand_path("#{File.dirname(__FILE__)}/../require")
+Require.bin!
+
+PeriodicCounter.new(
+ ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development',
+ Dir.pwd
+)
View
@@ -1,13 +0,0 @@
-require File.expand_path("#{File.dirname(__FILE__)}/../require")
-Require.lib!
-
-class GemTemplate
-
- def initialize(environment, root)
- @db, @log, @mail = ActiveWrapper.setup(
- :base => root,
- :env => environment
- )
- @db.establish_connection
- end
-end
No changes.
View
@@ -0,0 +1,36 @@
+require File.expand_path("#{File.dirname(__FILE__)}/../require")
+Require.lib!
+
+class PeriodicCounter
+
+ def initialize(environment, root)
+ @db, @log, @mail = ActiveWrapper.setup(
+ :base => root,
+ :env => environment
+ )
+ @db.establish_connection
+
+ @tables = ActiveRecord::Base.connection.tables.inject({}) do |hash, table|
+ hash[table] = ActiveRecord::Base.connection.columns(table).collect(&:name)
+ end
+
+ if File.exists?(counters_yml = "#{root}/config/counters.yml")
+ @counters = YAML::load(File.open(counters_yml))
+ else
+ raise "#{counters_yml} not found"
+ end
+
+ @counters.each do |table, counters|
+ columns = @tables[table]
+ if columns
+ columns.each do |column|
+ if counters.include?(column)
+ period_columns = columns.collect do |col|
+ col =~ /^#{column}/ && !col.include?('computed_at')
+ end
+ end
+ end
+ end
+ end
+ end
+end
View
@@ -15,17 +15,17 @@
gem :require
end
email 'mail@wintoni.us'
- name 'gem_template'
+ name 'periodic_counter'
homepage "http://github.com/winton/#{name}"
summary ""
version '0.1.0'
end
- bin { require 'lib/gem_template' }
+ bin { require 'lib/periodic_counter' }
lib do
gem :active_wrapper
- require 'lib/gem_template/gem_template'
+ require 'yaml'
end
rakefile do
@@ -36,7 +36,7 @@
spec_helper do
require 'require/spec_helper'
- require 'lib/gem_template'
+ require 'lib/periodic_counter'
require 'pp'
end
-end
+end
View
@@ -2,4 +2,4 @@
Require.spec_helper!
Spec::Runner.configure do |config|
-end
+end

0 comments on commit 4b41800

Please sign in to comment.