Module: ByStar::Between

Included in:
Base
Defined in:
lib/by_star/between.rb

Instance Method Summary collapse

Instance Method Details

#at_time(*args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/by_star/between.rb', line 53

def at_time(*args)
  with_by_star_options(*args) do |time, options|
    start_field = by_star_start_field(options)
    end_field = by_star_end_field(options)

    scope = self
    scope = if start_field == end_field
              by_star_point_overlap_query(scope, start_field, time)
            else
              by_star_span_overlap_query(scope, start_field, end_field, time, options)
            end
    scope = by_star_order(scope, options[:order]) if options[:order]
    scope
  end
end

#between_dates(*args) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/by_star/between.rb', line 45

def between_dates(*args)
  options = args.extract_options!
  start_date, end_date = ByStar::Normalization.extract_range(args)
  start_date = ByStar::Normalization.date(start_date)
  end_date   = ByStar::Normalization.date(end_date)
  between_times(start_date, end_date, options)
end

#between_times(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/by_star/between.rb', line 5

def between_times(*args)
  options = args.extract_options!.symbolize_keys!

  start_time, end_time = ByStar::Normalization.extract_range(args)
  offset = options[:offset] || 0

  if start_time.is_a?(Date)
    start_time = ByStar::Normalization.apply_offset_start(start_time.in_time_zone, offset)
  elsif start_time
    start_time += offset.seconds
  end

  if end_time.is_a?(Date)
    end_time = ByStar::Normalization.apply_offset_end(end_time.in_time_zone, offset)
  elsif end_time
    end_time += offset.seconds
  end

  start_field = by_star_start_field(options)
  end_field = by_star_end_field(options)

  scope = self
  scope = if !start_time && !end_time
            scope # do nothing
          elsif !end_time
            by_star_after_query(scope, start_field, start_time)
          elsif !start_time
            by_star_before_query(scope, start_field, end_time)
          elsif start_field == end_field
            by_star_point_query(scope, start_field, start_time, end_time)
          elsif options[:strict]
            by_star_span_strict_query(scope, start_field, end_field, start_time, end_time)
          else
            by_star_span_loose_query(scope, start_field, end_field, start_time, end_time, options)
          end

  scope = by_star_order(scope, options[:order]) if options[:order]
  scope
end

#by_calendar_month(*args) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/by_star/between.rb', line 111

def by_calendar_month(*args)
  with_by_star_options(*args) do |time, options|
    date = ByStar::Normalization.month(time, options)
    start_day = Array(options[:start_day])
    between_dates(date.beginning_of_calendar_month(*start_day), date.end_of_calendar_month(*start_day), options)
  end
end

#by_cweek(*args) ⇒ Object



84
85
86
87
88
# File 'lib/by_star/between.rb', line 84

def by_cweek(*args)
  with_by_star_options(*args) do |time, options|
    by_week(ByStar::Normalization.cweek(time, options), options)
  end
end

#by_day(*args) ⇒ Object



69
70
71
72
73
74
# File 'lib/by_star/between.rb', line 69

def by_day(*args)
  with_by_star_options(*args) do |time, options|
    date = ByStar::Normalization.date(time)
    between_dates(date, date, options)
  end
end

#by_fortnight(*args) ⇒ Object



97
98
99
100
101
102
# File 'lib/by_star/between.rb', line 97

def by_fortnight(*args)
  with_by_star_options(*args) do |time, options|
    date = ByStar::Normalization.fortnight(time, options)
    between_dates(date.beginning_of_fortnight, date.end_of_fortnight, options)
  end
end

#by_month(*args) ⇒ Object



104
105
106
107
108
109
# File 'lib/by_star/between.rb', line 104

def by_month(*args)
  with_by_star_options(*args) do |time, options|
    date = ByStar::Normalization.month(time, options)
    between_dates(date.beginning_of_month, date.end_of_month, options)
  end
end

