aggtive_record 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzY2MmY5YWYwNGY3NTQ2OGZjOWFkNmQ0OGMzMGY0OTBhYTI5YWU5ZQ==
5
+ data.tar.gz: !binary |-
6
+ OGMyYzBiZTRiYWI1NjBhYzA2YTFmYzAwY2U0YzI1MWU0MzYyYmM5Yg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MzFjZDIyMzk5MjVlOTNjYjA0ZWFlZjZhM2Q1MzcxMmUzZjQxNWQ2NTZmODRj
10
+ ZDFkMGIwZjNiODNhODk1NWU1Zjg2Y2UwNzFlM2E5ZDM0YTgwM2UzODA1NzA3
11
+ MmJkN2FiNjFjNGU1NTRkYWNiMzBmNTgyYWE1ODM3MTU2OWI2NGM=
12
+ data.tar.gz: !binary |-
13
+ OWYzYjlhNjgwMWZmZjc2MjBmYTUxMjEzYThmOTY0NDQ0M2VhMTg2ZTlmMzYz
14
+ M2FlZDQ5NzlmMGJmYTc2ZTczZmY0MDFmNGExNWQwNmUxOTE2NzFmMWUwZDRi
15
+ MjMwMzZlYjEzOTIyZGFiMzZkYmRmNWYyYTZiMzI2NzJmODE5NWQ=
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+
4
+ group :development do
5
+ gem "rspec", "~> 2.14.1"
6
+ gem "bundler"
7
+ gem "jeweler", "~> 1.8.4"
8
+ gem 'pry'
9
+ end
10
+
11
+ gem 'activesupport', '~> 3.2.14'
data/Gemfile.lock ADDED
@@ -0,0 +1,75 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.14)
5
+ i18n (~> 0.6, >= 0.6.4)
6
+ multi_json (~> 1.0)
7
+ addressable (2.3.5)
8
+ builder (3.2.2)
9
+ coderay (1.0.9)
10
+ diff-lcs (1.2.4)
11
+ faraday (0.8.8)
12
+ multipart-post (~> 1.2.0)
13
+ git (1.2.6)
14
+ github_api (0.10.1)
15
+ addressable
16
+ faraday (~> 0.8.1)
17
+ hashie (>= 1.2)
18
+ multi_json (~> 1.4)
19
+ nokogiri (~> 1.5.2)
20
+ oauth2
21
+ hashie (2.0.5)
22
+ highline (1.6.19)
23
+ httpauth (0.2.0)
24
+ i18n (0.6.5)
25
+ jeweler (1.8.7)
26
+ builder
27
+ bundler (~> 1.0)
28
+ git (>= 1.2.5)
29
+ github_api (= 0.10.1)
30
+ highline (>= 1.6.15)
31
+ nokogiri (= 1.5.10)
32
+ rake
33
+ rdoc
34
+ json (1.8.0)
35
+ jwt (0.1.8)
36
+ multi_json (>= 1.5)
37
+ method_source (0.8.2)
38
+ multi_json (1.7.9)
39
+ multi_xml (0.5.5)
40
+ multipart-post (1.2.0)
41
+ nokogiri (1.5.10)
42
+ oauth2 (0.9.2)
43
+ faraday (~> 0.8)
44
+ httpauth (~> 0.2)
45
+ jwt (~> 0.1.4)
46
+ multi_json (~> 1.0)
47
+ multi_xml (~> 0.5)
48
+ rack (~> 1.2)
49
+ pry (0.9.12.2)
50
+ coderay (~> 1.0.5)
51
+ method_source (~> 0.8)
52
+ slop (~> 3.4)
53
+ rack (1.5.2)
54
+ rake (10.1.0)
55
+ rdoc (4.0.1)
56
+ json (~> 1.4)
57
+ rspec (2.14.1)
58
+ rspec-core (~> 2.14.0)
59
+ rspec-expectations (~> 2.14.0)
60
+ rspec-mocks (~> 2.14.0)
61
+ rspec-core (2.14.5)
62
+ rspec-expectations (2.14.2)
63
+ diff-lcs (>= 1.1.3, < 2.0)
64
+ rspec-mocks (2.14.3)
65
+ slop (3.4.6)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ activesupport (~> 3.2.14)
72
+ bundler
73
+ jeweler (~> 1.8.4)
74
+ pry
75
+ rspec (~> 2.14.1)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Dan Nguyen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # AggtiveRecord
2
+
3
+ **NOTE:** This gem is not really functional yet. I just like to commit things good and early.
4
+
5
+ An attempt to formalize the description of common aggregations across timestamped records.
6
+
7
+ I wrote this gem because I found myself writing, over and over again, these kind of ActiveRecord scope:
8
+
9
+
10
+ To get the number of messages sent in the last two weeks:
11
+
12
+ Message.where('sent_at >= ?', 14.days.ago).count
13
+
14
+ To get the *rate* of messages *per week*:
15
+
16
+ Message.count * SECONDS_PER_WEEK / (Message.order('sent_at ASC').last - Message.order('sent_at ASC').first)
17
+
18
+ Or how about the rate *per week* for messages sent in the *past month* (because it's interesting to know if someone has sent more messages recently)?
19
+
20
+ Message.where('sent_at >= ?', 1.month.ago) * SECONDS_PER_WEEK / (Time.now - 1.month.ago)
21
+
22
+ This is easily enough to formalize in one application, but I figured maybe it'd be useful to have a convention for every app that I ever do that tries to aggregate records by time.
23
+
24
+ ### Let's make some meta-magic
25
+
26
+ So for the first example, for counting messages in the past two weeks, something like:
27
+
28
+ Message.count_past_14_days
29
+
30
+ OK, admittedly that's not much nicer than the standard ActiveRecord chain. But for the second example, it could be simplified to this:
31
+
32
+ Message.rate_per_week
33
+
34
+ And for the third example:
35
+
36
+ Message.rate_during_past_month_per_week
37
+
38
+
39
+
40
+ **AggtiveRecord** is an extension that can be applied to ActiveRecord. Maybe you'll find it useful, maybe not.
41
+
42
+
43
+ This gem takes some inspiration from the highly useful groupdate.
44
+
45
+
46
+ Message.count_during_past_year_per_day
47
+
48
+
49
+ Message.group_by_day.where('sent_at >= ?' 1.year.ago)
50
+
51
+
52
+
53
+
54
+
55
+
56
+ ### Interaction with :has_many
57
+
58
+ *Obviously, this is just speculative*
59
+
60
+ @person.rate_of_messages_per_day
61
+
62
+ @person.messages_count_during_past_year_per_day
63
+
64
+ @person.rate_word_count_from_messages_during_past_year_per_day
65
+
66
+ @person.count_messages_during_past_year_per_day
67
+ @person.messages_count_during_past_year_per_day
68
+
69
+ Message.count_during_past_year
70
+
71
+
72
+ A standard verbose call:
73
+
74
+ @person.sum_word_count_from_messages_during_past_year_per_day
75
+
76
+
77
+ *Under the hood query:*
78
+
79
+ @person.egg.sum(:word_count).
80
+ from(:messages).
81
+ during(:past_year).
82
+ per(:day)
83
+
84
+ *Which translates to this in ActiveRecord:*
85
+
86
+ @person.messages.
87
+ where('sent_at >= ?', 1.year.ago).
88
+ group('DAY(sent_at)').
89
+ sum(:word_count)
90
+
91
+
92
+ ### Result types
93
+
94
+
95
+
96
+
97
+ ### Scalars and arrays
98
+
99
+
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "aggtive_record"
17
+ gem.homepage = "http://github.com/dannguyen/aggtive_record"
18
+ gem.license = "MIT"
19
+ gem.summary = %Q{A convoluted way to describe aggregations of ActiveRecords over a datetime attribute}
20
+ gem.description = %Q{This is not even remotely finished or even started on. Please don't download.}
21
+ gem.email = "dansonguyen@gmail.com"
22
+ gem.authors = ["Dan Nguyen"]
23
+ # dependencies defined in Gemfile
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rspec/core'
28
+ require 'rspec/core/rake_task'
29
+ RSpec::Core::RakeTask.new(:spec) do |spec|
30
+ spec.pattern = FileList['spec/**/*_spec.rb']
31
+ end
32
+
33
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
34
+ spec.pattern = 'spec/**/*_spec.rb'
35
+ spec.rcov = true
36
+ end
37
+
38
+ task :default => :spec
39
+
40
+ require 'rdoc/task'
41
+ Rake::RDocTask.new do |rdoc|
42
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "aggtive_record #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,7 @@
1
+ module AggtiveRecord
2
+ module Adapter
3
+ module Mysql
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module AggtiveRecord
2
+ module Adapter
3
+
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ module AggtiveRecord
2
+ module Aggable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :datetime_attribute
7
+ end
8
+
9
+ module ClassMethods
10
+ def attr_timestamp(attname)
11
+ raise ArgumentError unless is_a_datetime?(att)
12
+ self.datetime_attribute = attname
13
+ end
14
+
15
+ def is_a_datetime?(attname)
16
+ self.columns.select{|r| r.name == attname.to_s && r.type =~ /date|time/}.first.present?
17
+ end
18
+ end
19
+
20
+
21
+ def method_missing
22
+
23
+ end
24
+
25
+ def respond_to
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ module AggtiveRecord
2
+ class Egg
3
+
4
+ attr_reader :timezone, :collation, :time_bucket, :time_span, :collective_method
5
+
6
+ def self.valid_format?
7
+
8
+ end
9
+
10
+ def self.parse(str)
11
+
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module AggtiveRecord
2
+ module Scopes
3
+ module Collation
4
+ extend ActiveRecord::Concern
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module AggtiveRecord
2
+ module Scopes
3
+ module TimeBucket
4
+ extend ActiveRecord::Concern
5
+
6
+ mattr_reader :prefix
7
+ self.prefix = 'by'
8
+
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,22 @@
1
+ module AggtiveRecord
2
+ module Scopes
3
+ module TimeSpan
4
+
5
+ extend ActiveRecord::Concern
6
+
7
+ # where("#{}" => 1.year.ago..Time.now)
8
+ #past_year
9
+
10
+ mattr_reader :prefix
11
+ self.prefix = 'during'
12
+
13
+ =begin
14
+
15
+
16
+
17
+ =end
18
+
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module AggtiveRecord
2
+ module Scopes
3
+
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ module AggtiveRecord
2
+ module Time
3
+
4
+ UNIFORM_PERIODS = [:week, :day, :hour, :minute, :second]
5
+ CALENDAR_PERIODS = [:year, :month]
6
+
7
+ PERIODS = CALENDAR_PERIODS + UNIFORM_PERIODS
8
+
9
+ TIMESTAMP_QUANTITIES = [
10
+ year: {
11
+ str: '%Y'
12
+ },
13
+ month:{
14
+ str: '%m'
15
+ },
16
+ day: {
17
+ str: '%d'
18
+ }
19
+
20
+ hour: {
21
+ str: '%H'
22
+ },
23
+
24
+ minute: {
25
+ str: '%i'
26
+ },
27
+
28
+ second: {
29
+ str: '%S'
30
+ }
31
+
32
+ ]
33
+
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_support'
2
+
3
+ module AggtiveRecord
4
+ end
5
+
6
+
7
+
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ require 'hashie'
4
+
5
+ class AnObject < ActiveRecord::Base
6
+ include AggtiveRecord
7
+ attr_accessor :star_count, :time_posted
8
+ attr_timestamp :time_posted
9
+ end
10
+
11
+ describe 'possible methods', skip: true do
12
+
13
+ context 'singular scopes' do
14
+
15
+ it '#past_14_days'
16
+ it '#past_year'
17
+ it '#past_20_years'
18
+ it 'should not have #past_years'
19
+
20
+ it 'by_year'
21
+ it 'by_minute'
22
+ it 'not by_microsecond'
23
+ end
24
+
25
+ end
26
+
27
+ describe 'AnObject with AggtiveRecord mixedin', skip: true do
28
+
29
+ context 'class setup' do
30
+ it 'should have @@timestamp_attribute set to :time_posted'
31
+ end
32
+
33
+ context 'scopes' do
34
+
35
+ describe '#during' do
36
+
37
+ describe '.past_[time frame]' do
38
+ it 'should scope within past_14_days'
39
+ it 'should scope within past_year'
40
+ end
41
+
42
+ describe '.[epoch_name]' do
43
+ it 'should scope #overall'
44
+ it 'should scope year(1995)' do
45
+ pending %q{
46
+ er...what's the <s></s>yntax for that...?
47
+
48
+
49
+ }
50
+ end
51
+ end
52
+
53
+ context 'AnObject scoped collection' do
54
+ describe '[agg]_of methods' do
55
+ it 'should have #count_of '
56
+ end
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'aggtive_record'
5
+
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+
12
+
13
+
14
+ RSpec.configure do |config|
15
+ config.color_enabled = true
16
+ config.tty = true
17
+ config.formatter = :documentation # :progress, :html, :textmate
18
+
19
+ config.before(:each) do
20
+ end
21
+
22
+ config.after(:each) do
23
+ end
24
+ end
25
+
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe "AggtiveRecord::Aggable" do
4
+
5
+ context 'class methods' do
6
+
7
+ describe '#is_a_datetime?' do
8
+ it 'returns true if named column exists and is date/time' do
9
+ @klass.is_a_datetime?
10
+ end
11
+
12
+ it 'returns false if named column does not exist'
13
+ it 'returns false if named column is not date/time'
14
+ end
15
+
16
+
17
+ describe '#attr_datetime' do
18
+ it 'sets @@datetime_attribute'
19
+ it 'raises ArgumentError if not a valid datetime column'
20
+ end
21
+
22
+ end
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aggtive_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Nguyen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.14
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.14
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.14.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.14.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: This is not even remotely finished or even started on. Please don't download.
84
+ email: dansonguyen@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.md
90
+ files:
91
+ - .document
92
+ - .rspec
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - VERSION
99
+ - lib/aggtive_record.rb
100
+ - lib/aggtive_record/adapter.rb
101
+ - lib/aggtive_record/adapter/mysql.rb
102
+ - lib/aggtive_record/aggable.rb
103
+ - lib/aggtive_record/memo.rb
104
+ - lib/aggtive_record/scopes.rb
105
+ - lib/aggtive_record/scopes/collation.rb
106
+ - lib/aggtive_record/scopes/time_bucket.rb
107
+ - lib/aggtive_record/scopes/time_span.rb
108
+ - lib/aggtive_record/time.rb
109
+ - spec/functional/basic_object_run_spec.rb
110
+ - spec/spec_helper.rb
111
+ - spec/unit/aggable.rb
112
+ homepage: http://github.com/dannguyen/aggtive_record
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.0.5
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: A convoluted way to describe aggregations of ActiveRecords over a datetime
136
+ attribute
137
+ test_files: []