Sha256: 4f1c1370bab3876241167d5973865475a5588e8c302d1642c929a439f5eacece

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

# Copyright (C) 2010-2013  Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

require "fileutils"

module ActiveGroonga
  class Database
    def initialize(path)
      @path = path
      @database = nil
    end

    def ensure_available
      return if @database
      if @path.exist?
        @database = Groonga::Database.open(@path.to_s,
                                           :context => Base.context)
      else
        FileUtils.mkdir_p(@path.dirname) unless @path.dirname.exist?
        @database = Groonga::Database.create(:path => @path.to_s,
                                             :context => Base.context)
      end
    end

    def remove
      ensure_available if @path.exist?
      return if @database.nil?
      @database.remove
      @database = nil
    end

    def close
      return if @database.nil?
      @database.close
      @database = nil
    end

    def reopen
      close
      ensure_available
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activegroonga-4.2.1 lib/active_groonga/database.rb
activegroonga-4.2.0 lib/active_groonga/database.rb
activegroonga-4.0.0 lib/active_groonga/database.rb