Sha256: 9cb1bfc169906ebac9e1bdf12bf9c57b0d167b5f2204a72676a63330e0f1e538
Contents?: true
Size: 1.76 KB
Versions: 8
Compression:
Stored size: 1.76 KB
Contents
require 'groonga' require File.expand_path(File.dirname(__FILE__)) + '/groovy/model' # overwrite Groonga::Record#inspect because the #attributes part is # making debugging take ages class Groonga::Record def inspect super end end module Groovy class Error < StandardError; end class ContextNotFound < Error; end class ContextAlreadyClosed < Error; end class << self def contexts @contexts ||= {} end def [](name) contexts[name.to_sym] end def first_context_name contexts.keys.first end def open(db_path, name = :default, opts = {}) unless db_path.is_a?(String) raise ArgumentError, "Invalid db_path: #{db_path}" end if contexts[name.to_sym] raise ArgumentError, "Context already defined: #{name}" end contexts[name.to_sym] = if name == :default Groonga::Context.default.tap { |ctx| open_or_create_db(ctx, db_path) } else init_context(db_path) end end def close(name = :default) ctx = contexts[name.to_sym] or raise ContextNotFound.new(name) contexts.delete(name.to_sym) ctx.close rescue Groonga::Closed => e raise ContextAlreadyClosed end def logger=(obj) @logger = obj end def logger @logger ||= Logger.new(STDOUT) end private def init_context(db_path) Groonga::Context.new.tap do |ctx| open_or_create_db(ctx, db_path) end end def open_or_create_db(ctx, path) if File.exist?(path) logger.info "Opening DB at #{path}" ctx.open_database(path) else dir = File.dirname(path) logger.info "Creating DB in #{dir}" FileUtils.mkdir_p(dir) ctx.create_database(path) end end end end
Version data entries
8 entries across 8 versions & 1 rubygems