Class: Vidibus::Encoder::Util::Flags

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vidibus/encoder/util/flags.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Flags

Initialize a flags object. One option is required to pass validation:

:base [Vidibus::Encoder::Base] The encoder object



13
14
15
# File 'lib/vidibus/encoder/util/flags.rb', line 13

def initialize(options = {})
  @base = options[:base]
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



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

def base
  @base
end

Instance Method Details

#cleanup(recipe) ⇒ Object

Remove empty placeholders.



82
83
84
# File 'lib/vidibus/encoder/util/flags.rb', line 82

def cleanup(recipe)
  recipe.gsub(/%\{[^\{]+\}/, '').gsub(/ +/, ' ')
end

#render(recipe) ⇒ Object

This method turns the recipe into a command string by replacing all placeholders and removing empty ones.

If a flag handler is defined for a placeholder and the profile setting is present, the flag handler will be called.

Examples:

base = Vidibus::Encoder::Base.new
flags = Vidibus::Encoder::Util::Flags.new(:base => base)
profile = Vidibus::Encoder::Util::Profile.new(:base => base)
encoder.instance_variable_set('@profile', profile)

recipe = 'some %{thing}'

# Without a matching profile setting
flags.render(recipe)
 # => 'some '

# With a matching profile setting
encoder.profile.settings[:thing] = 'beer'
flags.render(recipe)
# => 'some beer'

# With a matching profile setting and flag handler
encoder.profile.settings[:thing] = 'beer'
encoder.class.flag(:thing) { |value| "cold #{value}" }
flags.render(recipe)
# => 'some cold beer'


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vidibus/encoder/util/flags.rb', line 51

def render(recipe)
  recipe = recipe.gsub(/%\{([^\{]+)\}/) do |match|
    flag = $1.to_sym
    value = base.profile.try!(flag)
    if value
      if handler = base.class.registered_flags[flag]
        match = base.instance_exec(value, &handler)
      else
        match = value
      end
    end
    match
  end
  recipe = render_input(recipe)
  recipe = render_output(recipe)
  cleanup(recipe)
end

#render_input(recipe) ⇒ Object

Replace %input placeholder in recipe.



70
71
72
# File 'lib/vidibus/encoder/util/flags.rb', line 70

def render_input(recipe)
  recipe % {:input => %("#{base.input}")}
end

#render_output(recipe) ⇒ Object

Replace %output placeholder in recipe.



75
76
77
78
79
# File 'lib/vidibus/encoder/util/flags.rb', line 75

def render_output(recipe)
  return recipe unless base.input && base.output
  output = base.tmp.join(base.output.file_name)
  recipe % {:output => %("#{output}")}
end

#validateObject

Ensure that the base attribute is around.

Raises:



18
19
20
# File 'lib/vidibus/encoder/util/flags.rb', line 18

def validate
  raise(FlagError, 'Define a base class for flags') unless base
end