Sha256: 8bc20325bb2a0447a3f16b700aad3f8ff93fa5bbc0409c587a5d2a9de51568fd

Contents?: true

Size: 1.35 KB

Versions: 31

Compression:

Stored size: 1.35 KB

Contents

# encoding: utf-8

module Nanoc

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

    # 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"`.
    #
    # @param [Hash] hash A list of key-value pairs to make available
    #
    # @example Defining a context and accessing values
    #
    #     context = Nanoc::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."
    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

    # Returns a binding for this instance.
    #
    # @return [Binding] A binding for this instance
    def get_binding
      binding
    end

  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
nanoc-3.7.3 lib/nanoc/base/context.rb
nanoc-3.7.2 lib/nanoc/base/context.rb
nanoc-3.7.1 lib/nanoc/base/context.rb
nanoc-3.7.0 lib/nanoc/base/context.rb
nanoc-3.6.11 lib/nanoc/base/context.rb
nanoc-3.6.10 lib/nanoc/base/context.rb
nanoc-3.6.9 lib/nanoc/base/context.rb
nanoc-3.6.8 lib/nanoc/base/context.rb
nanoc-3.6.7 lib/nanoc/base/context.rb
nanoc-3.6.6 lib/nanoc/base/context.rb
nanoc-3.6.5 lib/nanoc/base/context.rb
nanoc-3.6.4 lib/nanoc/base/context.rb
nanoc-3.6.3 lib/nanoc/base/context.rb
nanoc-3.6.2 lib/nanoc/base/context.rb
nanoc-3.6.1 lib/nanoc/base/context.rb
nanoc-3.6.0 lib/nanoc/base/context.rb
nanoc-3.5.0 lib/nanoc/base/context.rb
nanoc-3.5.0b2 lib/nanoc/base/context.rb
nanoc-3.5.0b1 lib/nanoc/base/context.rb
nanoc-3.4.3 lib/nanoc/base/context.rb