Class: Subjuster::UserInput

Inherits:
Object
  • Object
show all
Defined in:
lib/subjuster/user_input.rb

Overview

Handle User Input

Being a CLI tool, Subjuster is supposed to take input from ARGV This module will get those data via arguments to the constructor method.

For Example:

inputs = UserInput.new(source: ARGV[0], target: ARGV[1], adjustment_in_sec: ARGV[2])
inputs.valid? # => true / false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, target: nil, adjustment_in_sec: 0) ⇒ UserInput

Returns a new instance of UserInput.



16
17
18
19
20
# File 'lib/subjuster/user_input.rb', line 16

def initialize(source:, target: nil, adjustment_in_sec: 0)
  @source_filepath = File.expand_path(source || '')
  @target_filepath = target && File.expand_path(target) || "#{source_filepath}.modified.srt"
  @adjustment_in_sec = adjustment_in_sec
end

Instance Attribute Details

#adjustment_in_secObject (readonly)

Returns the value of attribute adjustment_in_sec.



15
16
17
# File 'lib/subjuster/user_input.rb', line 15

def adjustment_in_sec
  @adjustment_in_sec
end

#source_filepathObject (readonly)

Returns the value of attribute source_filepath.



15
16
17
# File 'lib/subjuster/user_input.rb', line 15

def source_filepath
  @source_filepath
end

#target_filepathObject (readonly)

Returns the value of attribute target_filepath.



15
16
17
# File 'lib/subjuster/user_input.rb', line 15

def target_filepath
  @target_filepath
end

Instance Method Details

#valid?Boolean

Validates the source file, if it exists

Valid condition

inputs = UserInput.new(source: ARGV[0], target: ARGV[1], adjustment_in_sec: ARGV[2])
inputs.valid? # => true

Invalid condition

inputs = UserInput.new(source: ARGV[0], target: ARGV[1], adjustment_in_sec: ARGV[2])
inputs.valid? # => false

Returns:

  • (Boolean)


34
35
36
# File 'lib/subjuster/user_input.rb', line 34

def valid?
  File.exist?(source_filepath)
end

#validate!Object

Raises:



38
39
40
# File 'lib/subjuster/user_input.rb', line 38

def validate!
  raise InputError, "Invalid file: #{source_filepath}" unless valid?
end