Class: SubContext

Inherits:
Object show all
Defined in:
lib/volt/page/sub_context.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locals, context = nil) ⇒ SubContext

Returns a new instance of SubContext.



8
9
10
11
# File 'lib/volt/page/sub_context.rb', line 8

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Raises:

  • (NoMethodError)


17
18
19
20
21
22
23
24
25
26
# File 'lib/volt/page/sub_context.rb', line 17

def method_missing(method_name, *args, &block)
  method_name = method_name.to_s
  if @locals.has_key?(method_name)
    return @locals[method_name]
  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

Instance Attribute Details

#localsObject (readonly)

Returns the value of attribute locals.



6
7
8
# File 'lib/volt/page/sub_context.rb', line 6

def locals
  @locals
end

Instance Method Details

#respond_to?(method_name) ⇒ Boolean

Returns:



13
14
15
# File 'lib/volt/page/sub_context.rb', line 13

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