diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..c0e8630 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in groupdate.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..e2daad2 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2013 Andrew Kane + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2ab9458 --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +# Groupdate + +The simplest way to group by: + +- day +- week +- month +- year +- hour +- microseconds +- milliseconds +- second +- minute +- quarter +- decade +- century +- millennium + +:tada: Time zones supported!! + +PostgreSQL only at the moment - support for other datastores coming soon + +## Usage + +```ruby +User.group_by_day(:created_at).count +# => {2013-04-16 00:00:00 UTC=>50,2013-04-17 00:00:00 UTC=>100} + +Task.group_by_month(:updated_at).count +# => {2013-04-01 00:00:00 UTC=>23,2013-04-01 00:00:00 UTC=>44} + +Goal.group_by_year(:accomplished_at).count +# => {2012-01-01 00:00:00 UTC=>11,2013-01-01 00:00:00 UTC=>3} +``` + +The default time zone is `Time.zone`. Pass a time zone as the second argument. + +```ruby +User.group_by_day(:created_at, ActiveSupport::TimeZone["Pacific Time (US & Canada)"]).count +# => {2013-04-16 00:00:00 UTC=>80,2013-04-17 00:00:00 UTC=>70} +``` + +Use it with anything that you can use `group` with: + +```ruby +User.group_by_week(:created_at).sum(:tasks_count) + +User.group_by_hour(:created_at).average(:tasks_count) + +User.group_by_quarter(:created_at).maximum(:tasks_count) + +User.group_by_second(:created_at).average(:tasks_count) +``` + +## Installation + +Add this line to your application's Gemfile: + +```ruby +gem 'groupdate' +``` + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2995527 --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require "bundler/gem_tasks" diff --git a/groupdate.gemspec b/groupdate.gemspec new file mode 100644 index 0000000..16b42b1 --- /dev/null +++ b/groupdate.gemspec @@ -0,0 +1,25 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'groupdate/version' + +Gem::Specification.new do |spec| + spec.name = "groupdate" + spec.version = Groupdate::VERSION + spec.authors = ["Andrew Kane"] + spec.email = ["acekane1@gmail.com"] + spec.description = %q{The simplest way to group temporal data} + spec.summary = %q{The simplest way to group temporal data} + spec.homepage = "" + spec.license = "MIT" + + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_dependency "activerecord", ">= 3.0.0" + + spec.add_development_dependency "bundler", "~> 1.3" + spec.add_development_dependency "rake" +end diff --git a/lib/groupdate.rb b/lib/groupdate.rb new file mode 100644 index 0000000..cd04b0b --- /dev/null +++ b/lib/groupdate.rb @@ -0,0 +1,44 @@ +require "groupdate/version" + +module Groupdate + extend ActiveSupport::Concern + + # Pattern from kaminari + # https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/models/active_record_extension.rb + included do + # Future subclasses will pick up the model extension + class << self + def inherited_with_groupdate(kls) #:nodoc: + inherited_without_groupdate kls + kls.send(:include, ClassMethods) if kls.superclass == ActiveRecord::Base + end + alias_method_chain :inherited, :groupdate + end + + # Existing subclasses pick up the model extension as well + self.descendants.each do |kls| + kls.send(:include, ClassMethods) if kls.superclass == ActiveRecord::Base + end + end + + module ClassMethods + extend ActiveSupport::Concern + + included do + # Field list from + # http://www.postgresql.org/docs/9.1/static/functions-datetime.html + %w(microseconds milliseconds second minute hour day week month quarter year decade century millennium).each do |field| + self.scope :"group_by_#{field}", lambda {|column, time_zone = Time.zone| + if time_zone.is_a?(ActiveSupport::TimeZone) + time_zone = time_zone.tzinfo.name + end + sql = "DATE_TRUNC('#{field}', #{column}::timestamptz AT TIME ZONE ?) AT TIME ZONE ?" + group(sanitize_sql_array([sql, time_zone, time_zone])) + } + end + end + end + +end + +ActiveRecord::Base.send :include, Groupdate diff --git a/lib/groupdate/version.rb b/lib/groupdate/version.rb new file mode 100644 index 0000000..b54ed80 --- /dev/null +++ b/lib/groupdate/version.rb @@ -0,0 +1,3 @@ +module Groupdate + VERSION = "0.0.1" +end -- libgit2 0.21.0