#by_quarter(*args) ⇒ Object



119
120
121
122
123
124
# File 'lib/by_star/between.rb', line 119

def by_quarter(*args)
  with_by_star_options(*args) do |time, options|
    date = ByStar::Normalization.quarter(time, options)
    between_dates(date.beginning_of_quarter, date.end_of_quarter, options)
  end
end

#by_week(*args) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/by_star/between.rb', line 76

def by_week(*args)
  with_by_star_options(*args) do |time, options|
    date = ByStar::Normalization.week(time, options)
    start_day = Array(options[:start_day])
    between_dates(date.beginning_of_week(*start_day), date.end_of_week(*start_day), options)
  end
end

#by_weekend(*args) ⇒ Object



90
91
92
93
94
95
# File 'lib/by_star/between.rb', line 90

def by_weekend(*args)
  with_by_star_options(*args) do |time, options|
    date = ByStar::Normalization.week(time, options)
    between_dates(date.beginning_of_weekend, date.end_of_weekend, options)
  end
end

#by_year(*args) ⇒ Object



126
127
128
129
130
131
# File 'lib/by_star/between.rb', line 126

def by_year(*args)
  with_by_star_options(*args) do |time, options|
    date = ByStar::Normalization.year(time, options)
    between_dates(date.beginning_of_year, date.to_date.end_of_year, options)
  end
end

#next_day(options = {}) ⇒ Object



165
166
167
# File 'lib/by_star/between.rb', line 165

def next_day(options = {})
  between_times(Time.current, Time.current + 1.day, options)
end

#next_fortnight(options = {}) ⇒ Object



173
174
175
# File 'lib/by_star/between.rb', line 173

def next_fortnight(options = {})
  between_times(Time.current, Time.current + 2.weeks, options)
end

#next_month(options = {}) ⇒ Object



177
178
179
# File 'lib/by_star/between.rb', line 177

def next_month(options = {})
  between_times(Time.current, Time.current + 1.month, options)
end

#next_week(options = {}) ⇒ Object



169
170
171
# File 'lib/by_star/between.rb', line 169

def next_week(options = {})
  between_times(Time.current, Time.current  + 1.week, options)
end

#next_year(options = {}) ⇒ Object



181
182
183
# File 'lib/by_star/between.rb', line 181

def next_year(options = {})
  between_times(Time.current, Time.current + 1.year, options)
end

#past_day(options = {}) ⇒ Object



145
146
147
# File 'lib/by_star/between.rb', line 145

def past_day(options = {})
  between_times(Time.current - 1.day, Time.current, options)
end

#past_fortnight(options = {}) ⇒ Object



153
154
155
# File 'lib/by_star/between.rb', line 153

def past_fortnight(options = {})
  between_times(Time.current - 2.weeks, Time.current, options)
end

#past_month(options = {}) ⇒ Object



157
158
159
# File 'lib/by_star/between.rb', line 157

def past_month(options = {})
  between_times(Time.current - 1.month, Time.current, options)
end

#past_week(options = {}) ⇒ Object



149
150
151
# File 'lib/by_star/between.rb', line 149

def past_week(options = {})
  between_times(Time.current - 1.week, Time.current, options)
end

#past_year(options = {}) ⇒ Object



161
162
163
# File 'lib/by_star/between.rb', line 161

def past_year(options = {})
  between_times(Time.current - 1.year, Time.current, options)
end

#today(options = {}) ⇒ Object



133
134
135
# File 'lib/by_star/between.rb', line 133

def today(options = {})
  by_day(Date.current, options)
end

#tomorrow(options = {}) ⇒ Object



141
142
143
# File 'lib/by_star/between.rb', line 141

def tomorrow(options = {})
  by_day(Date.tomorrow, options)
end

#yesterday(options = {}) ⇒ Object



137
138
139
# File 'lib/by_star/between.rb', line 137

def yesterday(options = {})
  by_day(Date.yesterday, options)
end