Class: Braintrust::Vendor::Mustache::Context
- Inherits:
-
Object
- Object
- Braintrust::Vendor::Mustache::Context
- Defined in:
- lib/braintrust/vendor/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.
- #current ⇒ Object
-
#escape(value) ⇒ String
Allows customization of how Mustache escapes things.
-
#fetch(name, default = :__raise) ⇒ Object
Similar to Hash#fetch, finds a value by
namein the context's stack. -
#find(obj, key, default = nil) ⇒ Object
Finds a key in an object, using whatever method is most appropriate.
-
#has_key?(key) ⇒ Boolean
Do we know about a particular key? In other words, will calling
context[key]give us a result that was set. -
#initialize(mustache) ⇒ Context
constructor
Initializes a Mustache::Context.
-
#mustache_in_stack ⇒ Mustache
Find the first Mustache in the stack.
-
#partial(name, indentation = "") ⇒ Object
A {>partial} tag translates into a call to the context's
partialmethod, which would be this sucker right here. -
#pop ⇒ Context
Removes the most recently added object from the context's internal stack.
-
#push(new_obj) ⇒ Context
Adds a new object to the context's internal stack.
- #template_for_partial(partial) ⇒ Object
Constructor Details
#initialize(mustache) ⇒ Context
Initializes a Mustache::Context.
20 21 22 23 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 20 def initialize(mustache) @stack = [mustache] @partial_template_cache = {} end |
Instance Method Details
#[](name) ⇒ Object
Alias for fetch.
99 100 101 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 99 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"
94 95 96 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 94 def []=(name, value) push(name => value) end |
#current ⇒ Object
157 158 159 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 157 def current @stack.first end |
#escape(value) ⇒ String
Allows customization of how Mustache escapes things.
64 65 66 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 64 def escape(value) mustache_in_stack.escape(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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 117 def fetch(name, default = :__raise) @stack.each do |frame| # Prevent infinite recursion. next if frame == self value = find(frame, name, :__missing) return value if :__missing != value 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 |
#find(obj, key, default = nil) ⇒ Object
Finds a key in an object, using whatever method is most appropriate. If the object is a hash, does a simple hash lookup. If it's an object that responds to the key as a method call, invokes that method. You get the idea.
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 144 def find(obj, key, default = nil) return find_in_hash(obj.to_hash, key, default) if obj.respond_to?(:to_hash) unless obj.respond_to?(key) # no match for the key, but it may include a hyphen, so try again replacing hyphens with underscores. key = key.to_s.tr("-", "_") return default unless obj.respond_to?(key) end meth = obj.method(key) rescue proc { obj.send(key) } meth.arity == 1 ? meth.to_proc : meth.call end |
#has_key?(key) ⇒ Boolean
Do we know about a particular key? In other words, will calling
context[key] give us a result that was set. Basically.
105 106 107 108 109 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 105 def has_key?(key) fetch(key, false) rescue ContextMiss false end |
#mustache_in_stack ⇒ Mustache
Find the first Mustache in the stack.
If we're being rendered inside a Mustache object as a context, we'll use that one.
54 55 56 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 54 def mustache_in_stack @mustache_in_stack ||= @stack.find { |frame| frame.is_a?(Mustache) } end |
#partial(name, indentation = "") ⇒ 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 render the result.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 32 def partial(name, indentation = "") # Look for the first Mustache in the stack. mustache = mustache_in_stack # Indent the partial template by the given indentation. part = mustache.partial(name).to_s.gsub(/^/, indentation) # Get a template object for the partial and render the result. template_for_partial(part).render(self) end |
#pop ⇒ Context
Removes the most recently added object from the context's internal stack.
85 86 87 88 89 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 85 def pop @stack.shift @mustache_in_stack = nil self end |
#push(new_obj) ⇒ Context
Adds a new object to the context's internal stack.
74 75 76 77 78 |
# File 'lib/braintrust/vendor/mustache/context.rb', line 74 def push(new_obj) @stack.unshift(new_obj) @mustache_in_stack = nil self end |