Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

First commit

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

0 comments on commit 4b41800

Please sign in to comment.
Something went wrong with that request. Please try again.