Class: Spok

Inherits:
Object
  • Object
show all
Defined in:
lib/spok.rb,
lib/spok/version.rb,
lib/spok/workday.rb,
lib/spok/calendars.rb

Overview

workdays and restdays.

Defined Under Namespace

Modules: Calendars, Workday

Constant Summary collapse

DATE_FORMAT =

Internal: String specifying format for dates in the period.

'%Y%m%d'.freeze
VERSION =
'2.1.8'.freeze
@@default_calendar =
:brazil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_date, end_date) ⇒ Spok

Public: Initialize a Spok.

start_date - Initial Date for the Spok. end_date - Final Date for Spok.



61
62
63
64
65
# File 'lib/spok.rb', line 61

def initialize(start_date, end_date)
  @start_date = start_date
  @end_date = end_date
  validate!
end

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



12
13
14
# File 'lib/spok.rb', line 12

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



12
13
14
# File 'lib/spok.rb', line 12

def start_date
  @start_date
end

Class Method Details

.add_calendar(name, path) ⇒ Object

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.



27
28
29
# File 'lib/spok.rb', line 27

def self.add_calendar(name, path)
  Spok::Calendars.add(name, path)
end

.default_calendar=(calendar) ⇒ Object



307
308
309
# File 'lib/spok.rb', line 307

def default_calendar=(calendar)
  class_variable_set(:@@default_calendar, calendar)
end

.holiday?(date, calendar: Spok.default_calendar) ⇒ Boolean

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.

Returns:

  • (Boolean)


178
179
180
# File 'lib/spok.rb', line 178

def self.holiday?(date, calendar: Spok.default_calendar)
  Workday.holiday?(date, calendar: calendar)
end

.last_workday(date, calendar: Spok.default_calendar) ⇒ Object

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)>


192
193
194
# File 'lib/spok.rb', line 192

def self.last_workday(date, calendar: Spok.default_calendar)
  Workday.last_workday(date, calendar: calendar)
end

.next_workday(date, calendar: Spok.default_calendar) ⇒ Object

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)>


206
207
208
# File 'lib/spok.rb', line 206

def self.next_workday(date, calendar: Spok.default_calendar)
  Workday.next_workday(date, calendar: calendar)
end

.parse(dates_string) ⇒ Object

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.



45
46
47
48
49
50
51
52
53
54
55
# 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

.restday?(date, calendar: Spok.default_calendar) ⇒ Boolean

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.

Returns:

  • (Boolean)


132
133
134
# File 'lib/spok.rb', line 132

def self.restday?(date, calendar: Spok.default_calendar)
  Workday.restday?(date, calendar: calendar)
end

.weekend?(date) ⇒ Boolean

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.

Returns:

  • (Boolean)


162
163
164
# File 'lib/spok.rb', line 162

def self.weekend?(date)
  Workday.weekend?(date)
end

.workday?(date, calendar: Spok.default_calendar) ⇒ Boolean

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.

Returns:

  • (Boolean)


148
149
150
# File 'lib/spok.rb', line 148

def self.workday?(date, calendar: Spok.default_calendar)
  Workday.workday?(date, calendar: calendar)
end

Instance Method Details

#==(other) ⇒ Object

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.



290
291
292
293
294
# File 'lib/spok.rb', line 290

def ==(other)
  other.class == self.class &&
    other.start_date == @start_date &&
    other.end_date == @end_date
end

#days_countObject

Public: Returns an Integer with the number of days in the Spok.

Examples

spok.days_count
# => 2


264
265
266
# File 'lib/spok.rb', line 264

def days_count
  (@end_date - @start_date).to_i
end

#end_date_as_integerObject

Public: Returns the Spok end date as an Integer.

Examples

spok.end_date_as_integer
# => 20120103


83
84
85
# File 'lib/spok.rb', line 83

def end_date_as_integer
  as_integer @end_date
end

#end_date_as_stringObject

Public: Returns the Spok end date as a String.

Examples

spok.end_date_as_string
# => "20120103"


103
104
105
# File 'lib/spok.rb', line 103

def end_date_as_string
  as_string @end_date
end

#one_day?Boolean

Public: Informs whether the Spok has just one day or not.

Examples

spok.one_day?
# => false

Returns a boolean.

Returns:

  • (Boolean)


254
255
256
# File 'lib/spok.rb', line 254

def one_day?
  @start_date == @end_date
end

#start_date_as_integerObject

Public: Returns the Spok start date as an Integer.

Examples

spok.start_date_as_integer
# => 20120101


73
74
75
# File 'lib/spok.rb', line 73

def start_date_as_integer
  as_integer @start_date
end

#start_date_as_stringObject

Public: Returns the Spok start date as a String.

Examples

spok.start_date_as_string
# => "20120101"


93
94
95
# File 'lib/spok.rb', line 93

def start_date_as_string
  as_string @start_date
end

#to_aObject

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]


231
232
233
# File 'lib/spok.rb', line 231

def to_a
  (@start_date..@end_date).to_a
end

#to_calendar(calendar = Spok.default_calendar) ⇒ Object

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 ...>


218
219
220
221
222
223
# 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

#to_rangeObject

Public: Returns a range containing the Dates in the Spok.

Examples

spok.to_range
# => Sun, 01 Jan 2012..Tue, 03 Jan 2012


302
303
304
# File 'lib/spok.rb', line 302

def to_range
  (@start_date..@end_date)
end

#to_sObject

Public: Returns a String containing the Spok start and end date separated by a dash.

Examples

spok.to_s
# => "20120101-20120103"


242
243
244
# File 'lib/spok.rb', line 242

def to_s
  "#{start_date_as_string}-#{end_date_as_string}"
end

#workdays(calendar = Spok.default_calendar) ⇒ Object

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]


116
117
118
# 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

#years_countObject

Public: Returns a Float with the number of years in the Spok.

Examples

spok.years_count
# => 1.6


274
275
276
# File 'lib/spok.rb', line 274

def years_count
  ((@end_date - @start_date).to_f / 365).to_f.round(1)
end