Sha256: 66d8b69eac368f25cd66146fe78d5e0cbbefbb84a857d479f86d64fbb1602010

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Handlebarsjs
  # Wraps MiniRacer snapshot with specific emphasis on
  # loading Handlebars helpers onto the MiniRacer context
  # in the correct order. So that new contexts are preloaded
  # with the handlebars library and the configured helpers.
  class HandlebarsSnapshot
    attr_reader :scripts
    attr_reader :helpers

    def initialize
      @scripts = []
      @helpers = []
    end

    def add_library(name, script: nil, path: nil)
      add_script(name, 'library', script: script, path: path)
    end

    def add_snippet(name, script: nil, path: nil)
      add_script(name, 'snippet', script: script, path: path)
    end

    def add_helper(name, callback)
      @helpers << {
        name: name,
        callback: callback
      }
    end

    def register_helper(name)
      add_script(name, 'helper', script: "Handlebars.registerHelper('#{name}', ruby_#{name})")
    end

    def script
      scripts.map { |script| "// #{script[:type]} - #{script[:name]}\n#{script[:script]}" }.join("\n\n")
    end

    def snapshot
      @snapshot ||= MiniRacer::Snapshot.new(script)
    end

    def dirty?
      @snapshot.nil?
    end

    def debug
      puts script
    end

    private

    def add_script(name, type, script: nil, path: nil)
      raise Handlebarsjs::Error, 'script or path is required' if script.nil? && path.nil?
      raise Handlebarsjs::Error, 'script and path are mutually exclusive' if script && path

      script ||= File.read(path)
      add_script_item(name, type, script, path)
    end

    def add_script_item(name, type, script, path = nil)
      @snapshot = nil
      scripts << {
        name: name,
        type: type,
        script: script,
        path: path
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
handlebarsjs-0.3.0 lib/handlebarsjs/handlebars_snapshot.rb