Class: Nanoc::Int::Context Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/base/context.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides a context and a binding for use in filters such as the ERB and Haml ones.

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Context

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new context based off the contents of the hash.

Each pair in the hash will be converted to an instance variable and an instance method. For example, passing the hash ‘{ :foo => ’bar’ }‘ will cause `@foo` to have the value `“bar”`, and the instance method `#foo` to return the same value `“bar”`.

Examples:

Defining a context and accessing values


context = Nanoc::Int::Context.new(
  :name     => 'Max Payne',
  :location => 'in a cheap motel'
)
context.instance_eval do
  "I am #{name} and I am hiding #{@location}."
end
# => "I am Max Payne and I am hiding in a cheap motel."

Parameters:

  • hash (Hash)

    A list of key-value pairs to make available



26
27
28
29
30
31
32
33
34
35
# File 'lib/nanoc/base/context.rb', line 26

def initialize(hash)
  hash.each_pair do |key, value|
    # Build instance variable
    instance_variable_set('@' + key.to_s, value)

    # Define method
    metaclass = (class << self; self; end)
    metaclass.send(:define_method, key) { value }
  end
end

Instance Method Details

#get_bindingBinding

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a binding for this instance.

Returns:

  • (Binding)

    A binding for this instance



40
41
42
# File 'lib/nanoc/base/context.rb', line 40

def get_binding
  binding
end