Class: SimplesIdeias::Recurrence::Event::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/recurrence/event/base.rb

Direct Known Subclasses

Daily, Monthly, Weekly, Yearly

Constant Summary collapse

CARDINALS =
%w(first second third fourth fifth)
WEEKDAYS =
{
  "sun" => 0, "sunday"    => 0,
  "mon" => 1, "monday"    => 1,
  "tue" => 2, "tuesday"   => 2,
  "wed" => 3, "wednesday" => 3,
  "thu" => 4, "thursday"  => 4,
  "fri" => 5, "friday"    => 5,
  "sat" => 6, "saturday"  => 6
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/recurrence/event/base.rb', line 18

def initialize(options = {})
  every, options = nil, every if every.kind_of?(Hash)

  @options    = options
  @date       = options[:starts]
  @finished   = false

  validate
  raise ArgumentError, "interval should be greater than zero" if @options[:interval] <= 0
  raise ArgumentError, "repeat should be greater than zero" if !@options[:repeat].nil? && @options[:repeat] <= 0

  prepare!
end

Instance Attribute Details

#start_dateObject

Returns the value of attribute start_date.



16
17
18
# File 'lib/recurrence/event/base.rb', line 16

def start_date
  @start_date
end

Instance Method Details

#finished?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/recurrence/event/base.rb', line 53

def finished?
  @finished
end

#nextObject



44
45
46
47
# File 'lib/recurrence/event/base.rb', line 44

def next
  return nil if finished?
  @date || @start_date
end

#next!Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/recurrence/event/base.rb', line 32

def next!
  return nil if finished?
  return @date = @start_date if @start_date && @date.nil?

  @date = next_in_recurrence

  @finished = true if @options[:through] && @date >= @options[:through]
  @finished, @date = true, nil if @date > @options[:until]
  shift_to @date if @date && @options[:shift]
  @date
end

#reset!Object



49
50
51
# File 'lib/recurrence/event/base.rb', line 49

def reset!
  @date = nil
end