Class: HoursToSeconds
- Inherits:
-
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 =
'1.0.15'
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(i = nil, run_already = true) ⇒ HoursToSeconds
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]
#
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_interface ⇒ Object
#
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_silent ⇒ Object
104
105
106
|
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 104
def be_silent
@be_verbose = false
end
|
#be_verbose? ⇒ Boolean
111
112
113
|
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 111
def be_verbose?
@be_verbose
end
|
#display_result ⇒ Object
Also known as:
display, output, report
146
147
148
|
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 146
def display_result
e to_s if be_verbose? end
|
#n_milliseconds ⇒ Object
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
118
119
120
|
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 118
def n_seconds?
@n_seconds
end
|
#reset ⇒ Object
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 50
def reset
@format = ''
@be_verbose = true
end
|
#run ⇒ Object
155
156
157
|
# File 'lib/hours_to_seconds/hours_to_seconds.rb', line 155
def run
display_result end
|
#
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? 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') end
@format = i 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 _ += @splitted[1].to_i * 60 _ = _.to_f
_ += @splitted[2].to_f @n_seconds = _
end
|
#to_s ⇒ Object
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
|