Class: HoursToSeconds

Inherits:
Object
  • Object
show all
Defined in:
lib/hours_to_seconds/version/version.rb,
lib/hours_to_seconds/hours_to_seconds.rb,
lib/hours_to_seconds/www/embeddable_interface.rb

Overview

#

require ‘hours_to_seconds/www/embeddable_interface.rb’ include HoursToSeconds::EmbeddableInterface

#

Defined Under Namespace

Modules: EmbeddableInterface

Constant Summary collapse

VERSION =
#

VERSION

#
'1.0.15'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i = nil, run_already = true) ⇒ HoursToSeconds

#

initialize

#


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 30

def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_format(i)
  case run_already
  when :dont_run_yet, :do_not_run_yet
    run_already = false
  when :be_silent
    run_already = true
    @be_verbose = false
  end
  start_conversion
  run if run_already
end

Class Method Details

.[](i, optional_be_quiet = false) ⇒ Object

#

HoursToSeconds[]

The following asks this question:

"How many seconds does a 24 hours day have?"

Usage example:

x = HoursToSeconds[2, :be_quiet] # => 7200
#


171
172
173
174
175
176
177
178
179
180
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 171

def self.[](i, optional_be_quiet = false)
  _ = new(i, :dont_run_yet)
  case optional_be_quiet
  when :be_quiet, :be_silent
    optional_be_quiet = true
  end
  _.be_silent if optional_be_quiet
  _.run
  return _.n_seconds
end

.embeddable_interfaceObject

#

HoursToSeconds.embeddable_interface

#


51
52
53
54
55
# File 'lib/hours_to_seconds/www/embeddable_interface.rb', line 51

def self.embeddable_interface
  object = Object.new
  object.extend(::HoursToSeconds::EmbeddableInterface)
  return object
end

Instance Method Details

#be_silentObject

#

be_silent

#


104
105
106
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 104

def be_silent
  @be_verbose = false
end

#be_verbose?Boolean

#

be_verbose?

#

Returns:

  • (Boolean)


111
112
113
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 111

def be_verbose?
  @be_verbose
end

#display_resultObject Also known as: display, output, report

#

display_result

#


146
147
148
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 146

def display_result
  e to_s if be_verbose? # Simply call to_s here, but only if we are verbose.
end

#n_millisecondsObject

#

n_milliseconds

#


89
90
91
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 89

def n_milliseconds
  return @n_seconds.to_i * 1000
end

#n_seconds?Boolean Also known as: n_seconds, seconds

#

n_seconds?

#

Returns:

  • (Boolean)


118
119
120
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 118

def n_seconds?
  @n_seconds
end

#resetObject

#

reset (reset tag)

#


50
51
52
53
54
55
56
57
58
59
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 50

def reset
  # ======================================================================= #
  # === @format
  # ======================================================================= #
  @format = ''
  # ======================================================================= #
  # === @be_verbose
  # ======================================================================= #
  @be_verbose = true
end

#runObject

#

run

#


155
156
157
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 155

def run
  display_result # We only display it for now.
end

#set_format(i = nil) ⇒ Object

#

set_format

We have to make sure that the format will be proper, as otherwise further calculations may fail/be incorrect.

#


129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 129

def set_format(i = nil)
  i = '02:28:07.97' if i.nil? # Default.
  i = i.to_s.dup
  if i.end_with? 's'
    i.chop!
  end
  i.tr!('m',':') if i.include? 'm'
  i.tr!('h',':') if i.include? 'h'
  if i.count(':') == 2 and i =~ /^\d:/
    i.prepend('0') # Correct input such as '1:32:48.2' here.
  end
  @format = i # @format is now something like '02:28:07.97'.
end

#start_conversion(i = @format) ⇒ Object Also known as: convert, process

#

start_conversion

This is called from the method run(). It will set the ivar @n_seconds.

The method will assign to the instance variable @splitted.

#


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

def start_conversion(
    i = @format
  )
  @splitted = i.split(':')
  _  = @splitted[0].to_i * 60 * 60 # Add hours here.
  _ += @splitted[1].to_i * 60      # Add minutes here.
  _ = _.to_f
  # ======================================================================= #
  # Next add "n seconds". However had, this can be a String
  # such as "11.91". We don't want to lose the fraction
  # part though.
  # ======================================================================= #
  _ += @splitted[2].to_f           # Add Seconds here.
  @n_seconds = _
end

#to_sObject

#

to_s

#


96
97
98
99
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 96

def to_s
  result = sfancy(@format)+' sind '+simp(@n_seconds.to_s)+' Sekunden.'
  return result
end