Class: Vidibus::Encoder::Util::Output
- Inherits:
-
Object
- Object
- Vidibus::Encoder::Util::Output
- Defined in:
- lib/vidibus/encoder/util/output.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #base_name(str = file_name) ⇒ Object
-
#dir ⇒ Object
Return the directory name from path.
-
#directory? ⇒ Boolean
Return true if path is a directory.
-
#exist? ⇒ Boolean
Return true if path exists.
-
#file_name ⇒ Object
Extract the file name from given path or input file.
- #file_path ⇒ Object
-
#initialize(options) ⇒ Output
constructor
Initialize an output object.
-
#make_dir ⇒ Object
Create output directory.
-
#move_files ⇒ Object
Move files from tmp folder to output folder.
-
#present? ⇒ Boolean
Return true if a path has been defined.
-
#to_s ⇒ Object
Return the output path.
-
#validate ⇒ Object
Ensure that a path is given.
Constructor Details
#initialize(options) ⇒ Output
Initialize an output object. Two options are required:
:base [Vidibus::Encoder::Base] The encoder object :path [String] The path to the output file or directory
14 15 16 17 18 |
# File 'lib/vidibus/encoder/util/output.rb', line 14 def initialize() @base = [:base] @path = [:path] make_dir end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
7 8 9 |
# File 'lib/vidibus/encoder/util/output.rb', line 7 def base @base end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/vidibus/encoder/util/output.rb', line 6 def path @path end |
Instance Method Details
#base_name(str = file_name) ⇒ Object
54 55 56 |
# File 'lib/vidibus/encoder/util/output.rb', line 54 def base_name(str = file_name) str[/([^\/]+)\.[^\.]+$/, 1] end |
#dir ⇒ Object
Return the directory name from path.
26 27 28 |
# File 'lib/vidibus/encoder/util/output.rb', line 26 def dir @dir ||= directory? ? path : File.dirname(path) end |
#directory? ⇒ Boolean
Return true if path is a directory
69 70 71 |
# File 'lib/vidibus/encoder/util/output.rb', line 69 def directory? File.directory?(path) end |
#exist? ⇒ Boolean
Return true if path exists
64 65 66 |
# File 'lib/vidibus/encoder/util/output.rb', line 64 def exist? File.exist?(path) end |
#file_name ⇒ Object
Extract the file name from given path or input file.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/vidibus/encoder/util/output.rb', line 31 def file_name path[/([^\/]+\.[^\/]+)$/, 1] || begin if base.input base_name(base.input.path).tap do |name| if base.profile name << ".#{base.profile.file_extension}" if base.profile.name && base.profile.name.to_s != 'default' name.gsub!(/(\.[^\.]+)$/, "-#{base.profile.name}\\1") end else raise(OutputError, 'Could not determine file name because the current profile does not define a file extension') end end else raise(OutputError, 'Could not determine file name from input or output path') end end end |
#file_path ⇒ Object
50 51 52 |
# File 'lib/vidibus/encoder/util/output.rb', line 50 def file_path File.join(dir, file_name) if file_name end |
#make_dir ⇒ Object
Create output directory
79 80 81 |
# File 'lib/vidibus/encoder/util/output.rb', line 79 def make_dir FileUtils.mkdir_p(dir) unless exist? end |
#move_files ⇒ Object
Move files from tmp folder to output folder.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vidibus/encoder/util/output.rb', line 84 def move_files begin files = Dir["#{base.tmp}/*"] files.each do |file| FileUtils.mv(file, dir) file.gsub!(base.tmp.to_s, dir) end rescue => e raise("Moving output files from #{base.tmp} to #{path} failed: #{e.}") end end |
#present? ⇒ Boolean
Return true if a path has been defined.
59 60 61 |
# File 'lib/vidibus/encoder/util/output.rb', line 59 def present? !!path end |
#to_s ⇒ Object
Return the output path.
21 22 23 |
# File 'lib/vidibus/encoder/util/output.rb', line 21 def to_s file_path || path end |
#validate ⇒ Object
Ensure that a path is given.
74 75 76 |
# File 'lib/vidibus/encoder/util/output.rb', line 74 def validate present? || raise(OutputError, 'No output defined') end |