Class: TExp::DayInterval

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

Direct Known Subclasses

MonthInterval, WeekInterval

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(base_date, interval) ⇒ DayInterval

Returns a new instance of DayInterval.



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

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

#intervalObject (readonly)

Returns the value of attribute interval.



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

def interval
  @interval
end

Class Method Details

.parse_callback(stack) ⇒ Object



66
67
68
69
70
# File 'lib/texp/day_interval.rb', line 66

def parse_callback(stack)
  interval = stack.pop
  date = stack.pop
  stack.push self.new(date, interval)
end

Instance Method Details

#day_multiplierObject



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

def day_multiplier
  1
end

#encode(codes) ⇒ Object

Encode the temporal expression into codes.



46
47
48
49
50
51
52
53
# File 'lib/texp/day_interval.rb', line 46

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)


21
22
23
24
25
26
27
# File 'lib/texp/day_interval.rb', line 21

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

#inspectObject

Human readable version of the temporal expression.



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

def inspect
  if @interval == 1
    "every #{interval_unit}"
  elsif @interval == 2
    "every other #{interval_unit}"
  else
    "every #{@interval} #{pluralize(interval_unit)}"
  end
end

#interval_unitObject



11
12
13
# File 'lib/texp/day_interval.rb', line 11

def interval_unit
  "day"
end

#reanchor(new_anchor_date) ⇒ Object

Create a new temporal expression with a new anchor date.



30
31
32
# File 'lib/texp/day_interval.rb', line 30

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