Class: Vidibus::Encoder::Util::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/vidibus/encoder/util/output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @base = options[:base]
  @path = options[:path]
  make_dir
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



7
8
9
# File 'lib/vidibus/encoder/util/output.rb', line 7

def base
  @base
end

#pathObject

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

#dirObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


64
65
66
# File 'lib/vidibus/encoder/util/output.rb', line 64

def exist?
  File.exist?(path)
end

#file_nameObject

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_pathObject



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_dirObject

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_filesObject

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.message}")
  end
end

#present?Boolean

Return true if a path has been defined.

Returns:

  • (Boolean)


59
60
61
# File 'lib/vidibus/encoder/util/output.rb', line 59

def present?
  !!path
end

#to_sObject

Return the output path.



21
22
23
# File 'lib/vidibus/encoder/util/output.rb', line 21

def to_s
  file_path || path
end

#validateObject

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