Sha256: e3bce66b2630a6217c2534195efeb247b3fdf43102570148330933c4accf76f2

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

module Gon
  module Jbuilder
    class << self

      def parse_source(source, controller)
        output = ::JbuilderTemplate.encode(controller) do |json|
          eval source
        end
        JSON.parse(output)
      end

      def parse_jbuilder(jbuilder_path, controller)
        controller.instance_variables.each do |name|
          self.instance_variable_set(name, controller.instance_variable_get(name))
        end
        lines = find_partials(File.readlines(jbuilder_path))
        source = lines.join('')

        output = parse_source(source, controller)
      end

      def parse_partial(partial_line)
        path = partial_line.match(/['"]([^'"]*)['"]/)[1]
        options_hash = partial_line.match(/,(.*)/)[1]
        if options_hash.present?
          options = eval '{' + options_hash + '}'
          options.each do |name, val|
            self.instance_variable_set('@' + name.to_s, val)
            eval "def #{name}; self.instance_variable_get('@' + '#{name.to_s}'); end"
          end
        end
        find_partials(File.readlines(path))
      end

      def find_partials(lines = [])
        lines.map do |line|
          if line =~ /partial!/
            parse_partial(line)
          else
            line
          end
        end.flatten
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gon-2.3.0 lib/gon/jbuilder.rb
gon-2.2.2 lib/gon/jbuilder.rb
gon-2.2.0 lib/gon/jbuilder.rb
gon-2.1.2 lib/gon/jbuilder.rb
gon-2.1.0 lib/gon/jbuilder.rb