Module: IncludeDateScopes::DefineTimestampScopes

Included in:
IncludeDateScopes::DateScopes::ClassMethods
Defined in:
lib/include_date_scopes/define_timestamp_scopes.rb

Instance Method Summary collapse

Instance Method Details

#define_timestamp_scopes_for(column_name, prepend_name = false) ⇒ 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/include_date_scopes/define_timestamp_scopes.rb', line 5

def define_timestamp_scopes_for(column_name, prepend_name = false)
  prefix = prepend_name ? "#{column_name}_" : ""
  t = self.arel_table

  define_singleton_method :"#{prefix}between" do |start_date_or_time, stop_date_or_time|
    start_time = (start_date_or_time.is_a?(Date) ? start_date_or_time.to_time : start_date_or_time)
    stop_time = (stop_date_or_time.is_a?(Date) ? stop_date_or_time.to_time + 1.day : stop_date_or_time )
    where(t[column_name].gteq(start_time).and(t[column_name].lt stop_time))
  end

  define_singleton_method :"#{prefix}on_or_before_date" do |date|
    where( t[column_name].lt date.to_date.tomorrow.midnight.gmtime )
  end

  define_singleton_method :"#{prefix}on_or_before" do |time| 
    where(t[column_name].lteq (time.is_a?(Date) ? time.to_time : time))
  end

  define_singleton_method :"#{prefix}before" do |time| 
    where(t[column_name].lt (time.is_a?(Date) ? time.to_time : time))
  end

  define_singleton_method :"#{prefix}on_or_after_date" do |time|
    where(t[column_name].gteq time.to_date.midnight.gmtime )
  end

  define_singleton_method :"#{prefix}on_or_after" do |time|
    where(t[column_name].gteq (time.is_a?(Date) ? time.to_time + 1.day : time ))
  end

  define_singleton_method :"#{prefix}after" do |time|
    where(t[column_name].gt (time.is_a?(Date) ? time.to_time + 1.day : time ))
  end

  define_singleton_method :"#{prefix}on" do |day| 
    __send__(:"#{prefix}between", day.beginning_of_day, day.end_of_day)
  end

  define_singleton_method :"#{prefix}last_24_hours" do
    __send__(:"#{prefix}last_day")
  end

  [:minute, :hour].each do |time_unit|
    define_singleton_method :"#{prefix}next_#{time_unit}" do
     __send__(:"#{prefix}between", Time.now, 1.send(time_unit).from_now)
    end

    define_singleton_method :"#{prefix}last_#{time_unit}" do
      __send__(:"#{prefix}between", 1.send(time_unit).ago, Time.now)
    end
  end

  [:seconds, :minutes, :hours].each do |time_unit|
    define_singleton_method :"#{prefix}last_n_#{time_unit}" do |count|
      __send__(:"#{prefix}between", count.send(time_unit).ago, Time.now)
    end

    define_singleton_method :"#{prefix}next_n_#{time_unit}" do |count|
      __send__(:"#{prefix}between", Time.now, count.send(time_unit).from_now)
    end
  end

  define_singleton_method :"#{prefix}this_minute" do
    __send__(:"#{prefix}between", Time.now.change(sec: 0), Time.now.change(sec: 59, usec: Rational(999999999, 1000)))
  end

  [:hour, :day, :week, :month, :year].each do |time_unit|
    define_singleton_method :"#{prefix}this_#{time_unit}" do
      __send__(:"#{prefix}between", 1.send(time_unit).ago.send(:"end_of_#{time_unit}"), Time.now.send(:"end_of_#{time_unit}"))
    end
  end

  define_common_scopes prefix, column_name
end