Module: TimeScopes

Defined in:
lib/timescopes/scopes.rb,
lib/timescopes/rails/active_record_extension.rb

Defined Under Namespace

Modules: Rails

Class Method Summary collapse

Class Method Details

.daily(start = nil) ⇒ Object Also known as: day



42
43
44
45
46
# File 'lib/timescopes/scopes.rb', line 42

def daily(start=nil)
  start ||= Time.now
  [ Chronic.parse(start_of_day(start.utc)),
    Chronic.parse(start_of_day(1.day.since(start.utc))) ]
end

.hourly(start = nil) ⇒ Object Also known as: hour



35
36
37
38
39
# File 'lib/timescopes/scopes.rb', line 35

def hourly(start=nil)
  start ||= Time.now
  [ Chronic.parse(start_of_hour(start.utc)),
    Chronic.parse(start_of_hour(1.hour.since(start.utc))) ]
end

.minutely(start = nil) ⇒ Object Also known as: minute



28
29
30
31
32
# File 'lib/timescopes/scopes.rb', line 28

def minutely(start=nil)
  start ||= Time.now
  [ Chronic.parse(start_of_minute(start.utc)),
    Chronic.parse(start_of_minute(1.minute.since(start.utc))) ]
end

.monthly(start = nil) ⇒ Object Also known as: month



58
59
60
61
62
# File 'lib/timescopes/scopes.rb', line 58

def monthly(start=nil)
  start ||= Time.now
  [ Chronic.parse(start_of_month(start.utc)),
    Chronic.parse(start_of_month(1.month.since(start.utc))) ]
end

.next_range(scope, start) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/timescopes/scopes.rb', line 5

def next_range(scope, start)
  case scope.to_sym
  when :minutely, :minute
    1.hour.since(start).utc
  when :hourly, :hour
    1.day.since(start).utc
  when :daily, :day
    1.day.since(start).utc
  when :weekly, :week
    1.week.since(start).utc
  when :monthly, :month
    1.month.since(start).utc
  when :yearly, :year
    1.year.since(start).utc
  when :rolling_7, :rolling_30
    1.day.since(start).utc
  when :overall, nil
    nil
  else
    raise "Unknown scope passed to TimeScopes.next_range"
  end
end

.overall(start = nil) ⇒ Object



72
73
74
75
# File 'lib/timescopes/scopes.rb', line 72

def overall(start=nil)
  [ Chronic.parse('2000-01-01 00:00:00'),
    Chronic.parse(start_of_day(1.day.from_now.utc)) ]
end

.start_of_day(time) ⇒ Object



85
86
87
# File 'lib/timescopes/scopes.rb', line 85

def start_of_day(time)
  time.strftime("%Y-%m-%d 00:00:00Z")
end

.start_of_hour(time) ⇒ Object



81
82
83
# File 'lib/timescopes/scopes.rb', line 81

def start_of_hour(time)
  time.strftime("%Y-%m-%d %H:00:00Z")
end

.start_of_minute(time) ⇒ Object



77
78
79
# File 'lib/timescopes/scopes.rb', line 77

def start_of_minute(time)
  time.strftime("%Y-%m-%d %H:%M:00Z")
end

.start_of_month(time) ⇒ Object



89
90
91
# File 'lib/timescopes/scopes.rb', line 89

def start_of_month(time)
  time.strftime("%Y-%m-01 00:00:00Z")
end

.start_of_year(time) ⇒ Object



93
94
95
# File 'lib/timescopes/scopes.rb', line 93

def start_of_year(time)
  time.strftime("%Y-01-01 00:00:00Z")
end

.weekly(start = nil) ⇒ Object Also known as: week



49
50
51
52
53
54
55
# File 'lib/timescopes/scopes.rb', line 49

def weekly(start=nil)
  start ||= Time.now
  start = start.utc
  start = start - 1.day while !start.monday?
  [ Chronic.parse(start_of_day(start.utc)),
    Chronic.parse(start_of_day(1.week.since(start.utc))) ]
end

.yearly(start = nil) ⇒ Object Also known as: year



65
66
67
68
69
# File 'lib/timescopes/scopes.rb', line 65

def yearly(start=nil)
  start ||= Time.now
  [ Chronic.parse(start_of_year(start.utc)),
    Chronic.parse(start_of_year(1.year.since(start.utc))) ]
end