Class: Artic::Availability
- Inherits:
-
Object
- Object
- Artic::Availability
- 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.
Constant Summary collapse
- DAYS_OF_WEEK =
i[monday tuesday wednesday thursday friday saturday sunday].freeze
Instance Attribute Summary collapse
-
#date ⇒ Date
readonly
The date of this availability.
-
#day_of_week ⇒ Symbol
readonly
The day of the week, as a lowercase symbol (e.g.
:monday). -
#time_range ⇒ Object
readonly
Returns the value of attribute time_range.
Instance Method Summary collapse
-
#<=>(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:.
-
#==(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.
-
#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).
-
#identifier ⇒ Date|Symbol
Returns the identifier used to create this availability (i.e. either a day of the week or date).
-
#initialize(dow_or_date, time_range) ⇒ Availability
constructor
Initializes the availability.
Constructor Details
#initialize(dow_or_date, time_range) ⇒ Availability
Initializes the availability.
27 28 29 30 31 32 33 |
# File 'lib/artic/availability.rb', line 27 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
#date ⇒ Date (readonly)
Returns the date of this availability.
19 |
# File 'lib/artic/availability.rb', line 19 attr_reader :day_of_week, :date, :time_range |
#day_of_week ⇒ Symbol (readonly)
Rename to wday
Returns the day of the week, as a lowercase symbol (e.g. :monday).
19 20 21 |
# File 'lib/artic/availability.rb', line 19 def day_of_week @day_of_week end |
#time_range ⇒ Object (readonly)
Returns the value of attribute time_range.
19 |
# File 'lib/artic/availability.rb', line 19 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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/artic/availability.rb', line 79 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? # rubocop:disable Style/IfInsideElse 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 # rubocop:enable Style/IfInsideElse 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.
64 65 66 |
# File 'lib/artic/availability.rb', line 64 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).
49 50 51 52 53 54 55 |
# File 'lib/artic/availability.rb', line 49 def for_date?(date) if self.date self.date == date else day_of_week == date.strftime('%A').downcase.to_sym end end |
#identifier ⇒ Date|Symbol
Returns the identifier used to create this availability (i.e. either a day of the week or date).
39 40 41 |
# File 'lib/artic/availability.rb', line 39 def identifier date || day_of_week end |