timescopes 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/timescopes/rails/active_record_extension.rb +31 -0
- data/lib/timescopes/scopes.rb +38 -0
- data/lib/timescopes.rb +3 -34
- data/timescopes.gemspec +68 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33459f5097d5d9f3b5c4f180c3ddf9ebf259544a
|
4
|
+
data.tar.gz: a582cf895357b9ff22e43d5d70b733376119de96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc86d6dc4b3eee7ac2c62fb5e53f10484804c22202dc2c0c3cd1eef785e971cbb3fb64d0bf22ed6dde3965a837f5bcca11396aaa05dd08c798e9b00cb143dd4d
|
7
|
+
data.tar.gz: 0079a6bb100bd3f15115422a8c6ebf7d2c11453ed7fa21c8cf3b168a04c7479f86081f4cccea955c8ed5ca042721e6058c02fb5c8316c842893ce77879f39ebc
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module TimeScopes
|
2
|
+
module Rails
|
3
|
+
module ActiveRecordExtension
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def timescoped!(opts={})
|
8
|
+
default_scopes = [:daily, :weekly, :monthly]
|
9
|
+
|
10
|
+
field = opts[:field] || "created_at"
|
11
|
+
scopes = if opts[:only]
|
12
|
+
Array(opts[:only]).map(&:to_sym) & default_scopes
|
13
|
+
else
|
14
|
+
default_scopes
|
15
|
+
end
|
16
|
+
|
17
|
+
scopes.each do |scope|
|
18
|
+
eval %%
|
19
|
+
def #{scope}(start=nil)
|
20
|
+
start, finish = TimeScopes.#{scope}(start)
|
21
|
+
where('#{field} >= ? AND #{field} < ?', start.utc, finish.utc)
|
22
|
+
end
|
23
|
+
%
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
ActiveRecord::Base.send(:include, TimeScopes::Rails::ActiveRecordExtension)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'chronic'
|
2
|
+
|
3
|
+
module TimeScopes
|
4
|
+
class << self
|
5
|
+
def daily(start=nil)
|
6
|
+
start ||= Time.now
|
7
|
+
[ Chronic.parse(start_of_day(start.utc)),
|
8
|
+
Chronic.parse(start_of_day(1.day.since(start.utc))) ]
|
9
|
+
end
|
10
|
+
|
11
|
+
def weekly(start=nil)
|
12
|
+
start ||= Time.now
|
13
|
+
start = start.utc
|
14
|
+
start = start - 1.day while !start.monday?
|
15
|
+
[ Chronic.parse(start_of_day(start.utc)),
|
16
|
+
Chronic.parse(start_of_day(1.week.since(start.utc))) ]
|
17
|
+
end
|
18
|
+
|
19
|
+
def monthly(start=nil)
|
20
|
+
start ||= Time.now
|
21
|
+
[ Chronic.parse(start_of_month(start.utc)),
|
22
|
+
Chronic.parse(start_of_month(1.month.since(start.utc))) ]
|
23
|
+
end
|
24
|
+
|
25
|
+
def overall(start=nil)
|
26
|
+
[ Chronic.parse('2000-01-01 00:00:00'),
|
27
|
+
Chronic.parse(start_of_day(1.day.from_now.utc)) ]
|
28
|
+
end
|
29
|
+
|
30
|
+
def start_of_day(time)
|
31
|
+
time.strftime("%Y-%m-%d 00:00:00Z")
|
32
|
+
end
|
33
|
+
|
34
|
+
def start_of_month(time)
|
35
|
+
time.strftime("%Y-%m-01 00:00:00Z")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/timescopes.rb
CHANGED
@@ -1,36 +1,5 @@
|
|
1
|
-
|
2
|
-
class << self
|
3
|
-
def daily(start=nil)
|
4
|
-
start ||= Time.now
|
5
|
-
[ Chronic.parse(start_of_day(start.utc)),
|
6
|
-
Chronic.parse(start_of_day(1.day.since(start.utc))) ]
|
7
|
-
end
|
1
|
+
require_relative "./timescopes/scopes"
|
8
2
|
|
9
|
-
|
10
|
-
|
11
|
-
start = start.utc
|
12
|
-
start = start - 1.day while !start.monday?
|
13
|
-
[ Chronic.parse(start_of_day(start.utc)),
|
14
|
-
Chronic.parse(start_of_day(1.week.since(start.utc))) ]
|
15
|
-
end
|
16
|
-
|
17
|
-
def monthly(start=nil)
|
18
|
-
start ||= Time.now
|
19
|
-
[ Chronic.parse(start_of_month(start.utc)),
|
20
|
-
Chronic.parse(start_of_month(1.month.since(start.utc))) ]
|
21
|
-
end
|
22
|
-
|
23
|
-
def overall(start=nil)
|
24
|
-
[ Chronic.parse('2000-01-01 00:00:00'),
|
25
|
-
Chronic.parse(start_of_day(1.day.from_now.utc)) ]
|
26
|
-
end
|
27
|
-
|
28
|
-
def start_of_day(time)
|
29
|
-
time.strftime("%Y-%m-%d 00:00:00Z")
|
30
|
-
end
|
31
|
-
|
32
|
-
def start_of_month(time)
|
33
|
-
time.strftime("%Y-%m-01 00:00:00Z")
|
34
|
-
end
|
35
|
-
end
|
3
|
+
if defined?(ActiveRecord::Base)
|
4
|
+
require_relative "./timescopes/rails/active_record_extension"
|
36
5
|
end
|
data/timescopes.gemspec
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: timescopes 0.1.1 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "timescopes"
|
9
|
+
s.version = "0.1.1"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
s.authors = ["Ian Hunter"]
|
14
|
+
s.date = "2015-10-31"
|
15
|
+
s.description = ""
|
16
|
+
s.email = "ianhunter@gmail.com"
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.md"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.md",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"lib/timescopes.rb",
|
30
|
+
"lib/timescopes/rails/active_record_extension.rb",
|
31
|
+
"lib/timescopes/scopes.rb",
|
32
|
+
"test/helper.rb",
|
33
|
+
"test/test_timescopes.rb",
|
34
|
+
"timescopes.gemspec"
|
35
|
+
]
|
36
|
+
s.homepage = "http://github.com/ian/timescopes"
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.rubygems_version = "2.4.6"
|
39
|
+
s.summary = "General time scoping"
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
s.specification_version = 4
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_runtime_dependency(%q<chronic>, [">= 0"])
|
46
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
47
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
48
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
49
|
+
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
50
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<chronic>, [">= 0"])
|
53
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
54
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
55
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
56
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
57
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<chronic>, [">= 0"])
|
61
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
62
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
63
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
64
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
65
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timescopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian Hunter
|
@@ -110,8 +110,11 @@ files:
|
|
110
110
|
- Rakefile
|
111
111
|
- VERSION
|
112
112
|
- lib/timescopes.rb
|
113
|
+
- lib/timescopes/rails/active_record_extension.rb
|
114
|
+
- lib/timescopes/scopes.rb
|
113
115
|
- test/helper.rb
|
114
116
|
- test/test_timescopes.rb
|
117
|
+
- timescopes.gemspec
|
115
118
|
homepage: http://github.com/ian/timescopes
|
116
119
|
licenses:
|
117
120
|
- MIT
|