Sha256: 236428d193b73f4376d3fa623f0324ce5571c241c735134bb0d3aefb2b62ca82

Contents?: true

Size: 978 Bytes

Versions: 14

Compression:

Stored size: 978 Bytes

Contents

# A sub context takes in a hash of local variables that should be available
# in front of the current context.  It basically proxies the local variables
# first, then failing those proxies the context.

class SubContext
  attr_reader :locals

  def initialize(locals, context=nil)
    @locals = locals.stringify_keys
    @context = context
  end

  def respond_to?(method_name)
    !!(@locals[method_name.to_s] || (@context && @context.respond_to?(method_name)) || super)
  end

  def method_missing(method_name, *args, &block)
    method_name = method_name.to_s
    if @locals.has_key?(method_name)
      obj = @locals[method_name]

      # TODORW: Might get a normal proc, flag internal procs
      if obj.is_a?(Proc)
        obj = obj.call(*args)
      end
      return obj
    elsif @context
      return @context.send(method_name, *args, &block)
    end

    raise NoMethodError.new("undefined method `#{method_name}' for \"#{self.inspect}\":#{self.class.to_s}")
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
volt-0.8.14 lib/volt/page/sub_context.rb
volt-0.8.13 lib/volt/page/sub_context.rb
volt-0.8.11 lib/volt/page/sub_context.rb
volt-0.8.10 lib/volt/page/sub_context.rb
volt-0.8.9 lib/volt/page/sub_context.rb
volt-0.8.8 lib/volt/page/sub_context.rb
volt-0.8.7 lib/volt/page/sub_context.rb
volt-0.8.6 lib/volt/page/sub_context.rb
volt-0.8.5 lib/volt/page/sub_context.rb
volt-0.8.4 lib/volt/page/sub_context.rb
volt-0.8.3 lib/volt/page/sub_context.rb
volt-0.8.2 lib/volt/page/sub_context.rb
volt-0.8.1 lib/volt/page/sub_context.rb
volt-0.8.0 lib/volt/page/sub_context.rb