Class: AudioGlue::Template

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/audio_glue/template.rb,
lib/audio_glue/template/head_context.rb

Overview

Represents an audio template. Every particular template is a subclass of 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 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.

Examples:

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.

Defined Under Namespace

Classes: HeadContext

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables = {}) ⇒ Template

Returns a new instance of Template.

Parameters:

  • variables (Hash) (defaults to: {})

    hash of parameters which can be used as instance variables in body statement of .glue template.



84
85
86
87
88
# File 'lib/audio_glue/template.rb', line 84

def initialize(variables = {})
  variables.each do |var, value|
    instance_variable_set("@#{var}", value)
  end
end

Class Attribute Details

.body_procObject

Returns the value of attribute body_proc.



44
45
46
# File 'lib/audio_glue/template.rb', line 44

def body_proc
  @body_proc
end

.channelsObject

Returns the value of attribute channels.



44
45
46
# File 'lib/audio_glue/template.rb', line 44

def channels
  @channels
end

.formatObject

Returns the value of attribute format.



44
45
46
# File 'lib/audio_glue/template.rb', line 44

def format
  @format
end

.pathObject

Returns the value of attribute path.



44
45
46
# File 'lib/audio_glue/template.rb', line 44

def path
  @path
end

.rateObject

Returns the value of attribute rate.



44
45
46
# File 'lib/audio_glue/template.rb', line 44

def rate
  @rate
end

Class Method Details

.body { ... } ⇒ void

This method returns an undefined value.

Process the body block in a .glue template.

Yields:

  • block which will be executed in context of BodyContext object. It should define body of audio template.



63
64
65
# File 'lib/audio_glue/template.rb', line 63

def self.body(&block)
  self.body_proc = block
end

.head { ... } ⇒ void

This method returns an undefined value.

Process the head block in a .glue template.

Yields:

  • block which will be executed in context of HeadContext object.



53
54
55
# File 'lib/audio_glue/template.rb', line 53

def self.head(&block)
  HeadContext.new(self).instance_eval(&block)
end

.inspectString

Redefine the inspect method to provide more information when the template is an anonymous class.

Returns:

  • (String)


71
72
73
74
75
76
77
78
79
# 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

Instance Method Details

#build_snippet_packetAudioGlue::SnippetPacket

Execute the body of the template to build a snippet packet.



93
94
95
96
97
# File 'lib/audio_glue/template.rb', line 93

def build_snippet_packet
  @__packet__ = SnippetPacket.new(format, rate, channels)
  instance_eval(&body_proc)
  @__packet__
end

#inspectString

Redefine inspect to provide more information if the class of the template object is anonymous.

Returns:

  • (String)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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