Class: TimeKeeper::CustomTime

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/time_keeper/custom_time.rb

Constant Summary collapse

TIME_FORMAT_REGEX =
/^((\d){1}|(\d){2})((:(\d){2}){1})?([ ])*(am|pm)?$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_in_str) ⇒ CustomTime

Returns a new instance of CustomTime.



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

def initialize(time_in_str)
  @time_in_str = time_in_str.downcase.strip
end

Instance Attribute Details

#time_in_strObject

Returns the value of attribute time_in_str.



2
3
4
# File 'lib/time_keeper/custom_time.rb', line 2

def time_in_str
  @time_in_str
end

Instance Method Details

#in_hoursObject



29
30
31
32
# File 'lib/time_keeper/custom_time.rb', line 29

def in_hours
  raise "Please parse the time before extracting in hours" unless @parsed_time
  @parsed_time.hour + ((@parsed_time.min.to_f / 60) * 100).round.to_f/100
end

#in_minutesObject



24
25
26
27
# File 'lib/time_keeper/custom_time.rb', line 24

def in_minutes
  raise "Please parse the time before extracting in minutes" unless @parsed_time
  @parsed_time.hour * 60 + @parsed_time.min
end

#in_secondsObject



19
20
21
22
# File 'lib/time_keeper/custom_time.rb', line 19

def in_seconds
  raise "Please parse the time before extracting in seconds" unless @parsed_time
  @parsed_time.hour * 3600 + @parsed_time.min * 60
end

#parseObject



14
15
16
17
# File 'lib/time_keeper/custom_time.rb', line 14

def parse
  @parsed_time = TimeKeeper::Parser::Base.build(@time_in_str).parse
  self
end

#to_sObject



34
35
36
37
38
# File 'lib/time_keeper/custom_time.rb', line 34

def to_s
  h = (self.in_minutes.to_i / 60)
  m = (self.in_minutes.to_i % 60)
  "%.2d:%.2d" % [h,m]
end