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.3.0'

Class Method Summary collapse

Class Method Details

.compile(template) ⇒ String

Compile a template.

Eval the returned value to render the template within the current binding.

Parameters:

  • template (String)

    the template to compile

Returns:

  • (String)

    the compiled template



31
32
33
# File 'lib/jason.rb', line 31

def self.compile(template)
  "#{eruby_template(template).src}; Jason.process(_buf)"
end

.process(buffer)



35
36
37
# File 'lib/jason.rb', line 35

def self.process(buffer)
  JSON.load(remove_trailing_commas(buffer)).to_json
end

.render(template, binding = nil) ⇒ String

Render a template.

Examples:

Jason.render('foo: bar') # => '{"foo": "bar"}'

Parameters:

  • template (String)

    the template to render

  • binding (Binding) (defaults to: nil)

    the binding to render the template in

Returns:

  • (String)

    the rendered template



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

def self.render(template, binding = nil)
  if binding
    yaml = eruby_template(template).result(binding)
  else
    yaml = eruby_template(template).result
  end
  
  process(yaml)
end