class AudioGlue::Template
Represents an audio template. Every particular template is a subclass of {AudioGlue::Template}. Quite often the classes can be anonymous. (That’s why inspect
is redefined to provide more information.)
The Template
class owns format
, rate
, channels
, and also a block used to create an {AudioGlue::SnippetPacket} (“render”, in terms of the view templates).
A Template
instance differs from the Template
class. It has instance variables, that can be used in the body
block.
@example
class HiTemplate < AudioGlue::Template # Output file information head do format :mo3 rate 22050 channels 2 end # Block to "render" template body do - file('/hi.mp3') if @with_smalltalk - url('http://say.it/how-are-you.mp3') end end end # Create an instance of Template. We wonder how our friend is doing so # we pass ":with_smalltalk => true", to add a remote URL snippet. template = HiTemplate.new(:with_smalltalk => true) # Let's create a snippet packet: packet = template.build_snippet_packet => # <AudioGlue::SnippetPacket ..> # Now we can pass a snippet packet to the adapter to build output audio.
Attributes
Public Class Methods
Process the body
block in a .glue
template.
@yield block which will be executed in context of
{AudioGlue::Template::BodyContext} object. It should define body of audio template.
@return [void]
# File lib/audio_glue/template.rb, line 63 def self.body(&block) self.body_proc = block end
Process the head
block in a .glue
template.
@yield block which will be executed in context of
{AudioGlue::Template::HeadContext} object.
@return [void]
# File lib/audio_glue/template.rb, line 53 def self.head(&block) HeadContext.new(self).instance_eval(&block) end
Redefine the inspect
method to provide more information when the template is an anonymous class.
@return [String]
# File lib/audio_glue/template.rb, line 71 def self.inspect if self.name super else info = "<AudioGlue::Template(class)" info << " path=#{path.inspect}" if path info << ">" end end
@param variables [Hash] hash of parameters which can be used as instance
variables in +body+ statement of +.glue+ template.
# File lib/audio_glue/template.rb, line 84 def initialize(variables = {}) variables.each do |var, value| instance_variable_set("@#{var}", value) end end
Public Instance Methods
Execute the body of the template to build a snippet packet.
@return [AudioGlue::SnippetPacket]
# File lib/audio_glue/template.rb, line 93 def build_snippet_packet @__packet__ = SnippetPacket.new(format, rate, channels) instance_eval(&body_proc) @__packet__ end
Redefine inspect
to provide more information if the class of the template object is anonymous.
@return [String]
# File lib/audio_glue/template.rb, line 103 def inspect if self.class.name super else info = "<AudioGlue::Template" info << "(path=#{path.inspect})" if path instance_variables.each do |var| info << " #{var}=" info << instance_variable_get(var).inspect end info << ">" end end
Private Instance Methods
Create snippet.
@param type [Symbol] snippet type @param source [String] @param opts [Hash] any options supported by adapter
@return [AudioGlue::Snippet]
# File lib/audio_glue/template.rb, line 147 def create_snippet(type, source, opts = {}) Snippet.new(type, source, @__packet__, opts) end
Create a snippet (with the :file
type) for the given audio file.
@param file_path [String] path to an audio file in local file system
@return [AudioGlue::Snippet]
# File lib/audio_glue/template.rb, line 125 def file(file_path) create_snippet(:file, file_path) end
Create a snippet (with :url
type) for the given audio URL.
@param remote_url [String] remote location of audio file
@return [AudioGlue::Snippet]
# File lib/audio_glue/template.rb, line 135 def url(remote_url) create_snippet(:url, remote_url) end