Class: GBWorkDay::WorkWeek

Inherits:
Object
  • Object
show all
Defined in:
lib/gb_work_day/work_week.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work_days = 7) ⇒ WorkWeek

Returns a new instance of WorkWeek.

Parameters:

  • work_days (#to_i) (defaults to: 7)

    Amount of working days in a week. Default value is 7.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gb_work_day/work_week.rb', line 6

def initialize(work_days = 7)
  work_days = work_days.to_i
  week_start = 1
  raise ArgumentError, 'Work days have to be between 0 and 7!' unless work_days >= 0 && work_days <= 7
  @work_days_per_week = work_days
  @week_start = week_start
  @free_days_per_week = 7 - @work_days_per_week
  @work_days = []
  @work_days_per_week.times do
    day = week_start % 7
    @work_days << (day != 0 ? day : 7)
    week_start += 1
  end
  @work_days.sort!
  @free_days = (1..7).to_a - @work_days
end

Instance Attribute Details

#free_daysObject (readonly)

Returns the value of attribute free_days.



3
4
5
# File 'lib/gb_work_day/work_week.rb', line 3

def free_days
  @free_days
end

#free_days_per_weekObject (readonly)

Returns the value of attribute free_days_per_week.



3
4
5
# File 'lib/gb_work_day/work_week.rb', line 3

def free_days_per_week
  @free_days_per_week
end

#work_daysObject (readonly)

Returns the value of attribute work_days.



3
4
5
# File 'lib/gb_work_day/work_week.rb', line 3

def work_days
  @work_days
end

#work_days_per_weekObject (readonly)

Returns the value of attribute work_days_per_week.



3
4
5
# File 'lib/gb_work_day/work_week.rb', line 3

def work_days_per_week
  @work_days_per_week
end

Class Method Details

.currentObject



48
49
50
# File 'lib/gb_work_day/work_week.rb', line 48

def current
  Thread.current[:working_week] ||= self.new
end

.current=(new_week) ⇒ Object



52
53
54
# File 'lib/gb_work_day/work_week.rb', line 52

def current=(new_week)
  Thread.current[:working_week] = new_week if WorkWeek === new_week
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



39
40
41
# File 'lib/gb_work_day/work_week.rb', line 39

def ==(other) # :nodoc:
  work_days_per_week == other.work_days_per_week
end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


43
44
45
# File 'lib/gb_work_day/work_week.rb', line 43

def eql?(other) # :nodoc:
  work_days_per_week.eql?(other.work_days_per_week)
end

#free_day?(day) ⇒ Boolean

Check if given day is a work day

Parameters:

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/gb_work_day/work_week.rb', line 33

def free_day?(day)
  week_day = day.wday
  week_day = 7 if week_day == 0
  free_days.include? week_day
end

#work_day?(day) ⇒ Boolean

Check if given day is a work day

Parameters:

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/gb_work_day/work_week.rb', line 25

def work_day?(day)
  week_day = day.wday
  week_day = 7 if week_day == 0
  work_days.include? week_day
end