by_star 4.0.0 → 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/mysql.yml +92 -92
- data/.github/workflows/postgresql.yml +99 -99
- data/.gitignore +6 -6
- data/.travis.yml +92 -92
- data/CHANGELOG.md +63 -59
- data/Gemfile +18 -18
- data/MIT-LICENSE +20 -20
- data/README.md +616 -616
- data/Rakefile +18 -18
- data/UPGRADING +4 -4
- data/by_star.gemspec +34 -34
- data/cleaner.rb +24 -24
- data/lib/by_star/base.rb +69 -69
- data/lib/by_star/between.rb +185 -185
- data/lib/by_star/directional.rb +35 -35
- data/lib/by_star/kernel/date.rb +41 -41
- data/lib/by_star/kernel/in_time_zone.rb +20 -20
- data/lib/by_star/kernel/time.rb +41 -41
- data/lib/by_star/normalization.rb +156 -156
- data/lib/by_star/orm/active_record/by_star.rb +75 -75
- data/lib/by_star/orm/mongoid/by_star.rb +90 -90
- data/lib/by_star/orm/mongoid/reorder.rb +23 -23
- data/lib/by_star/version.rb +3 -3
- data/lib/by_star.rb +18 -18
- data/spec/database.yml +15 -15
- data/spec/fixtures/active_record/models.rb +12 -12
- data/spec/fixtures/active_record/schema.rb +19 -19
- data/spec/fixtures/mongoid/models.rb +31 -31
- data/spec/fixtures/shared/seeds.rb +36 -36
- data/spec/gemfiles/Gemfile.rails +5 -5
- data/spec/gemfiles/Gemfile.rails32 +7 -7
- data/spec/gemfiles/Gemfile.rails40 +7 -7
- data/spec/gemfiles/Gemfile.rails41 +7 -7
- data/spec/gemfiles/Gemfile.rails42 +7 -7
- data/spec/gemfiles/Gemfile.rails50 +7 -7
- data/spec/gemfiles/Gemfile.rails51 +7 -7
- data/spec/gemfiles/Gemfile.rails52 +7 -7
- data/spec/gemfiles/Gemfile.rails60 +7 -7
- data/spec/gemfiles/Gemfile.rails61 +7 -7
- data/spec/integration/active_record/active_record_spec.rb +41 -41
- data/spec/integration/mongoid/mongoid_spec.rb +39 -39
- data/spec/integration/shared/at_time.rb +53 -53
- data/spec/integration/shared/between_dates.rb +99 -99
- data/spec/integration/shared/between_times.rb +99 -99
- data/spec/integration/shared/by_calendar_month.rb +55 -55
- data/spec/integration/shared/by_cweek.rb +54 -54
- data/spec/integration/shared/by_day.rb +120 -120
- data/spec/integration/shared/by_direction.rb +126 -126
- data/spec/integration/shared/by_fortnight.rb +48 -48
- data/spec/integration/shared/by_month.rb +50 -50
- data/spec/integration/shared/by_quarter.rb +49 -49
- data/spec/integration/shared/by_week.rb +54 -54
- data/spec/integration/shared/by_weekend.rb +49 -49
- data/spec/integration/shared/by_year.rb +48 -48
- data/spec/integration/shared/index_scope_parameter.rb +111 -111
- data/spec/integration/shared/offset_parameter.rb +32 -32
- data/spec/integration/shared/order_parameter.rb +36 -36
- data/spec/integration/shared/relative.rb +174 -174
- data/spec/spec_helper.rb +33 -33
- data/spec/unit/kernel_date_spec.rb +113 -113
- data/spec/unit/kernel_time_spec.rb +57 -57
- data/spec/unit/normalization_spec.rb +384 -384
- data/tmp/.gitignore +1 -1
- metadata +3 -3
data/Rakefile
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
require 'bundler'
|
2
|
-
require 'rspec/core/rake_task'
|
3
|
-
|
4
|
-
Bundler::GemHelper.install_tasks
|
5
|
-
RSpec::Core::RakeTask.new(:spec)
|
6
|
-
|
7
|
-
def orm_test(orm)
|
8
|
-
RSpec::Core::RakeTask.new(orm) do |task|
|
9
|
-
task.pattern = "./spec/{unit,integration/#{orm}}/{,/*/**}/*_spec.rb"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
namespace :spec do
|
14
|
-
orm_test 'active_record'
|
15
|
-
orm_test 'mongoid'
|
16
|
-
end
|
17
|
-
|
18
|
-
task default: :spec
|
1
|
+
require 'bundler'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
def orm_test(orm)
|
8
|
+
RSpec::Core::RakeTask.new(orm) do |task|
|
9
|
+
task.pattern = "./spec/{unit,integration/#{orm}}/{,/*/**}/*_spec.rb"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
namespace :spec do
|
14
|
+
orm_test 'active_record'
|
15
|
+
orm_test 'mongoid'
|
16
|
+
end
|
17
|
+
|
18
|
+
task default: :spec
|
data/UPGRADING
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Upgrading ByStar
|
2
|
-
----------------
|
3
|
-
|
4
|
-
* As of version 4.0.0, ByStar changes the way it handles `Date` arg to the `#between_times` method. If a `Date` is given as the second (end) arg, the query will use `Date.end_of_day` to include all time values which fall inside that date.
|
1
|
+
Upgrading ByStar
|
2
|
+
----------------
|
3
|
+
|
4
|
+
* As of version 4.0.0, ByStar changes the way it handles `Date` arg to the `#between_times` method. If a `Date` is given as the second (end) arg, the query will use `Date.end_of_day` to include all time values which fall inside that date.
|
data/by_star.gemspec
CHANGED
@@ -1,34 +1,34 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require File.expand_path("../lib/by_star/version", __FILE__)
|
3
|
-
|
4
|
-
Gem::Specification.new do |s|
|
5
|
-
s.name = "by_star"
|
6
|
-
s.version = ByStar::VERSION
|
7
|
-
s.authors = ["Ryan Bigg", "Johnny Shields"]
|
8
|
-
s.email = ["radarlistener@gmail.com"]
|
9
|
-
s.homepage = "http://github.com/radar/by_star"
|
10
|
-
s.summary = "ActiveRecord and Mongoid extension for easier date scopes and time ranges"
|
11
|
-
s.description = "ActiveRecord and Mongoid extension for easier date scopes and time ranges"
|
12
|
-
|
13
|
-
s.required_ruby_version = '>= 2.0.0'
|
14
|
-
|
15
|
-
s.post_install_message = File.read('UPGRADING') if File.
|
16
|
-
|
17
|
-
s.add_dependency "activesupport", ">= 3.2.0"
|
18
|
-
|
19
|
-
s.add_development_dependency "chronic"
|
20
|
-
s.add_development_dependency "bundler"
|
21
|
-
s.add_development_dependency "sqlite3"
|
22
|
-
s.add_development_dependency "activerecord"
|
23
|
-
s.add_development_dependency "mongoid"
|
24
|
-
s.add_development_dependency "pg"
|
25
|
-
s.add_development_dependency "mysql2"
|
26
|
-
s.add_development_dependency "rspec-rails", "~> 3.1"
|
27
|
-
s.add_development_dependency "timecop", "~> 0.3"
|
28
|
-
s.add_development_dependency "pry"
|
29
|
-
|
30
|
-
s.files = `git ls-files`.split($/)
|
31
|
-
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
32
|
-
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
33
|
-
s.require_paths = ['lib']
|
34
|
-
end
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/by_star/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "by_star"
|
6
|
+
s.version = ByStar::VERSION
|
7
|
+
s.authors = ["Ryan Bigg", "Johnny Shields"]
|
8
|
+
s.email = ["radarlistener@gmail.com"]
|
9
|
+
s.homepage = "http://github.com/radar/by_star"
|
10
|
+
s.summary = "ActiveRecord and Mongoid extension for easier date scopes and time ranges"
|
11
|
+
s.description = "ActiveRecord and Mongoid extension for easier date scopes and time ranges"
|
12
|
+
|
13
|
+
s.required_ruby_version = '>= 2.0.0'
|
14
|
+
|
15
|
+
s.post_install_message = File.read('UPGRADING') if File.exist?('UPGRADING')
|
16
|
+
|
17
|
+
s.add_dependency "activesupport", ">= 3.2.0"
|
18
|
+
|
19
|
+
s.add_development_dependency "chronic"
|
20
|
+
s.add_development_dependency "bundler"
|
21
|
+
s.add_development_dependency "sqlite3"
|
22
|
+
s.add_development_dependency "activerecord"
|
23
|
+
s.add_development_dependency "mongoid"
|
24
|
+
s.add_development_dependency "pg"
|
25
|
+
s.add_development_dependency "mysql2"
|
26
|
+
s.add_development_dependency "rspec-rails", "~> 3.1"
|
27
|
+
s.add_development_dependency "timecop", "~> 0.3"
|
28
|
+
s.add_development_dependency "pry"
|
29
|
+
|
30
|
+
s.files = `git ls-files`.split($/)
|
31
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
32
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
33
|
+
s.require_paths = ['lib']
|
34
|
+
end
|
data/cleaner.rb
CHANGED
@@ -1,25 +1,25 @@
|
|
1
|
-
files = Dir["**/*"]
|
2
|
-
ignored_files = [
|
3
|
-
/log\/.*/,
|
4
|
-
]
|
5
|
-
|
6
|
-
files.delete_if do |file|
|
7
|
-
if File.directory?(file)
|
8
|
-
true
|
9
|
-
else
|
10
|
-
ignored_files.any? do |condition|
|
11
|
-
if condition.is_a?(String)
|
12
|
-
file == condition
|
13
|
-
else
|
14
|
-
condition.match(file)
|
15
|
-
end
|
16
|
-
end || false
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
for file in files - ignored_files
|
21
|
-
if File.file?(file)
|
22
|
-
lines = File.readlines(file).map { |line| line.gsub(/^\s+$/, "\n") }
|
23
|
-
File.open(file, "w+") { |f| f.write(lines.join) }
|
24
|
-
end
|
1
|
+
files = Dir["**/*"]
|
2
|
+
ignored_files = [
|
3
|
+
/log\/.*/,
|
4
|
+
]
|
5
|
+
|
6
|
+
files.delete_if do |file|
|
7
|
+
if File.directory?(file)
|
8
|
+
true
|
9
|
+
else
|
10
|
+
ignored_files.any? do |condition|
|
11
|
+
if condition.is_a?(String)
|
12
|
+
file == condition
|
13
|
+
else
|
14
|
+
condition.match(file)
|
15
|
+
end
|
16
|
+
end || false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
for file in files - ignored_files
|
21
|
+
if File.file?(file)
|
22
|
+
lines = File.readlines(file).map { |line| line.gsub(/^\s+$/, "\n") }
|
23
|
+
File.open(file, "w+") { |f| f.write(lines.join) }
|
24
|
+
end
|
25
25
|
end
|
data/lib/by_star/base.rb
CHANGED
@@ -1,69 +1,69 @@
|
|
1
|
-
module ByStar
|
2
|
-
|
3
|
-
module Base
|
4
|
-
|
5
|
-
include ByStar::Between
|
6
|
-
include ByStar::Directional
|
7
|
-
|
8
|
-
def by_star_field(*args)
|
9
|
-
options = args.extract_options!
|
10
|
-
@by_star_start_field ||= args[0]
|
11
|
-
@by_star_end_field ||= args[1]
|
12
|
-
@by_star_offset ||= options[:offset]
|
13
|
-
@by_star_scope ||= options[:scope]
|
14
|
-
@by_star_index_scope ||= options[:index_scope]
|
15
|
-
end
|
16
|
-
|
17
|
-
def by_star_offset(options = {})
|
18
|
-
(options[:offset] || @by_star_offset || 0).seconds
|
19
|
-
end
|
20
|
-
|
21
|
-
def by_star_start_field(options={})
|
22
|
-
field = options[:field] ||
|
23
|
-
options[:start_field] ||
|
24
|
-
@by_star_start_field ||
|
25
|
-
by_star_default_field
|
26
|
-
field.to_s
|
27
|
-
end
|
28
|
-
|
29
|
-
def by_star_end_field(options={})
|
30
|
-
field = options[:field] ||
|
31
|
-
options[:end_field] ||
|
32
|
-
@by_star_end_field ||
|
33
|
-
by_star_start_field
|
34
|
-
field.to_s
|
35
|
-
end
|
36
|
-
|
37
|
-
protected
|
38
|
-
|
39
|
-
# Wrapper function which extracts time and options for each by_star query.
|
40
|
-
# Note the following syntax examples are valid:
|
41
|
-
#
|
42
|
-
# Post.by_month # defaults to current time
|
43
|
-
# Post.by_month(2, year: 2004) # February, 2004
|
44
|
-
# Post.by_month(Time.now)
|
45
|
-
# Post.by_month(Time.now, field: "published_at")
|
46
|
-
# Post.by_month(field: "published_at")
|
47
|
-
#
|
48
|
-
def with_by_star_options(*args, &block)
|
49
|
-
options = args.extract_options!.symbolize_keys!
|
50
|
-
time = args.first || Time.zone.now
|
51
|
-
block.call(time, options)
|
52
|
-
end
|
53
|
-
|
54
|
-
def by_star_eval_index_scope(start_time, end_time, options)
|
55
|
-
value = options[:index_scope] || @by_star_index_scope
|
56
|
-
value = value.call(start_time, end_time, options) if value.is_a?(Proc)
|
57
|
-
case value
|
58
|
-
when nil, false then nil
|
59
|
-
when Time, DateTime, Date then value.in_time_zone
|
60
|
-
when ActiveSupport::Duration then start_time - value
|
61
|
-
when Numeric then start_time - value.seconds
|
62
|
-
when :beginning_of_day
|
63
|
-
offset = options[:offset] || 0
|
64
|
-
(start_time - offset).beginning_of_day + offset
|
65
|
-
else raise 'ByStar :index_scope option value is not a supported type.'
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
1
|
+
module ByStar
|
2
|
+
|
3
|
+
module Base
|
4
|
+
|
5
|
+
include ByStar::Between
|
6
|
+
include ByStar::Directional
|
7
|
+
|
8
|
+
def by_star_field(*args)
|
9
|
+
options = args.extract_options!
|
10
|
+
@by_star_start_field ||= args[0]
|
11
|
+
@by_star_end_field ||= args[1]
|
12
|
+
@by_star_offset ||= options[:offset]
|
13
|
+
@by_star_scope ||= options[:scope]
|
14
|
+
@by_star_index_scope ||= options[:index_scope]
|
15
|
+
end
|
16
|
+
|
17
|
+
def by_star_offset(options = {})
|
18
|
+
(options[:offset] || @by_star_offset || 0).seconds
|
19
|
+
end
|
20
|
+
|
21
|
+
def by_star_start_field(options={})
|
22
|
+
field = options[:field] ||
|
23
|
+
options[:start_field] ||
|
24
|
+
@by_star_start_field ||
|
25
|
+
by_star_default_field
|
26
|
+
field.to_s
|
27
|
+
end
|
28
|
+
|
29
|
+
def by_star_end_field(options={})
|
30
|
+
field = options[:field] ||
|
31
|
+
options[:end_field] ||
|
32
|
+
@by_star_end_field ||
|
33
|
+
by_star_start_field
|
34
|
+
field.to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
# Wrapper function which extracts time and options for each by_star query.
|
40
|
+
# Note the following syntax examples are valid:
|
41
|
+
#
|
42
|
+
# Post.by_month # defaults to current time
|
43
|
+
# Post.by_month(2, year: 2004) # February, 2004
|
44
|
+
# Post.by_month(Time.now)
|
45
|
+
# Post.by_month(Time.now, field: "published_at")
|
46
|
+
# Post.by_month(field: "published_at")
|
47
|
+
#
|
48
|
+
def with_by_star_options(*args, &block)
|
49
|
+
options = args.extract_options!.symbolize_keys!
|
50
|
+
time = args.first || Time.zone.now
|
51
|
+
block.call(time, options)
|
52
|
+
end
|
53
|
+
|
54
|
+
def by_star_eval_index_scope(start_time, end_time, options)
|
55
|
+
value = options[:index_scope] || @by_star_index_scope
|
56
|
+
value = value.call(start_time, end_time, options) if value.is_a?(Proc)
|
57
|
+
case value
|
58
|
+
when nil, false then nil
|
59
|
+
when Time, DateTime, Date then value.in_time_zone
|
60
|
+
when ActiveSupport::Duration then start_time - value
|
61
|
+
when Numeric then start_time - value.seconds
|
62
|
+
when :beginning_of_day
|
63
|
+
offset = options[:offset] || 0
|
64
|
+
(start_time - offset).beginning_of_day + offset
|
65
|
+
else raise 'ByStar :index_scope option value is not a supported type.'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/by_star/between.rb
CHANGED
@@ -1,185 +1,185 @@
|
|
1
|
-
module ByStar
|
2
|
-
|
3
|
-
module Between
|
4
|
-
|
5
|
-
def between_times(*args)
|
6
|
-
options = args.extract_options!.symbolize_keys!
|
7
|
-
|
8
|
-
start_time, end_time = ByStar::Normalization.extract_range(args)
|
9
|
-
offset = options[:offset] || 0
|
10
|
-
|
11
|
-
if start_time.is_a?(Date)
|
12
|
-
start_time = ByStar::Normalization.apply_offset_start(start_time.in_time_zone, offset)
|
13
|
-
elsif start_time
|
14
|
-
start_time += offset.seconds
|
15
|
-
end
|
16
|
-
|
17
|
-
if end_time.is_a?(Date)
|
18
|
-
end_time = ByStar::Normalization.apply_offset_end(end_time.in_time_zone, offset)
|
19
|
-
elsif end_time
|
20
|
-
end_time += offset.seconds
|
21
|
-
end
|
22
|
-
|
23
|
-
start_field = by_star_start_field(options)
|
24
|
-
end_field = by_star_end_field(options)
|
25
|
-
|
26
|
-
scope = self
|
27
|
-
scope = if !start_time && !end_time
|
28
|
-
scope # do nothing
|
29
|
-
elsif !end_time
|
30
|
-
by_star_after_query(scope, start_field, start_time)
|
31
|
-
elsif !start_time
|
32
|
-
by_star_before_query(scope, start_field, end_time)
|
33
|
-
elsif start_field == end_field
|
34
|
-
by_star_point_query(scope, start_field, start_time, end_time)
|
35
|
-
elsif options[:strict]
|
36
|
-
by_star_span_strict_query(scope, start_field, end_field, start_time, end_time)
|
37
|
-
else
|
38
|
-
by_star_span_loose_query(scope, start_field, end_field, start_time, end_time, options)
|
39
|
-
end
|
40
|
-
|
41
|
-
scope = by_star_order(scope, options[:order]) if options[:order]
|
42
|
-
scope
|
43
|
-
end
|
44
|
-
|
45
|
-
def between_dates(*args)
|
46
|
-
options = args.extract_options!
|
47
|
-
start_date, end_date = ByStar::Normalization.extract_range(args)
|
48
|
-
start_date = ByStar::Normalization.date(start_date)
|
49
|
-
end_date = ByStar::Normalization.date(end_date)
|
50
|
-
between_times(start_date, end_date, options)
|
51
|
-
end
|
52
|
-
|
53
|
-
def at_time(*args)
|
54
|
-
with_by_star_options(*args) do |time, options|
|
55
|
-
start_field = by_star_start_field(options)
|
56
|
-
end_field = by_star_end_field(options)
|
57
|
-
|
58
|
-
scope = self
|
59
|
-
scope = if start_field == end_field
|
60
|
-
by_star_point_overlap_query(scope, start_field, time)
|
61
|
-
else
|
62
|
-
by_star_span_overlap_query(scope, start_field, end_field, time, options)
|
63
|
-
end
|
64
|
-
scope = by_star_order(scope, options[:order]) if options[:order]
|
65
|
-
scope
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def by_day(*args)
|
70
|
-
with_by_star_options(*args) do |time, options|
|
71
|
-
date = ByStar::Normalization.date(time)
|
72
|
-
between_dates(date, date, options)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def by_week(*args)
|
77
|
-
with_by_star_options(*args) do |time, options|
|
78
|
-
date = ByStar::Normalization.week(time, options)
|
79
|
-
start_day = Array(options[:start_day])
|
80
|
-
between_dates(date.beginning_of_week(*start_day), date.end_of_week(*start_day), options)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def by_cweek(*args)
|
85
|
-
with_by_star_options(*args) do |time, options|
|
86
|
-
by_week(ByStar::Normalization.cweek(time, options), options)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def by_weekend(*args)
|
91
|
-
with_by_star_options(*args) do |time, options|
|
92
|
-
date = ByStar::Normalization.week(time, options)
|
93
|
-
between_dates(date.beginning_of_weekend, date.end_of_weekend, options)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def by_fortnight(*args)
|
98
|
-
with_by_star_options(*args) do |time, options|
|
99
|
-
date = ByStar::Normalization.fortnight(time, options)
|
100
|
-
between_dates(date.beginning_of_fortnight, date.end_of_fortnight, options)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
def by_month(*args)
|
105
|
-
with_by_star_options(*args) do |time, options|
|
106
|
-
date = ByStar::Normalization.month(time, options)
|
107
|
-
between_dates(date.beginning_of_month, date.end_of_month, options)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def by_calendar_month(*args)
|
112
|
-
with_by_star_options(*args) do |time, options|
|
113
|
-
date = ByStar::Normalization.month(time, options)
|
114
|
-
start_day = Array(options[:start_day])
|
115
|
-
between_dates(date.beginning_of_calendar_month(*start_day), date.end_of_calendar_month(*start_day), options)
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def by_quarter(*args)
|
120
|
-
with_by_star_options(*args) do |time, options|
|
121
|
-
date = ByStar::Normalization.quarter(time, options)
|
122
|
-
between_dates(date.beginning_of_quarter, date.end_of_quarter, options)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
def by_year(*args)
|
127
|
-
with_by_star_options(*args) do |time, options|
|
128
|
-
date = ByStar::Normalization.year(time, options)
|
129
|
-
between_dates(date.beginning_of_year, date.to_date.end_of_year, options)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def today(options = {})
|
134
|
-
by_day(Date.current, options)
|
135
|
-
end
|
136
|
-
|
137
|
-
def yesterday(options = {})
|
138
|
-
by_day(Date.yesterday, options)
|
139
|
-
end
|
140
|
-
|
141
|
-
def tomorrow(options = {})
|
142
|
-
by_day(Date.tomorrow, options)
|
143
|
-
end
|
144
|
-
|
145
|
-
def past_day(options = {})
|
146
|
-
between_times(Time.current - 1.day, Time.current, options)
|
147
|
-
end
|
148
|
-
|
149
|
-
def past_week(options = {})
|
150
|
-
between_times(Time.current - 1.week, Time.current, options)
|
151
|
-
end
|
152
|
-
|
153
|
-
def past_fortnight(options = {})
|
154
|
-
between_times(Time.current - 2.weeks, Time.current, options)
|
155
|
-
end
|
156
|
-
|
157
|
-
def past_month(options = {})
|
158
|
-
between_times(Time.current - 1.month, Time.current, options)
|
159
|
-
end
|
160
|
-
|
161
|
-
def past_year(options = {})
|
162
|
-
between_times(Time.current - 1.year, Time.current, options)
|
163
|
-
end
|
164
|
-
|
165
|
-
def next_day(options = {})
|
166
|
-
between_times(Time.current, Time.current + 1.day, options)
|
167
|
-
end
|
168
|
-
|
169
|
-
def next_week(options = {})
|
170
|
-
between_times(Time.current, Time.current + 1.week, options)
|
171
|
-
end
|
172
|
-
|
173
|
-
def next_fortnight(options = {})
|
174
|
-
between_times(Time.current, Time.current + 2.weeks, options)
|
175
|
-
end
|
176
|
-
|
177
|
-
def next_month(options = {})
|
178
|
-
between_times(Time.current, Time.current + 1.month, options)
|
179
|
-
end
|
180
|
-
|
181
|
-
def next_year(options = {})
|
182
|
-
between_times(Time.current, Time.current + 1.year, options)
|
183
|
-
end
|
184
|
-
end
|
185
|
-
end
|
1
|
+
module ByStar
|
2
|
+
|
3
|
+
module Between
|
4
|
+
|
5
|
+
def between_times(*args)
|
6
|
+
options = args.extract_options!.symbolize_keys!
|
7
|
+
|
8
|
+
start_time, end_time = ByStar::Normalization.extract_range(args)
|
9
|
+
offset = options[:offset] || 0
|
10
|
+
|
11
|
+
if start_time.is_a?(Date)
|
12
|
+
start_time = ByStar::Normalization.apply_offset_start(start_time.in_time_zone, offset)
|
13
|
+
elsif start_time
|
14
|
+
start_time += offset.seconds
|
15
|
+
end
|
16
|
+
|
17
|
+
if end_time.is_a?(Date)
|
18
|
+
end_time = ByStar::Normalization.apply_offset_end(end_time.in_time_zone, offset)
|
19
|
+
elsif end_time
|
20
|
+
end_time += offset.seconds
|
21
|
+
end
|
22
|
+
|
23
|
+
start_field = by_star_start_field(options)
|
24
|
+
end_field = by_star_end_field(options)
|
25
|
+
|
26
|
+
scope = self
|
27
|
+
scope = if !start_time && !end_time
|
28
|
+
scope # do nothing
|
29
|
+
elsif !end_time
|
30
|
+
by_star_after_query(scope, start_field, start_time)
|
31
|
+
elsif !start_time
|
32
|
+
by_star_before_query(scope, start_field, end_time)
|
33
|
+
elsif start_field == end_field
|
34
|
+
by_star_point_query(scope, start_field, start_time, end_time)
|
35
|
+
elsif options[:strict]
|
36
|
+
by_star_span_strict_query(scope, start_field, end_field, start_time, end_time)
|
37
|
+
else
|
38
|
+
by_star_span_loose_query(scope, start_field, end_field, start_time, end_time, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
scope = by_star_order(scope, options[:order]) if options[:order]
|
42
|
+
scope
|
43
|
+
end
|
44
|
+
|
45
|
+
def between_dates(*args)
|
46
|
+
options = args.extract_options!
|
47
|
+
start_date, end_date = ByStar::Normalization.extract_range(args)
|
48
|
+
start_date = ByStar::Normalization.date(start_date)
|
49
|
+
end_date = ByStar::Normalization.date(end_date)
|
50
|
+
between_times(start_date, end_date, options)
|
51
|
+
end
|
52
|
+
|
53
|
+
def at_time(*args)
|
54
|
+
with_by_star_options(*args) do |time, options|
|
55
|
+
start_field = by_star_start_field(options)
|
56
|
+
end_field = by_star_end_field(options)
|
57
|
+
|
58
|
+
scope = self
|
59
|
+
scope = if start_field == end_field
|
60
|
+
by_star_point_overlap_query(scope, start_field, time)
|
61
|
+
else
|
62
|
+
by_star_span_overlap_query(scope, start_field, end_field, time, options)
|
63
|
+
end
|
64
|
+
scope = by_star_order(scope, options[:order]) if options[:order]
|
65
|
+
scope
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def by_day(*args)
|
70
|
+
with_by_star_options(*args) do |time, options|
|
71
|
+
date = ByStar::Normalization.date(time)
|
72
|
+
between_dates(date, date, options)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def by_week(*args)
|
77
|
+
with_by_star_options(*args) do |time, options|
|
78
|
+
date = ByStar::Normalization.week(time, options)
|
79
|
+
start_day = Array(options[:start_day])
|
80
|
+
between_dates(date.beginning_of_week(*start_day), date.end_of_week(*start_day), options)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def by_cweek(*args)
|
85
|
+
with_by_star_options(*args) do |time, options|
|
86
|
+
by_week(ByStar::Normalization.cweek(time, options), options)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def by_weekend(*args)
|
91
|
+
with_by_star_options(*args) do |time, options|
|
92
|
+
date = ByStar::Normalization.week(time, options)
|
93
|
+
between_dates(date.beginning_of_weekend, date.end_of_weekend, options)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def by_fortnight(*args)
|
98
|
+
with_by_star_options(*args) do |time, options|
|
99
|
+
date = ByStar::Normalization.fortnight(time, options)
|
100
|
+
between_dates(date.beginning_of_fortnight, date.end_of_fortnight, options)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def by_month(*args)
|
105
|
+
with_by_star_options(*args) do |time, options|
|
106
|
+
date = ByStar::Normalization.month(time, options)
|
107
|
+
between_dates(date.beginning_of_month, date.end_of_month, options)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def by_calendar_month(*args)
|
112
|
+
with_by_star_options(*args) do |time, options|
|
113
|
+
date = ByStar::Normalization.month(time, options)
|
114
|
+
start_day = Array(options[:start_day])
|
115
|
+
between_dates(date.beginning_of_calendar_month(*start_day), date.end_of_calendar_month(*start_day), options)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def by_quarter(*args)
|
120
|
+
with_by_star_options(*args) do |time, options|
|
121
|
+
date = ByStar::Normalization.quarter(time, options)
|
122
|
+
between_dates(date.beginning_of_quarter, date.end_of_quarter, options)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def by_year(*args)
|
127
|
+
with_by_star_options(*args) do |time, options|
|
128
|
+
date = ByStar::Normalization.year(time, options)
|
129
|
+
between_dates(date.beginning_of_year, date.to_date.end_of_year, options)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def today(options = {})
|
134
|
+
by_day(Date.current, options)
|
135
|
+
end
|
136
|
+
|
137
|
+
def yesterday(options = {})
|
138
|
+
by_day(Date.yesterday, options)
|
139
|
+
end
|
140
|
+
|
141
|
+
def tomorrow(options = {})
|
142
|
+
by_day(Date.tomorrow, options)
|
143
|
+
end
|
144
|
+
|
145
|
+
def past_day(options = {})
|
146
|
+
between_times(Time.current - 1.day, Time.current, options)
|
147
|
+
end
|
148
|
+
|
149
|
+
def past_week(options = {})
|
150
|
+
between_times(Time.current - 1.week, Time.current, options)
|
151
|
+
end
|
152
|
+
|
153
|
+
def past_fortnight(options = {})
|
154
|
+
between_times(Time.current - 2.weeks, Time.current, options)
|
155
|
+
end
|
156
|
+
|
157
|
+
def past_month(options = {})
|
158
|
+
between_times(Time.current - 1.month, Time.current, options)
|
159
|
+
end
|
160
|
+
|
161
|
+
def past_year(options = {})
|
162
|
+
between_times(Time.current - 1.year, Time.current, options)
|
163
|
+
end
|
164
|
+
|
165
|
+
def next_day(options = {})
|
166
|
+
between_times(Time.current, Time.current + 1.day, options)
|
167
|
+
end
|
168
|
+
|
169
|
+
def next_week(options = {})
|
170
|
+
between_times(Time.current, Time.current + 1.week, options)
|
171
|
+
end
|
172
|
+
|
173
|
+
def next_fortnight(options = {})
|
174
|
+
between_times(Time.current, Time.current + 2.weeks, options)
|
175
|
+
end
|
176
|
+
|
177
|
+
def next_month(options = {})
|
178
|
+
between_times(Time.current, Time.current + 1.month, options)
|
179
|
+
end
|
180
|
+
|
181
|
+
def next_year(options = {})
|
182
|
+
between_times(Time.current, Time.current + 1.year, options)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|