Module: Jason
- Defined in:
- lib/jason.rb,
lib/jason/version.rb,
lib/jason/json_eruby.rb,
lib/jason/rails_template_handler.rb
Overview
Renders and compiles Jason templates.
Defined Under Namespace
Classes: JSONEruby, RailsTemplateHandler
Constant Summary collapse
- VERSION =
'0.6.0'
Class Attribute Summary collapse
-
.output_format
The output format of the JSON.
-
.output_listeners
The output listeners for jason.
Class Method Summary collapse
-
.compile(template) ⇒ String
Compile a template.
-
.process(buffer)
Process a rendered buffer.
-
.render(template, binding = nil) ⇒ String
Render a template.
Class Attribute Details
.output_format
The output format of the JSON.
I suggest using :pretty
for development and testing and :compact
for
production.
17 18 19 |
# File 'lib/jason.rb', line 17 def output_format @output_format ||= :compact end |
.output_listeners
The output listeners for jason.
All objects in this array are sent #call with the generated JSON whenever jason processes a buffer. This can be useful for logging output like so:
Jason.output_listeners << lambda { |json| Rails.logger.info(json) }
29 30 31 |
# File 'lib/jason.rb', line 29 def output_listeners @output_listeners ||= [] end |
Class Method Details
.compile(template) ⇒ String
Compile a template.
Eval the returned value to render the template within the current binding.
58 59 60 |
# File 'lib/jason.rb', line 58 def self.compile(template) "#{eruby_template(template).src}; Jason.process(_buf)" end |
.process(buffer)
Process a rendered buffer.
Removes any trailing commas and compresses the buffer. After generating the JSON, it calls each one of the output listeners with the generated JSON.
You should not have to directly call this method.
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/jason.rb', line 70 def self.process(buffer) obj = JSON.load(remove_trailing_commas(buffer)) if output_format == :pretty json = JSON.pretty_generate(obj) else json = JSON.generate(obj) end output_listeners.each { |listener| listener.call(obj) } json end |
.render(template, binding = nil) ⇒ String
Render a template.
42 43 44 45 46 47 48 49 50 |
# File 'lib/jason.rb', line 42 def self.render(template, binding = nil) if binding yaml = eruby_template(template).result(binding) else yaml = eruby_template(template).result end process(yaml) end |