Class: ShiftSubtitles::Operation
- Inherits:
-
Object
- Object
- ShiftSubtitles::Operation
- Defined in:
- lib/shift_subtitles/operation.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
- #action_with_validation(action) ⇒ Object
-
#initialize(action, time) ⇒ Operation
constructor
A new instance of Operation.
- #negative_time_difference ⇒ Object
- #seconds_difference ⇒ Object
- #time_difference ⇒ Object
- #time_with_validation(time) ⇒ Object
- #valid_action?(action) ⇒ Boolean
- #valid_time?(time) ⇒ Boolean
Constructor Details
#initialize(action, time) ⇒ Operation
Returns a new instance of Operation.
7 8 9 10 |
# File 'lib/shift_subtitles/operation.rb', line 7 def initialize action, time @action = action_with_validation(action) @time = time_with_validation(time) end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
5 6 7 |
# File 'lib/shift_subtitles/operation.rb', line 5 def action @action end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
5 6 7 |
# File 'lib/shift_subtitles/operation.rb', line 5 def time @time end |
Instance Method Details
#action_with_validation(action) ⇒ Object
19 20 21 |
# File 'lib/shift_subtitles/operation.rb', line 19 def action_with_validation action valid_action?(action) ? action : raise("Please specify either 'add' or 'subtract' as the operation") end |
#negative_time_difference ⇒ Object
41 42 43 |
# File 'lib/shift_subtitles/operation.rb', line 41 def negative_time_difference -time_difference end |
#seconds_difference ⇒ Object
12 13 14 15 16 17 |
# File 'lib/shift_subtitles/operation.rb', line 12 def seconds_difference case action when "add" then return time_difference when "subtract" then return negative_time_difference end end |
#time_difference ⇒ Object
37 38 39 |
# File 'lib/shift_subtitles/operation.rb', line 37 def time_difference time.sub(',','.').to_f end |
#time_with_validation(time) ⇒ Object
23 24 25 26 27 |
# File 'lib/shift_subtitles/operation.rb', line 23 def time_with_validation time raise("Please specify a time to shift") unless time raise("Please specify a valid time to shift") unless valid_time?(time) time end |
#valid_action?(action) ⇒ Boolean
33 34 35 |
# File 'lib/shift_subtitles/operation.rb', line 33 def valid_action? action action =~ /\A(add|subtract)\Z/ end |
#valid_time?(time) ⇒ Boolean
29 30 31 |
# File 'lib/shift_subtitles/operation.rb', line 29 def valid_time? time time =~ /\A[0-9]{2},[0-9]{3}\Z/ end |