| 1 | require 'mustache/context_miss' |
| 2 | |
| 3 | class Mustache |
| 4 | |
| 5 | # A Context represents the context which a Mustache template is |
| 6 | # executed within. All Mustache tags reference keys in the Context. |
| 7 | # |
| 8 | class Context |
| 9 | |
| 10 | # Initializes a Mustache::Context. |
| 11 | # |
| 12 | # @param [Mustache] mustache A Mustache instance. |
| 13 | # |
| 14 | def initialize(mustache) |
| 15 | @stack = [mustache] |
| 16 | @partial_template_cache = {} |
| 17 | end |
| 18 | |
| 19 | # A {{>partial}} tag translates into a call to the context's |
| 20 | # `partial` method, which would be this sucker right here. |
| 21 | # |
| 22 | # If the Mustache view handling the rendering (e.g. the view |
| 23 | # representing your profile page or some other template) responds |
| 24 | # to `partial`, we call it and render the result. |
| 25 | # |
| 26 | def partial(name, indentation = '') |
| 27 | # Look for the first Mustache in the stack. |
| 28 | mustache = mustache_in_stack |
| 29 | |
| 30 | # Indent the partial template by the given indentation. |
| 31 | part = mustache.partial(name).to_s.gsub(/^/, indentation) |
| 32 | |
| 33 | # Get a template object for the partial and render the result. |
| 34 | template_for_partial(part).render(self) |
| 35 | end |
| 36 | |
| 37 | def template_for_partial(partial) |
| 38 | @partial_template_cache[partial] ||= Template.new(partial) |
| 39 | end |
| 40 | |
| 41 | # Find the first Mustache in the stack. |
| 42 | # |
| 43 | # If we're being rendered inside a Mustache object as a context, |
| 44 | # we'll use that one. |
| 45 | # |
| 46 | # @return [Mustache] First Mustache in the stack. |
| 47 | # |
| 48 | def mustache_in_stack |
| 49 | @mustache_in_stack ||= @stack.find { |frame| frame.is_a?(Mustache) } |
| 50 | end |
| 51 | |
| 52 | # Allows customization of how Mustache escapes things. |
| 53 | # |
| 54 | # @param [Object] value Value to escape. |
| 55 | # |
| 56 | # @return [String] Escaped string. |
| 57 | # |
| 58 | def escape(value) |
| 59 | mustache_in_stack.escape(value) |
| 60 | end |
| 61 | |
| 62 | # Adds a new object to the context's internal stack. |
| 63 | # |
| 64 | # @param [Object] new_obj Object to be added to the internal stack. |
| 65 | # |
| 66 | # @return [Context] Returns the Context. |
| 67 | # |
| 68 | def push(new_obj) |
| 69 | @stack.unshift(new_obj) |
| 70 | @mustache_in_stack = nil |
| 71 | self |
| 72 | end |
| 73 | |
| 74 | # Removes the most recently added object from the context's |
| 75 | # internal stack. |
| 76 | # |
| 77 | # @return [Context] Returns the Context. |
| 78 | # |
| 79 | def pop |
| 80 | @stack.shift |
| 81 | @mustache_in_stack = nil |
| 82 | self |
| 83 | end |
| 84 | |
| 85 | # Can be used to add a value to the context in a hash-like way. |
| 86 | # |
| 87 | # context[:name] = "Chris" |
| 88 | def []=(name, value) |
| 89 | push(name => value) |
| 90 | end |
| 91 | |
| 92 | # Alias for `fetch`. |
| 93 | def [](name) |
| 94 | fetch(name, nil) |
| 95 | end |
| 96 | |
| 97 | # Do we know about a particular key? In other words, will calling |
| 98 | # `context[key]` give us a result that was set. Basically. |
| 99 | def has_key?(key) |
| 100 | fetch(key, false) |
| 101 | rescue ContextMiss |
| 102 | false |
| 103 | end |
| 104 | |
| 105 | # Similar to Hash#fetch, finds a value by `name` in the context's |
| 106 | # stack. You may specify the default return value by passing a |
| 107 | # second parameter. |
| 108 | # |
| 109 | # If no second parameter is passed (or raise_on_context_miss is |
| 110 | # set to true), will raise a ContextMiss exception on miss. |
| 111 | def fetch(name, default = :__raise) |
| 112 | @stack.each do |frame| |
| 113 | # Prevent infinite recursion. |
| 114 | next if frame == self |
| 115 | |
| 116 | value = find(frame, name, :__missing) |
| 117 | return value if :__missing != value |
| 118 | end |
| 119 | |
| 120 | if default == :__raise || mustache_in_stack.raise_on_context_miss? |
| 121 | raise ContextMiss.new("Can't find #{name} in #{@stack.inspect}") |
| 122 | else |
| 123 | default |
| 124 | end |
| 125 | end |
| 126 | |
| 127 | # Finds a key in an object, using whatever method is most |
| 128 | # appropriate. If the object is a hash, does a simple hash lookup. |
| 129 | # If it's an object that responds to the key as a method call, |
| 130 | # invokes that method. You get the idea. |
| 131 | # |
| 132 | # @param [Object] obj The object to perform the lookup on. |
| 133 | # @param [String,Symbol] key The key whose value you want |
| 134 | # @param [Object] default An optional default value, to return if the key is not found. |
| 135 | # |
| 136 | # @return [Object] The value of key in object if it is found, and default otherwise. |
| 137 | # |
| 138 | def find(obj, key, default = nil) |
| 139 | return find_in_hash(obj.to_hash, key, default) if obj.respond_to?(:to_hash) |
| 140 | |
| 141 | unless obj.respond_to?(key) |
| 142 | # no match for the key, but it may include a hyphen, so try again replacing hyphens with underscores. |
| 143 | key = key.to_s.tr('-', '_') |
| 144 | return default unless obj.respond_to?(key) |
| 145 | end |
| 146 | |
| 147 | meth = obj.method(key) rescue proc { obj.send(key) } |
| 148 | meth.arity == 1 ? meth.to_proc : meth.call |
| 149 | end |
| 150 | |
| 151 | def current |
| 152 | @stack.first |
| 153 | end |
| 154 | |
| 155 | |
| 156 | private |
| 157 | |
| 158 | # Fetches a hash key if it exists, or returns the given default. |
| 159 | def find_in_hash(obj, key, default) |
| 160 | return obj[key] if obj.has_key?(key) |
| 161 | return obj[key.to_s] if obj.has_key?(key.to_s) |
| 162 | return obj[key] if obj.respond_to?(:default_proc) && obj.default_proc && obj[key] |
| 163 | |
| 164 | # If default is :__missing then we are from #fetch which is hunting through the stack |
| 165 | # If default is nil then we are reducing dot notation |
| 166 | if :__missing != default && mustache_in_stack.raise_on_context_miss? |
| 167 | raise ContextMiss.new("Can't find #{key} in #{obj}") |
| 168 | else |
| 169 | obj.fetch(key, default) |
| 170 | end |
| 171 | end |
| 172 | end |
| 173 | end |