Class: ShiftSubtitles::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/shift_subtitles/operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#actionObject (readonly)

Returns the value of attribute action.



5
6
7
# File 'lib/shift_subtitles/operation.rb', line 5

def action
  @action
end

#timeObject (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_differenceObject



41
42
43
# File 'lib/shift_subtitles/operation.rb', line 41

def negative_time_difference
  -time_difference
end

#seconds_differenceObject



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_differenceObject



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

Returns:

  • (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

Returns:

  • (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