Class: Mustache::Context
- Inherits:
-
Object
- Object
- Mustache::Context
- Defined in:
- lib/mustache/context.rb
Overview
A Context represents the context which a Mustache template is executed within. All Mustache tags reference keys in the Context.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Alias for ‘fetch`.
-
#[]=(name, value) ⇒ Object
Can be used to add a value to the context in a hash-like way.
-
#fetch(name, default = :__raise) ⇒ Object
Similar to Hash#fetch, finds a value by ‘name` in the context’s stack.
-
#has_key?(key) ⇒ Boolean
Do we know about a particular key? In other words, will calling ‘context` give us a result that was set.
-
#initialize(mustache) ⇒ Context
constructor
Expect to be passed an instance of ‘Mustache`.
-
#mustache_in_stack ⇒ Object
Find the first Mustache in the stack.
-
#partial(name) ⇒ Object
A {>partial} tag translates into a call to the context’s ‘partial` method, which would be this sucker right here.
-
#pop ⇒ Object
Removes the most recently added object from the context’s internal stack.
-
#push(new) ⇒ Object
(also: #update)
Adds a new object to the context’s internal stack.
Constructor Details
#initialize(mustache) ⇒ Context
Expect to be passed an instance of ‘Mustache`.
16 17 18 |
# File 'lib/mustache/context.rb', line 16 def initialize(mustache) @stack = [mustache] end |
Instance Method Details
#[](name) ⇒ Object
Alias for ‘fetch`.
78 79 80 |
# File 'lib/mustache/context.rb', line 78 def [](name) fetch(name, nil) end |
#[]=(name, value) ⇒ Object
Can be used to add a value to the context in a hash-like way.
context = “Chris”
73 74 75 |
# File 'lib/mustache/context.rb', line 73 def []=(name, value) push(name => value) end |
#fetch(name, default = :__raise) ⇒ Object
Similar to Hash#fetch, finds a value by ‘name` in the context’s stack. You may specify the default return value by passing a second parameter.
If no second parameter is passed (or raise_on_context_miss is set to true), will raise a ContextMiss exception on miss.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mustache/context.rb', line 96 def fetch(name, default = :__raise) @stack.each do |frame| # Prevent infinite recursion. next if frame == self # Is this frame a hash? hash = frame.respond_to?(:has_key?) if hash && frame.has_key?(name) return frame[name] elsif hash && frame.has_key?(name.to_s) return frame[name.to_s] elsif !hash && frame.respond_to?(name) return frame.__send__(name) end end if default == :__raise || mustache_in_stack.raise_on_context_miss? raise ContextMiss.new("Can't find #{name} in #{@stack.inspect}") else default end end |
#has_key?(key) ⇒ Boolean
Do we know about a particular key? In other words, will calling ‘context` give us a result that was set. Basically.
84 85 86 87 88 |
# File 'lib/mustache/context.rb', line 84 def has_key?(key) !!fetch(key) rescue ContextMiss false end |
#mustache_in_stack ⇒ Object
Find the first Mustache in the stack. If we’re being rendered inside a Mustache object as a context, we’ll use that one.
46 47 48 49 50 |
# File 'lib/mustache/context.rb', line 46 def mustache_in_stack @stack.detect do |frame| frame.is_a?(Mustache) end end |
#partial(name) ⇒ Object
A {>partial} tag translates into a call to the context’s ‘partial` method, which would be this sucker right here.
If the Mustache view handling the rendering (e.g. the view representing your profile page or some other template) responds to ‘partial`, we call it and use the result. Otherwise we render and compile the partial as its own view and return the result.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mustache/context.rb', line 27 def partial(name) # Look for any Mustaches in the stack. mustache = mustache_in_stack if mustache.respond_to? :partial # We found a mustache and it responds to `partial`, send it. mustache.render(mustache.partial(name), self) elsif mustache # We found a mustache without `partial`, use it to render. mustache.render_file(name, self) else # Can't find any staches, abort and use whatever we can.. raise "No Mustache views in stack." end end |
#pop ⇒ Object
Removes the most recently added object from the context’s internal stack.
Returns the Context.
65 66 67 68 |
# File 'lib/mustache/context.rb', line 65 def pop @stack.shift self end |
#push(new) ⇒ Object Also known as: update
Adds a new object to the context’s internal stack.
Returns the Context.
55 56 57 58 |
# File 'lib/mustache/context.rb', line 55 def push(new) @stack.unshift(new) self end |