class Spok
Public: Class responsible for dealing with periods of Dates, considering workdays and restdays.
Constants
- DATE_FORMAT
Internal: String specifying format for dates in the period.
- VERSION
Attributes
Public Class Methods
Public: Add a new calendar of holidays to Spok
. Calendars
are defined with YAML files containing a single key with its name and an array of dates.
For reference of builtin calendars, please see the `lib/spok/config` directory.
name - A String or Symbol with the name of the calendar. path - A String with the full path for the calendar YAML file.
Returns nothing.
# File lib/spok.rb, line 27 def self.add_calendar(name, path) Spok::Calendars.add(name, path) end
# File lib/spok.rb, line 307 def default_calendar=(calendar) class_variable_set(:@@default_calendar, calendar) end
Public: Checks if a given Date is on a holiday.
date - The Date to be checked. calendar - Symbol informing in which calendar the date will be checked
(default: :brazil).
Examples
Spok.holiday?(Date.new(2012, 5, 1)) # => true
Returns a boolean.
# File lib/spok.rb, line 178 def self.holiday?(date, calendar: Spok.default_calendar) Workday.holiday?(date, calendar: calendar) end
Public: Returns the last workday until the informed date. It returns the informed date in case it is a workday.
date - End Date to check for workdays. calendar - Symbol informing in which calendar the date will be checked
(default: :brazil).
Examples
Spok.last_workday(Date.new(2012, 10, 21)) # => #<Date: 2012-10-19 ((2456220j,0s,0n),+0s,2299161j)>
# File lib/spok.rb, line 192 def self.last_workday(date, calendar: Spok.default_calendar) Workday.last_workday(date, calendar: calendar) end
Public: Initialize a Spok
.
start_date
- Initial Date for the Spok
. end_date
- Final Date for Spok
.
# File lib/spok.rb, line 61 def initialize(start_date, end_date) @start_date = start_date @end_date = end_date validate! end
Public: Returns the next workday starting from the informed date. It returns the informed date in case it is a workday.
date - Start Date to check for workdays. calendar - Symbol informing in which calendar the date will be checked
(default: :brazil).
Examples
Spok.next_workday(Date.new(2012, 10, 21)) # => #<Date: 2012-10-19 ((2456220j,0s,0n),+0s,2299161j)>
# File lib/spok.rb, line 206 def self.next_workday(date, calendar: Spok.default_calendar) Workday.next_workday(date, calendar: calendar) end
Public: Parses a string into a Spok
.
dates_string - String containing the start and end dates for a period of
days separated by a dash.
Examples
Spok.parse('20120101-20120103') # => #<Spok:0x00007f951e8a2ea0 ...> Spok.parse('invalid string') # => nil
Returns a Spok
or nil when the string does not contain two valid dates.
# File lib/spok.rb, line 45 def self.parse(dates_string) return nil unless dates_string start_date, end_date = dates_string.split('-') if start_date && end_date return Spok.new(::Date.parse(start_date), ::Date.parse(end_date)) end nil end
Public: Checks if a given day is a restday.
date - The Date to be checked. calendar - Symbol informing in which calendar the date will be checked
(default: :brazil).
Examples
Spok.restday?(Date.new(2012, 8, 6)) # => false
Returns a boolean.
# File lib/spok.rb, line 132 def self.restday?(date, calendar: Spok.default_calendar) Workday.restday?(date, calendar: calendar) end
Public: Checks if a given Date is on a weekend.
date - The Date to be checked.
Examples
Spok.weekend?(Date.new(2012, 8, 6)) # => false
Returns a boolean.
# File lib/spok.rb, line 162 def self.weekend?(date) Workday.weekend?(date) end
Public: Checks if a given day is a workday.
date - The Date to be checked. calendar - Symbol informing in which calendar the date will be checked
(default: :brazil).
Examples
Spok.workday?(Date.new(2012, 8, 6)) # => true
Returns a boolean.
# File lib/spok.rb, line 148 def self.workday?(date, calendar: Spok.default_calendar) Workday.workday?(date, calendar: calendar) end
Public Instance Methods
Public: Compares the Spok
with other Spok
. Two spoks are considered equal when they are both instances of Spok
, and have the same start and end dates.
other - Spok
to be checked against.
Examples
spok == other # => false
Returns a boolean.
# File lib/spok.rb, line 290 def ==(other) other.class == self.class && other.start_date == @start_date && other.end_date == @end_date end
Public: Returns an Integer with the number of days in the Spok
.
Examples
spok.days_count # => 2
# File lib/spok.rb, line 264 def days_count (@end_date - @start_date).to_i end
Public: Returns the Spok
end date as an Integer.
Examples
spok.end_date_as_integer # => 20120103
# File lib/spok.rb, line 83 def end_date_as_integer as_integer @end_date end
Public: Returns the Spok
end date as a String.
Examples
spok.end_date_as_string # => "20120103"
# File lib/spok.rb, line 103 def end_date_as_string as_string @end_date end
Public: Informs whether the Spok
has just one day or not.
Examples
spok.one_day? # => false
Returns a boolean.
# File lib/spok.rb, line 254 def one_day? @start_date == @end_date end
Public: Returns the Spok
start date as an Integer.
Examples
spok.start_date_as_integer # => 20120101
# File lib/spok.rb, line 73 def start_date_as_integer as_integer @start_date end
Public: Returns the Spok
start date as a String.
Examples
spok.start_date_as_string # => "20120101"
# File lib/spok.rb, line 93 def start_date_as_string as_string @start_date end
Public: Returns an array containing all Dates on Spok
.
Examples
spok.to_a # => [Sun, 01 Jan 2012, Mon, 02 Jan 2012, Tue, 03 Jan 2012]
# File lib/spok.rb, line 231 def to_a (@start_date..@end_date).to_a end
Public: Returns a Spok
containing the same dates in a different calendar.
calendar - Symbol informing calendar for new Spok
(default: :brazil).
Examples
spok.to_calendar(:brazil) # => #<Spok:0x00007fbf122dba08 ...>
# File lib/spok.rb, line 218 def to_calendar(calendar = Spok.default_calendar) Spok.new( Workday.last_workday(@start_date, calendar: calendar), Workday.last_workday(@end_date, calendar: calendar) ) end
Public: Returns a range containing the Dates in the Spok
.
Examples
spok.to_range # => Sun, 01 Jan 2012..Tue, 03 Jan 2012
# File lib/spok.rb, line 302 def to_range (@start_date..@end_date) end
Public: Returns a String containing the Spok
start and end date separated by a dash.
Examples
spok.to_s # => "20120101-20120103"
# File lib/spok.rb, line 242 def to_s "#{start_date_as_string}-#{end_date_as_string}" end
Public: Returns an array containing all workdays on Spok
.
calendar - Symbol informing in which calendar to check for workdays
(default: :brazil).
Examples
spok.workdays # => [Mon, 02 Jan 2012, Tue, 03 Jan 2012]
# File lib/spok.rb, line 116 def workdays(calendar = Spok.default_calendar) (@start_date..@end_date).to_a.delete_if{ |date| Workday.restday?(date, calendar: calendar) } end
Public: Returns a Float with the number of years in the Spok
.
Examples
spok.years_count # => 1.6
# File lib/spok.rb, line 274 def years_count ((@end_date - @start_date).to_f / 365).to_f.round(1) end
Private Instance Methods
# File lib/spok.rb, line 318 def as_integer(date) date.strftime(DATE_FORMAT).to_i end
# File lib/spok.rb, line 314 def as_string(date) date.strftime(DATE_FORMAT) end
# File lib/spok.rb, line 322 def validate! raise ArgumentError, 'Start date must be present.' unless @start_date raise ArgumentError, 'End date must be present.' unless @end_date if @start_date > @end_date raise ArgumentError, "End date (#{@end_date}) must be greater or equal to start date (#{@start_date})" end end