Class: PunchTime::TimeRecord

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ActiveModel::Validations
Defined in:
lib/punch_time/time_record.rb

Defined Under Namespace

Classes: Scale

Constant Summary collapse

MINUTE_SECONDS =
60
HOUR_MINUTES =
60
DAY_HOURS =
24

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&config) ⇒ TimeRecord

Returns a new instance of TimeRecord.

Parameters:

  • config (Hash)


28
29
30
# File 'lib/punch_time/time_record.rb', line 28

def initialize(&config)
  @configuration = Configuration.new(&config)
end

Instance Attribute Details

#end_timeObject

Returns the value of attribute end_time.



15
16
17
# File 'lib/punch_time/time_record.rb', line 15

def end_time
  @end_time
end

#start_timeObject

Returns the value of attribute start_time.



14
15
16
# File 'lib/punch_time/time_record.rb', line 14

def start_time
  @start_time
end

Instance Method Details

#night_overtime_work?Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
# File 'lib/punch_time/time_record.rb', line 105

def night_overtime_work?
  night_start_time = @configuration.night[:start_time].strftime('%H:%M:%S')
  end_time >
    DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{night_start_time}")
            .change(offset: @configuration.offset)
end

#overtime_work?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/punch_time/time_record.rb', line 98

def overtime_work?
  shift_out_time = @configuration.shift_out_time.strftime('%H:%M:%S')
  end_time > DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_out_time}")
                     .change(offset: @configuration.offset)
end

#punch(start_time, end_time) ⇒ Object

Parameters:

  • start_time (DateTime)
  • end_time (DateTime)

Raises:

  • (ArgumentError)


34
35
36
37
38
# File 'lib/punch_time/time_record.rb', line 34

def punch(start_time, end_time)
  self.start_time = start_time
  self.end_time = end_time
  raise ArgumentError, errors.messages unless valid?
end

#sum_night_workScale

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/punch_time/time_record.rb', line 41

def sum_night_work
  night_works = []
  night_start_time = @configuration.night[:start_time].strftime('%H:%M:%S')
  night_end_time = @configuration.night[:end_time].strftime('%H:%M:%S')
  start_time.to_date.upto(end_time.to_date) do |x|
    night_start = DateTime.parse("#{x.strftime('%Y-%m-%d')} #{night_start_time}")
                          .change(offset: @configuration.offset)
    night_end = DateTime.parse("#{x.next_day.strftime('%Y-%m-%d')} #{night_end_time}")
                        .change(offset: @configuration.offset)
    if end_time > night_end
      night_works.append(night_end - night_start)
    elsif (end_time - night_start).positive?
      night_works.append(end_time - night_start)
    end
  end
  night_work = night_works.inject(:+)
  convert_humanize(night_work)
end

#sum_over_workScale

Returns:



61
62
63
64
65
66
67
68
69
# File 'lib/punch_time/time_record.rb', line 61

def sum_over_work
  shift_out_time = @configuration.shift_out_time.strftime('%H:%M:%S')
  over_work = end_time -
              DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_out_time}")
                      .change(offset: @configuration.offset) -
              sum_night_work.rational

  convert_humanize(over_work)
end

#sum_tardyScale

Returns:



81
82
83
84
85
86
87
# File 'lib/punch_time/time_record.rb', line 81

def sum_tardy
  shift_in_time = @configuration.shift_in_time.strftime('%H:%M:%S')
  tardy = start_time -
          DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_in_time}")
                  .change(offset: @configuration.offset)
  convert_humanize(tardy)
end

#sum_workScale

Returns:



72
73
74
75
76
77
78
# File 'lib/punch_time/time_record.rb', line 72

def sum_work
  shift_in_time = @configuration.shift_in_time.strftime('%H:%M:%S')
  shift_in = DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_in_time}")
                     .change(offset: @configuration.offset)
  work = end_time - shift_in - sum_break
  convert_humanize(work)
end

#tardy?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
# File 'lib/punch_time/time_record.rb', line 90

def tardy?
  shift_in_time = @configuration.shift_in_time.strftime('%H:%M:%S')
  start_time >
    DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_in_time}")
            .change(offset: @configuration.offset)
end