# HG changeset patch # User Michael Granger # Date 1508341596 25200 # Wed Oct 18 08:46:36 2017 -0700 # Node ID 7f4d7005a32ac8883dbd229233483f84273a64a2 # Parent 8ae1e5cbab9349f216c0bba6bcbe04b1032448db Simplified default DB code diff --git a/lib/wordnet.rb b/lib/wordnet.rb --- a/lib/wordnet.rb +++ b/lib/wordnet.rb @@ -15,7 +15,7 @@ # Release version - VERSION = '1.0.1' + VERSION = '1.1.0' # VCS revision REVISION = %q$Revision: $ diff --git a/lib/wordnet/lexicon.rb b/lib/wordnet/lexicon.rb --- a/lib/wordnet/lexicon.rb +++ b/lib/wordnet/lexicon.rb @@ -9,6 +9,13 @@ require 'wordnet/constants' require 'wordnet/model' +# Try to load the default database gem, ignoring it if it's not installed. +begin + require 'wordnet-defaultdb' +rescue LoadError + # No-op +end + # WordNet lexicon class - provides access to the WordNet lexical # database, and provides factory methods for looking up words[rdoc-ref:WordNet::Word] @@ -129,30 +136,8 @@ ### Get the Sequel URI of the default database, if it's installed. def self::default_db_uri - self.log.debug "Fetching the default db URI" - - # Try to load the default database gem, ignoring it if it's not installed. - begin - gem 'wordnet-defaultdb' - rescue Gem::LoadError - end - - # Now try the gem datadir first, and fall back to a local installation of the - # default db - datadir = nil - if Gem.datadir( 'wordnet-defaultdb' ) - datadir = Pathname( Gem.datadir('wordnet-defaultdb') ) - else - self.log.warn " no defaultdb gem; looking for the development database" - datadir = Pathname( __FILE__ ).dirname.parent.parent + - 'wordnet-defaultdb/data/wordnet-defaultdb' - end - - dbfile = datadir + 'wordnet31.sqlite' - self.log.debug " dbfile is: %s" % [ dbfile ] - - if dbfile.exist? - return "sqlite:#{dbfile}" + if defined?( WordNet::DefaultDB ) + return WordNet::DefaultDB.uri else return nil end diff --git a/spec/helpers.rb b/spec/helpers.rb --- a/spec/helpers.rb +++ b/spec/helpers.rb @@ -5,9 +5,12 @@ # SimpleCov test coverage reporting; enable this using the :coverage rake task require 'simplecov' if ENV['COVERAGE'] +$LOAD_PATH.unshift( 'wordnet-defaultdb/lib' ) + require 'rspec' require 'loggability/spechelpers' require 'wordnet' +require 'wordnet/defaultdb' ### RSpec helper functions. diff --git a/spec/wordnet/lexicon_spec.rb b/spec/wordnet/lexicon_spec.rb --- a/spec/wordnet/lexicon_spec.rb +++ b/spec/wordnet/lexicon_spec.rb @@ -16,44 +16,9 @@ end - context "the default_db_uri method" do - - it "uses the wordnet-defaultdb database gem (if available)" do - expect( Gem ).to receive( :datadir ).with( 'wordnet-defaultdb' ).at_least( :once ). - and_return( '/tmp/foo' ) - expect( FileTest ).to receive( :exist? ).with( '/tmp/foo/wordnet31.sqlite' ). - and_return( true ) - - expect( WordNet::Lexicon.default_db_uri ).to eq( "sqlite:/tmp/foo/wordnet31.sqlite" ) - end - - it "uses the development version of the wordnet-defaultdb database gem if it's " + - "not installed" do - expect( Gem ).to receive( :datadir ).with( 'wordnet-defaultdb' ). - and_return( nil ) - expect( FileTest ).to receive( :exist? ).with( devdb.to_s ). - and_return( true ) - - expect( WordNet::Lexicon.default_db_uri ).to eq( "sqlite:#{devdb}" ) - end - - it "returns nil if there is no default database" do - expect( Gem ).to receive( :datadir ).with( 'wordnet-defaultdb' ). - and_return( nil ) - expect( FileTest ).to receive( :exist? ).with( devdb.to_s ). - and_return( false ) - - expect( WordNet::Lexicon.default_db_uri ).to be_nil() - end - - end - - it "raises an exception if created with no arguments and no defaultdb is available" do - expect( Gem ).to receive( :datadir ).with( 'wordnet-defaultdb' ).at_least( :once ). + expect( WordNet::DefaultDB ).to receive( :uri ).at_least( :once ). and_return( nil ) - expect( FileTest ).to receive( :exist? ).with( devdb.to_s ). - and_return( false ) expect { WordNet::Lexicon.new diff --git a/wordnet-defaultdb/lib/wordnet-defaultdb.rb b/wordnet-defaultdb/lib/wordnet-defaultdb.rb --- a/wordnet-defaultdb/lib/wordnet-defaultdb.rb +++ b/wordnet-defaultdb/lib/wordnet-defaultdb.rb @@ -1,40 +1,5 @@ -#!/usr/bin/env ruby - -require 'pathname' - -# This gem is a container for the default WordNetSQL database files required for -# the 'wordnet' gem. It's mostly just a wrapper around the Sqlite database from: -# -# http://sqlunet.sourceforge.net/ -# -# == Author/s -# -# * Michael Granger -# -module WordNet - module DefaultDB - - # Library version constant - VERSION = '2.0.0' +# -*- ruby -*- +#encoding: utf-8 - # Version-control revision constant - REVISION = %q$Revision$ +require 'wordnet/defaultdb' - # The data directory which contains the database file - DATA_DIR = if ENV['WORDNET_DEFAULTDB_DATADIR'] - Pathname( ENV['WORDNET_DEFAULTDB_DATADIR'] ) - elsif Gem.datadir( 'wordnet-defaultdb' ) && File.directory?( Gem.datadir('wordnet-defaultdb') ) - Pathname( Gem.datadir('wordnet-defaultdb') ) - else - Pathname( __FILE__ ).dirname.parent + 'data/wordnet-defaultdb' - end - - - ### The Sequel URI for the database - def self::uri - - end - - end # module DefaultDB -end # module Wordnet - diff --git a/wordnet-defaultdb/lib/wordnet/defaultdb.rb b/wordnet-defaultdb/lib/wordnet/defaultdb.rb new file mode 100644 --- /dev/null +++ b/wordnet-defaultdb/lib/wordnet/defaultdb.rb @@ -0,0 +1,41 @@ +# -*- ruby -*- +#encoding: utf-8 + +require 'wordnet' +require 'pathname' + +# This gem is a container for the default database files required for the +# 'wordnet' gem. It's mostly just a wrapper around the Sqlite database from +# SQLUNet: +# +# http://sqlunet.sourceforge.net/ +# +module WordNet::DefaultDB + + # Library version constant + VERSION = '2.0.0' + + # Version-control revision constant + REVISION = %q$Revision$ + + # The data directory which contains the database file + DATA_DIR = if ENV['WORDNET_DEFAULTDB_DATADIR'] + Pathname( ENV['WORDNET_DEFAULTDB_DATADIR'] ) + elsif Gem.datadir( 'wordnet-defaultdb' ) && File.directory?( Gem.datadir('wordnet-defaultdb') ) + Pathname( Gem.datadir('wordnet-defaultdb') ) + else + Pathname( __FILE__ ).dirname.parent.parent + 'data/wordnet-defaultdb' + end + + # The name of the bundled Sqlite database + DATABASE_FILENAME = 'wordnet31.sqlite' + + + ### Return the Sequel URI for the database + def self::uri + dbfile = WordNet::DefaultDB::DATA_DIR + DATABASE_FILENAME + return nil unless dbfile.exist? + return "sqlite:%s" % [ dbfile ] + end + +end # module WordNet::DefaultDB diff --git a/wordnet-defaultdb/spec/spec_helper.rb b/wordnet-defaultdb/spec/spec_helper.rb new file mode 100644 --- /dev/null +++ b/wordnet-defaultdb/spec/spec_helper.rb @@ -0,0 +1,32 @@ +#!/usr/bin/env rspec + +BEGIN { + require 'pathname' + $LOAD_PATH << Pathname( __FILE__ ).dirname.parent + 'lib' +} + +require 'rspec' +require 'wordnet/defaultdb' + + +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + config.shared_context_metadata_behavior = :apply_to_host_groups + + config.filter_run_when_matching :focus + config.example_status_persistence_file_path = "spec/.state" + config.disable_monkey_patching! + config.warnings = true + + config.profile_examples = 10 + config.order = :random + Kernel.srand config.seed +end + diff --git a/wordnet-defaultdb/spec/wordnet/defaultdb_spec.rb b/wordnet-defaultdb/spec/wordnet/defaultdb_spec.rb new file mode 100644 --- /dev/null +++ b/wordnet-defaultdb/spec/wordnet/defaultdb_spec.rb @@ -0,0 +1,25 @@ +#!/usr/bin/env rspec -cfd + +require_relative '../spec_helper' + +require 'rspec' + +require 'wordnet' +require 'wordnet/defaultdb' + + +RSpec.describe WordNet::DefaultDB do + + it "knows what the URL of its database is" do + expect( described_class.uri ).to start_with( 'sqlite:' ).and( end_with('wordnet31.sqlite') ) + end + + + it "returns nil if its data file does not exist" do + expect( FileTest ).to receive( :exist? ).with( a_string_ending_with('wordnet31.sqlite') ). + and_return( false ) + expect( described_class.uri ).to be_nil + end + +end +