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

Public: Class responsible for dealing with periods of Dates, considering 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.0.1'.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


217
218
219
# File 'lib/spok.rb', line 217

def default_calendar=(calendar)
  class_variable_set(:@@default_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

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.


200
201
202
203
204
# File 'lib/spok.rb', line 200

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

174
175
176
# File 'lib/spok.rb', line 174

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)

164
165
166
# File 'lib/spok.rb', line 164

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]

141
142
143
# File 'lib/spok.rb', line 141

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

128
129
130
131
132
133
# File 'lib/spok.rb', line 128

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

212
213
214
# File 'lib/spok.rb', line 212

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"

152
153
154
# File 'lib/spok.rb', line 152

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

184
185
186
# File 'lib/spok.rb', line 184

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