Skip to content

Commit

Permalink
extension_path -> to_path
Browse files Browse the repository at this point in the history
  • Loading branch information
jethrodaniel committed Dec 7, 2024
1 parent a89ff17 commit 695b165
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ This gem compiles SQLite's uuid extension into a shared library using Ruby's nat

It doesn't actually compile a Ruby native extension, it just uses the ruby extension process to compile the SQLite library.

It then exposes a method (`SqliteExtensions::UUID.extension_path`) which returns the location of that shared library, which can be passed to [sqlite3](https://github.com/sparklemotion/sqlite3-ruby)'s `load_extension` method.
It then exposes a method (`SqliteExtensions::UUID.to_path`) which returns the location of that shared library, which can be passed to [sqlite3](https://github.com/sparklemotion/sqlite3-ruby)'s `load_extension` method.

For Rails, it also exposes a [railtie](https://api.rubyonrails.org/v7.2/classes/Rails/Railtie.html) (via `require: "sqlite_extensions/uuid/rails"`) that patches Rails' [configure_connection](https://github.com/rails/rails/blob/v8.0.0.rc1/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L815) method for the SQLite adapter, so that all SQLite database connections load the extension.

Expand Down
10 changes: 7 additions & 3 deletions lib/sqlite_extensions/uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

module SqliteExtensions
module UUID
def self.extension_path
spec = Gem.loaded_specs["sqlite_extensions-uuid"]
File.join(spec.require_path, "sqlite_extensions/uuid/uuid")
class << self
def to_path
spec = Gem.loaded_specs["sqlite_extensions-uuid"]
File.join(spec.require_path, "sqlite_extensions/uuid/uuid")
end

alias extension_path to_path
end
end
end
4 changes: 2 additions & 2 deletions lib/sqlite_extensions/uuid/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def configure_connection

begin
@raw_connection.enable_load_extension(true)
@raw_connection.load_extension(SqliteExtensions::UUID.extension_path)
@raw_connection.load_extension(SqliteExtensions::UUID.to_path)
rescue SQLite3::Exception => e
Rails.logger.error { "Error loading sqlite extension '#{SqliteExtensions::UUID.extension_path}' (#{e})" }
Rails.logger.error { "Error loading sqlite extension '#{SqliteExtensions::UUID.to_path}' (#{e})" }
ensure
@raw_connection.enable_load_extension(false)
end
Expand Down
8 changes: 6 additions & 2 deletions test/test_uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
require "test_helper"

class TestUUID < Minitest::Test
def test_extension_path
def test_to_path
loaded_specs_stub = {
"sqlite_extensions-uuid" => Struct.new(:require_path).new("foo")
}
Gem.stub(:loaded_specs, loaded_specs_stub) do
path = SqliteExtensions::UUID.extension_path
path = SqliteExtensions::UUID.to_path
assert_kind_of String, path

assert path.end_with?("sqlite_extensions/uuid/uuid")
assert_equal "foo/sqlite_extensions/uuid/uuid", path
end
end

def test_extension_path_alias
assert_equal SqliteExtensions::UUID.to_path, SqliteExtensions::UUID.extension_path
end

def test_it_works
require "sqlite3"

Expand Down

0 comments on commit 695b165

Please sign in to comment.