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



11
12
13
14
15
# File 'lib/timescopes/scopes.rb', line 11

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



5
6
7
8
9
# File 'lib/timescopes/scopes.rb', line 5

def hourly(start=nil)
  start ||= Time.now
  [ Chronic.parse(start.strftime("%Y-%m-%d #{start.hour}:00:00Z")),
    Chronic.parse(start.strftime("%Y-%m-%d #{start.hour}:59:59Z")) ]
end

.monthly(start = nil) ⇒ Object



25
26
27
28
29
# File 'lib/timescopes/scopes.rb', line 25

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

.overall(start = nil) ⇒ Object



31
32
33
34
# File 'lib/timescopes/scopes.rb', line 31

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



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

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

.start_of_month(time) ⇒ Object



40
41
42
# File 'lib/timescopes/scopes.rb', line 40

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

.weekly(start = nil) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/timescopes/scopes.rb', line 17

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