Module: Flott::EnvironmentMixin

Includes:
FilenameMixin
Included in:
Environment
Defined in:
lib/flott.rb

Overview

This module can be included into classes that should act as an environment for Flott templates. An instance variable @__output__ (EnvironmentMixin#output) should hold an output object, that responds to the #<< method, usually an IO object. If no initialize method is defined in the including class, EnvironmentMixin#initialize uses STDOUT as this output object.

If the class has its own initialize method, the environment can be initialized with super(output, escape) class Environment

  include EnvironmentMixin
  def initialize(output, escape)
    super(output, escape)
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FilenameMixin

#interpret_filename_as_page

Instance Attribute Details

#escapeObject

The escape object for this Environment object.



237
238
239
# File 'lib/flott.rb', line 237

def escape
  @escape
end

#page_cacheObject

If the currently evaluated Template originated from a Flott::Cache this method returns it, otherwise nil is returned.



241
242
243
# File 'lib/flott.rb', line 241

def page_cache
  @page_cache
end

#templateObject

The template that was evaluated in this environment last.



244
245
246
# File 'lib/flott.rb', line 244

def template
  @template
end

Instance Method Details

#[](name) ⇒ Object

Returns the instance variable name. The leading ‘@’ can be omitted in name.



267
268
269
270
271
# File 'lib/flott.rb', line 267

def [](name)
  name = name.to_s
  name = "@#{name}" unless name[0] == ?@
  instance_variable_get name
end

#[]=(name, value) ⇒ Object

Sets the instance variable name to value. The leading ‘@’ can be omitted in name.



275
276
277
278
279
# File 'lib/flott.rb', line 275

def []=(name, value)
  name = name.to_s
  name = "@#{name}" unless name[0] == ?@
  instance_variable_set name, value
end

#function(id, opts = {}, &block) ⇒ Object Also known as: fun

Creates a function (actually, a singleton method) id from the block block on this object, that can be called later in the template:

[function :fac do |n|
  if n < 2
    1
  else
    n * fac(n - 1)
  end
end]
fac(10) = [=fac(10)]


291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/flott.rb', line 291

def function(id, opts = {}, &block)
  sc = class << self; self; end
  if opts[:memoize]
    cache = {}
    sc.instance_eval do
      define_method(id) do |*args|
        if cache.key?(args)
          cache[args]
        else
          cache[args] = block[args]
        end
      end
    end
  else
    sc.instance_eval { define_method(id, &block) }
  end
  nil
end

#initialize(output = STDOUT, escape = Flott::Parser::HTML_ESCAPE) ⇒ Object

Creates an Environment object, that outputs to output. The default ouput object is STDOUT, but any object that responds to #<< will do. escape is a object that responds to #call (usually a Proc instance), and given a string, returns an escaped version of the string as an result. escape defaults to Flott::Parser::HTML_ESCAPE.



219
220
221
222
# File 'lib/flott.rb', line 219

def initialize(output = STDOUT, escape = Flott::Parser::HTML_ESCAPE)
  @__output__ = output
  @__escape__ = escape
end

#memoize(id) ⇒ Object

Memoize method with id id, if called.



313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/flott.rb', line 313

def memoize(id)
  cache = {}
  old_method = method(id)
  sc = class << self; self; end
  sc.send(:define_method, id) do |*args|
    if cache.key?(args)
      cache[args]
    else
      cache[args] = old_method.call(*args)
    end
  end
end

#outputObject

The output object for this Environment object. It should respond to the #<< method of appending strings.



226
227
228
# File 'lib/flott.rb', line 226

def output
  @__output__
end

#output=(output) ⇒ Object

Sets the output object for this Environment object, to output. It should respond to the #<< method of appending strings.



232
233
234
# File 'lib/flott.rb', line 232

def output=(output)
  @__output__ = output
end

#rootdirObject

Returns the root directory of this environment, it should be constant during the whole evaluation.



248
249
250
# File 'lib/flott.rb', line 248

def rootdir
  @__rootdir__
end

#update(hash) ⇒ Object

Updates the instance variables of this environment with values from hash.



260
261
262
263
# File 'lib/flott.rb', line 260

def update(hash)
  hash.each { |name, value| self[name] = value }
  self
end

#workdirObject

Returns the current work directory of this environment. This value changes during evaluation of a template.



254
255
256
# File 'lib/flott.rb', line 254

def workdir
  @__workdir__ or raise EvalError, "workdir was undefined"
end