Sha256: 79cf6d93d7a694d5f9555ac6f26e0e8c5aee29101ce6a8c0f26116b8ff191105

Contents?: true

Size: 1.76 KB

Versions: 14

Compression:

Stored size: 1.76 KB

Contents

module Nanoc::Int
  # Provides a context and a binding for use in filters such as the ERB and
  # Haml ones.
  #
  # @api private
  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::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."
    def initialize(hash)
      hash.each_pair do |key, value|
        instance_variable_set('@' + key.to_s, value)
      end
    end

    # Returns a binding for this instance.
    #
    # @return [Binding] A binding for this instance
    # rubocop:disable Style/AccessorMethodName
    def get_binding
      binding
    end
    # rubocop:enable Style/AccessorMethodName

    def method_missing(method, *args, &blk)
      ivar_name = '@' + method.to_s
      if instance_variable_defined?(ivar_name)
        instance_variable_get(ivar_name)
      else
        super
      end
    end

    def respond_to_missing?(method, include_all)
      ivar_name = '@' + method.to_s
      instance_variable_defined?(ivar_name) || super
    end

    def include(mod)
      metaclass = class << self; self; end
      metaclass.instance_eval { include(mod) }
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
nanoc-4.7.9 lib/nanoc/base/entities/context.rb
nanoc-4.7.8 lib/nanoc/base/entities/context.rb
nanoc-4.7.7 lib/nanoc/base/entities/context.rb
nanoc-4.7.6 lib/nanoc/base/entities/context.rb
nanoc-4.7.5 lib/nanoc/base/entities/context.rb
nanoc-4.7.4 lib/nanoc/base/entities/context.rb
nanoc-4.7.3 lib/nanoc/base/entities/context.rb
nanoc-4.7.2 lib/nanoc/base/entities/context.rb
nanoc-4.7.1 lib/nanoc/base/entities/context.rb
nanoc-4.7.0 lib/nanoc/base/entities/context.rb
nanoc-4.6.4 lib/nanoc/base/entities/context.rb
nanoc-4.6.3 lib/nanoc/base/entities/context.rb
nanoc-4.6.2 lib/nanoc/base/entities/context.rb
nanoc-4.6.1 lib/nanoc/base/entities/context.rb