class RbLunrjs::Index

Public Class Methods

new(options) click to toggle source
# File lib/rb_lunrjs.rb, line 14
def initialize(options)
  @lunr_options = options.fetch(:lunr).fetch(:options)
  @lunr_documents = options.fetch(:lunr).fetch(:documents)
  @out_file = options.fetch(:out_file)
  @gzip = options.fetch(:gzip, false)
  @debug = options.fetch(:debug, false)
end

Public Instance Methods

compile_coffee() click to toggle source
# File lib/rb_lunrjs.rb, line 52
def compile_coffee
  code = File.read(File.join(dir, '..', 'js/app.js.coffee'))
  File.open(File.join(dir, '..', 'js/app.js'), 'w') do |f|
    f << CoffeeScript.compile(code)
  end
  code
end
debug_mode(ctx) click to toggle source
# File lib/rb_lunrjs.rb, line 22
def debug_mode(ctx)
  ctx['console'] = STDOUT
  def STDOUT.debug(*a)
    puts sprintf(*a.map(&:to_s))
  end
end
dir() click to toggle source
# File lib/rb_lunrjs.rb, line 60
def dir
  @dir ||= File.dirname(__FILE__)
end
dump() click to toggle source
# File lib/rb_lunrjs.rb, line 29
    def dump
      compile_coffee
      V8::Context.new do |ctx|
        debug_mode(ctx) if @debug
        ctx.load(File.join(dir, '..', 'js/lunr.min.js'))
        ctx.load(File.join(dir, '..', 'js/app.js'))
        json = ctx.eval <<-JS
            search = new Search(#{JSON.dump(@lunr_options)}, #{JSON.dump(@lunr_documents)});
            search.toJSON();
        JS
        if @gzip
          Zlib::GzipWriter.open("#{@out_file}.gz") do |gz|
            gz.write json
          end
        else
          File.open("#{@out_file}", 'w') do |f|
            f << json
          end
        end
      end
      true
    end