Class: Workpattern::Week

Inherits:
Object
  • Object
show all
Defined in:
lib/workpattern/week.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, finish, type = 1, hours_per_day = 24) ⇒ Week


7
8
9
10
11
12
13
14
15
# File 'lib/workpattern/week.rb', line 7

def initialize(start,finish,type=1,hours_per_day=24)
  @hours_per_day = hours_per_day
  @start=DateTime.new(start.year,start.month,start.day)
  @finish=DateTime.new(finish.year,finish.month,finish.day)
  @values = Array.new(6)
  0.upto(6) do |i| 
    @values[i] = working_day * type
  end
end

Instance Attribute Details

#finishObject

Returns the value of attribute finish


5
6
7
# File 'lib/workpattern/week.rb', line 5

def finish
  @finish
end

#hours_per_dayObject

Returns the value of attribute hours_per_day


5
6
7
# File 'lib/workpattern/week.rb', line 5

def hours_per_day
  @hours_per_day
end

#startObject

Returns the value of attribute start


5
6
7
# File 'lib/workpattern/week.rb', line 5

def start
  @start
end

#totalObject

Returns the value of attribute total


5
6
7
# File 'lib/workpattern/week.rb', line 5

def total
  @total
end

#valuesObject

Returns the value of attribute values


5
6
7
# File 'lib/workpattern/week.rb', line 5

def values
  @values
end

#week_totalObject

Returns the value of attribute week_total


5
6
7
# File 'lib/workpattern/week.rb', line 5

def week_total
  @week_total
end

Instance Method Details

#<=>(other_week) ⇒ Object


17
18
19
20
21
22
23
24
25
# File 'lib/workpattern/week.rb', line 17

def <=>(other_week)
  if self.start < other_week.start
    return -1
  elsif self.start == other_week.start
    return 0
  else
    return 1
  end      
end

#calc(start_date, duration, midnight = false) ⇒ Object


54
55
56
57
58
59
# File 'lib/workpattern/week.rb', line 54

def calc(start_date,duration, midnight=false)
  return start_date,duration,false if duration==0
  return add(start_date,duration) if duration > 0
  return subtract(self.start,duration, midnight) if (self.total==0) && (duration <0)
  return subtract(start_date,duration, midnight) if duration <0  
end

#diff(start_date, finish_date) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/workpattern/week.rb', line 70

def diff(start_date,finish_date)
  start_date,finish_date=finish_date,start_date if ((start_date <=> finish_date))==1
  
  if (start_date.jd==finish_date.jd) 
    duration, start_date=diff_in_same_day(start_date, finish_date)
  elsif (finish_date.jd<=self.finish.jd)
    duration, start_date=diff_in_same_weekpattern(start_date,finish_date)
  else 
    duration, start_date=diff_beyond_weekpattern(start_date,finish_date)
  end
  return duration, start_date

end

#duplicateObject


48
49
50
51
52
# File 'lib/workpattern/week.rb', line 48

def duplicate()
  duplicate_week=Week.new(self.start,self.finish)
  0.upto(6).each do |i| duplicate_week.values[i] = @values[i] end
  return duplicate_week
end

#resting?(date) ⇒ Boolean


66
67
68
# File 'lib/workpattern/week.rb', line 66

def resting?(date)
  !working?(date)
end

#working?(date) ⇒ Boolean


61
62
63
64
# File 'lib/workpattern/week.rb', line 61

def working?(date)
  return true if bit_pos_time(date) & @values[date.wday] > 0
  false
end

#workpattern(days, from_time, to_time, type) ⇒ Object


42
43
44
45
46
# File 'lib/workpattern/week.rb', line 42

def workpattern(days,from_time,to_time,type)
  DAYNAMES[days].each do |day| 
    type==1 ? work_on_day(day,from_time,to_time) : rest_on_day(day,from_time,to_time)
  end
end