lib/groovy.rb in groovy-0.1.3 vs lib/groovy.rb in groovy-0.1.4

- old
+ new

@@ -1,30 +1,49 @@ require 'groonga' require File.expand_path(File.dirname(__FILE__)) + '/groovy/model' module Groovy + class Error < StandardError; end + class ContextNotFound < Error; end + class ContextAlreadyClosed < Error; end + class << self def contexts @contexts ||= {} end - def [](key) - contexts[key.to_sym] + def [](name) + contexts[name.to_sym] end def first_context_name contexts.keys.first end def open(db_path, name = :default, opts = {}) - raise "Context already defined: #{name}" if contexts[name.to_sym] + 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, opts) 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