File: template.rb

package info (click to toggle)
ruby-jsonify 0.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 240 kB
  • sloc: ruby: 748; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 744 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

module Jsonify

  class Template < Tilt::Template
    self.default_mime_type = 'application/json'

    def self.engine_initialized?
      defined? ::Jsonify
    end

    def initialize_engine
      require_template_library 'jsonify'
    end

    def prepare; end

    def evaluate(scope, locals, &block)
      return super(scope, locals, &block) if data.respond_to?(:to_str)
      json = ::Jsonify::Builder.new
      data.call(json)
      json.compile!
    end

    def precompiled_preamble(locals)
      return super if locals.include? :json
      "json = ::Jsonify::Builder.new\n#{super}"
    end

    def precompiled_postamble(locals)
      "json.compile!"
    end

    def precompiled_template(locals)
      data.to_str
    end
  end

end