Class: Spok

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

Overview

Public: Class responsible for dealing with periods of Dates, considering workdays and restdays.

Defined Under Namespace

Modules: Workday

Constant Summary collapse

DATE_FORMAT =

Internal: String specifying format for dates in the period.

'%Y%m%d'.freeze
VERSION =
'1.1.0'.freeze
@@default_calendar =
:brasil

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.


46
47
48
49
50
# File 'lib/spok.rb', line 46

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


10
11
12
# File 'lib/spok.rb', line 10

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date


10
11
12
# File 'lib/spok.rb', line 10

def start_date
  @start_date
end

Class Method Details

.default_calendar=(calendar) ⇒ Object


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

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.


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/spok.rb', line 30

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.


185
186
187
188
189
# File 'lib/spok.rb', line 185

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

159
160
161
# File 'lib/spok.rb', line 159

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

68
69
70
# File 'lib/spok.rb', line 68

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"

88
89
90
# File 'lib/spok.rb', line 88

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)

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

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

58
59
60
# File 'lib/spok.rb', line 58

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"

78
79
80
# File 'lib/spok.rb', line 78

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]

126
127
128
# File 'lib/spok.rb', line 126

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: :brasil).

Examples

spok.to_calendar(:brasil)
# => #<Spok:0x00007fbf122dba08 ...>

113
114
115
116
117
118
# File 'lib/spok.rb', line 113

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

197
198
199
# File 'lib/spok.rb', line 197

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"

137
138
139
# File 'lib/spok.rb', line 137

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: :brasil).

Examples

spok.workdays
# => [Mon, 02 Jan 2012, Tue, 03 Jan 2012]

101
102
103
# File 'lib/spok.rb', line 101

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

169
170
171
# File 'lib/spok.rb', line 169

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