Class: Mustache

Inherits:
Object
  • Object
show all
Defined in:
lib/mustache_json.rb,
lib/mustache/json/backends/yajl.rb,
lib/mustache/json/backends/json_gem.rb,
lib/mustache/json/backends/json_pure.rb,
lib/mustache/json/backends/active_support.rb

Defined Under Namespace

Modules: JSON

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.serializable_methodsObject

An array of methods that should be used for serialization. By default it is all public instance methods defined on the specific class (no superclasses). Override this for more specific control.



8
9
10
# File 'lib/mustache_json.rb', line 8

def self.serializable_methods
  public_instance_methods(false)
end

Instance Method Details

#serializable_hashObject

A hash of the compiled Mustache, including all serializable methods as well as the specified context.



14
15
16
17
18
19
20
21
22
# File 'lib/mustache_json.rb', line 14

def serializable_hash
  hash = self.class.serializable_methods.inject({}) do |result, method|
    # Symbolize the method to work better with the Mustache Context.
    result[method.to_sym] = self.send(method)
    result
  end
  
  hash.merge!(context)
end

#to_json(additional_context = {}) ⇒ Object

Convert the current Mustache object to JSON and provide optional additional context for the result.



26
27
28
29
# File 'lib/mustache_json.rb', line 26

def to_json(additional_context = {})
  context.merge(additional_context)
  Mustache::JSON.encode(serializable_hash)
end