Class: TExp::DayInterval

Inherits:
Base
  • Object
show all
Defined in:
lib/texp/day_interval.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#*, #+, #-, #-@, #each, #first_day_of_window, #last_day_of_window, register_parse_callback, #to_s, #window

Constructor Details

#initialize(base_date, interval) ⇒ DayInterval

Returns a new instance of DayInterval.



7
8
9
10
# File 'lib/texp/day_interval.rb', line 7

def initialize(base_date, interval)
  @base_date = base_date.kind_of?(Date) ? base_date : nil
  @interval = interval
end

Instance Attribute Details

#base_dateObject (readonly)

Returns the value of attribute base_date.



5
6
7
# File 'lib/texp/day_interval.rb', line 5

def base_date
  @base_date
end

Class Method Details

.parse_callback(stack) ⇒ Object



52
53
54
55
56
# File 'lib/texp/day_interval.rb', line 52

def parse_callback(stack)
  interval = stack.pop
  date = stack.pop
  stack.push TExp::DayInterval.new(date, interval)
end

Instance Method Details

#encode(codes) ⇒ Object

Encode the temporal expression into codes.



36
37
38
39
40
41
42
43
# File 'lib/texp/day_interval.rb', line 36

def encode(codes)
  if @base_date
    encode_date(codes, @base_date)
  else
    codes << 0
  end
  codes << ',' << @interval << encoding_token
end

#includes?(date) ⇒ Boolean

Is date included in the temporal expression.

Returns:

  • (Boolean)


13
14
15
16
17
18
19
# File 'lib/texp/day_interval.rb', line 13

def includes?(date)
  if @base_date.nil? || date < @base_date
    false
  else
    ((date.mjd - base_mjd) % @interval) == 0
  end
end

#inspectObject

Human readable version of the temporal expression.



27
28
29
30
31
32
33
# File 'lib/texp/day_interval.rb', line 27

def inspect
  if @interval == 1
    "every day starting on #{humanize_date(@base_date)}"
  else
    "every #{ordinal(@interval)} day starting on #{humanize_date(@base_date)}"
  end
end

#reanchor(new_anchor_date) ⇒ Object

Create a new temporal expression with a new anchor date.



22
23
24
# File 'lib/texp/day_interval.rb', line 22

def reanchor(new_anchor_date)
  self.class.new(new_anchor_date, @interval)
end