Class: Artic::Availability

Inherits:
Object
  • Object
show all
Defined in:
lib/artic/availability.rb

Overview

Availability represents a slot of time in a givn day of the week or date when you’re available.

Author:

  • Alessandro Desantis

Constant Summary collapse

DAYS_OF_WEEK =
%i(monday tuesday wednesday thursday friday saturday sunday).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dow_or_date, time_range) ⇒ Availability

Initializes the availability.

Parameters:

  • dow_or_date (Symbol|Date)

    a day of the week (e.g. :monday) or date

  • time_range (TimeRange|Range)

    a time range

Raises:

  • (ArgumentError)

    if an invalid day of the week is passed



26
27
28
29
30
31
32
# File 'lib/artic/availability.rb', line 26

def initialize(dow_or_date, time_range)
  @date = dow_or_date if dow_or_date.is_a?(Date)
  @day_of_week = (@date ? @date.strftime('%A').downcase : dow_or_date).to_sym
  @time_range = TimeRange.build(time_range)

  validate_day_of_week
end

Instance Attribute Details

#dateDate (readonly)

Returns the date of this availability.

Returns:

  • (Date)

    the date of this availability



18
# File 'lib/artic/availability.rb', line 18

attr_reader :day_of_week, :date, :time_range

#day_of_weekSymbol (readonly)

TODO:

Rename to wday

Returns the day of the week, as a lowercase symbol (e.g. :monday).

Returns:

  • (Symbol)

    the day of the week, as a lowercase symbol (e.g. :monday)



18
19
20
# File 'lib/artic/availability.rb', line 18

def day_of_week
  @day_of_week
end

#time_rangeObject (readonly)

Returns the value of attribute time_range.



18
# File 'lib/artic/availability.rb', line 18

attr_reader :day_of_week, :date, :time_range

Instance Method Details

#<=>(other) ⇒ Fixnum

Returns whether this availability should be before, after or in the same position of the availability passed as an argument, by following these rules:

* availabilities with a weekday come before those with a date;
* if both availabilities are for a weekday, the day and time ranges are compared;
* if both availabilities are for a date, the date and time ranges are compared.

Returns:

  • (Fixnum)

    -1 if this availability should come before the argument, 0 if it should be at the same position, 1 if it should come after the argument.

See Also:

  • TimeRange#<=>


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/artic/availability.rb', line 78

def <=>(other)
  # availabilities for weekdays come before availabilities for specific dates
  return -1 if date.nil? && !other.date.nil?
  return 1 if !date.nil? && other.date.nil?

  if date.nil? && other.date.nil? # both availabilities are for a weekday
    if day_of_week == other.day_of_week # availabilities are for the same weekday
      time_range.min <=> other.time_range.min # compare times
    else # availabilities are for different weekdays
      index1 = DAYS_OF_WEEK.index(day_of_week)
      index2 = DAYS_OF_WEEK.index(other.day_of_week)

      index1 <=> index2 # compares weekdays
    end
  else # both availabilities are for a date
    if date == other.date # both availabilities are for the same date
      time_range.min <=> other.time_range.min # compare times
    else # availabilities are for different dates
      date <=> other.date # compare dates
    end
  end
end

#==(other) ⇒ Boolean

Determines whether this availability and the one passed as an argument represent the same day/time range combination, by checking for equality of both the identifier and the time range.

Parameters:

Returns:

  • (Boolean)


63
64
65
# File 'lib/artic/availability.rb', line 63

def ==(other)
  identifier == other.identifier && time_range == other.time_range
end

#for_date?(date) ⇒ Boolean

Returns whether the availability is appliable to the given date (i.e. the date is the same or the weekday is the same).

Parameters:

  • date (Date)

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/artic/availability.rb', line 48

def for_date?(date)
  if self.date
    self.date == date
  else
    day_of_week == date.strftime('%A').downcase.to_sym
  end
end

#identifierDate|Symbol

Returns the identifier used to create this availability (i.e. either a day of the week or date).

Returns:

  • (Date|Symbol)


38
39
40
# File 'lib/artic/availability.rb', line 38

def identifier
  date || day_of_week
end