Class: Spill::Window

Inherits:
Data
  • Object
show all
Defined in:
lib/spill/window.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label

Returns:

  • (Object)

    the current value of label



4
5
6
# File 'lib/spill/window.rb', line 4

def label
  @label
end

#sinceObject (readonly)

Returns the value of attribute since

Returns:

  • (Object)

    the current value of since



4
5
6
# File 'lib/spill/window.rb', line 4

def since
  @since
end

Class Method Details

.default(now: Time.now) ⇒ Object



5
6
7
# File 'lib/spill/window.rb', line 5

def self.default(now: Time.now)
  new(since: midnight(now) - 86_400, label: "today + yesterday")
end

.midnight(time) ⇒ Object



23
24
25
# File 'lib/spill/window.rb', line 23

def self.midnight(time)
  Time.new(time.year, time.month, time.day)
end

.parse(expr, now: Time.now) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/spill/window.rb', line 9

def self.parse(expr, now: Time.now)
  text = expr.strip
  since =
    case text
    when "today" then midnight(now)
    when "yesterday" then midnight(now) - 86_400
    when /\A(\d+)\s+hours?\s+ago\z/ then now - ($1.to_i * 3_600)
    when /\A(\d+)\s+days?\s+ago\z/ then now - ($1.to_i * 86_400)
    when /\A(\d+)\s+weeks?\s+ago\z/ then now - ($1.to_i * 7 * 86_400)
    else Time.parse(text)
    end
  new(since: since, label: text)
end