Skip to content

Commit fdbee87

Browse files
Merge pull request #4 from fractaledmind/extension-constants
Get path to installed extensions.
2 parents 278de92 + 9488667 commit fdbee87

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

Gemfile.lock

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
sqlpkg (0.2.2)
4+
sqlpkg (0.2.3.1)
55

66
GEM
77
remote: https://rubygems.org/
@@ -128,10 +128,6 @@ GEM
128128
nokogiri (1.15.5)
129129
mini_portile2 (~> 2.8.2)
130130
racc (~> 1.4)
131-
nokogiri (1.15.5-arm64-darwin)
132-
racc (~> 1.4)
133-
nokogiri (1.15.5-x86_64-linux)
134-
racc (~> 1.4)
135131
parallel (1.24.0)
136132
parser (3.2.2.4)
137133
ast (~> 2.4.1)
@@ -205,8 +201,6 @@ GEM
205201
rubyzip (2.3.2)
206202
sqlite3 (1.6.9)
207203
mini_portile2 (~> 2.8.0)
208-
sqlite3 (1.6.9-arm64-darwin)
209-
sqlite3 (1.6.9-x86_64-linux)
210204
standard (1.32.1)
211205
language_server-protocol (~> 3.17.0.2)
212206
lint_roller (~> 1.0)

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,42 @@ bundle exec sqlpkg help
7878
└────────────────────────────────────────────────┘
7979
```
8080

81+
You can get the path to an installed extension using the `Sqlpkg.path_for` method, e.g.:
82+
83+
```ruby
84+
Sqlpkg.path_for("nalgeon/uuid")
85+
# => "./.sqlpkg/nalgeon/uuid/uuid.dylib"
86+
```
87+
88+
You can also use the shorter `.[]` alias:
89+
90+
```ruby
91+
Sqlpkg["nalgeon/uuid"]
92+
# => "./.sqlpkg/nalgeon/uuid/uuid.dylib"
93+
```
94+
95+
If you try to access an extension that hasn't been installed, a `Sqlpkg::ExtensionNotInstalledError` will be raised:
96+
97+
```ruby
98+
Sqlpkg["nalgeon/ulid"]
99+
# raises Sqlpkg::ExtensionNotInstalledError
100+
```
101+
102+
This feature is particulary useful for the [new Rails feature](https://github.com/rails/rails/pull/53827) and [`sqlite3-ruby` feature](https://github.com/sparklemotion/sqlite3-ruby/pull/586) that allows automatically loading extensions via the `extensions` keyword defined in the Rails `config/database.yml` or the `SQLite3::Database.new` method call. You can now use either:
103+
104+
```yaml
105+
development:
106+
adapter: sqlite3
107+
extensions:
108+
- <%= Sqlpkg.path_for("asg017/ulid") %>
109+
```
110+
111+
or if you are using `SQLite3::Database` directly:
112+
113+
```ruby
114+
db = SQLite3::Database.new(":memory:", extensions: [Sqlpkg.path_for("asg017/ulid")])
115+
```
116+
81117
## Development
82118

83119
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

lib/sqlpkg.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
# frozen_string_literal: true
22

33
module Sqlpkg
4+
DIR = ".sqlpkg"
5+
FILE_PATTERN = "*.{dylib,so,dll}"
6+
7+
Error = Class.new(StandardError)
8+
ExtensionNotInstalledError = Class.new(Error)
9+
10+
class << self
11+
# File path for identified extension
12+
# => "./.sqlpkg/nalgeon/uuid/uuid.dylib"
13+
def path_for(identifier)
14+
path_glob = File.join(file_dir, identifier, FILE_PATTERN)
15+
path = Dir.glob(path_glob).first
16+
17+
path || raise(ExtensionNotInstalledError, "No extension found for identifier: #{identifier}")
18+
end
19+
alias_method :[], :path_for
20+
21+
# The directory where `sqlpkg` stores installed extensions
22+
# => "./.sqlpkg"
23+
def file_dir
24+
File.join(__dir__, DIR)
25+
end
26+
27+
# List of file paths for all installed extensions
28+
# => ["./.sqlpkg/asg017/ulid/ulid0.dylib", "./.sqlpkg/nalgeon/uuid/uuid.dylib"]
29+
def installed_extension_paths
30+
Dir.glob File.join(file_dir, "**", FILE_PATTERN)
31+
end
32+
end
433
end
534

635
require_relative "sqlpkg/version"

test/test_sqlpkg.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,30 @@ class TestSqlpkg < Minitest::Test
66
def test_that_it_has_a_version_number
77
refute_nil ::Sqlpkg::VERSION
88
end
9+
10+
def test_path_for_when_glob_returns_paths
11+
path = "./.sqlpkg/nalgeon/uuid/uuid.dylib"
12+
Dir.stub :glob, [path] do
13+
assert_equal path, Sqlpkg.path_for("nalgeon/uuid")
14+
end
15+
end
16+
17+
def test_path_for_when_glob_returns_empty_array
18+
assert_raises Sqlpkg::ExtensionNotInstalledError do
19+
Sqlpkg.path_for("nalgeon/uuid")
20+
end
21+
end
22+
23+
def test_accessor_when_glob_returns_paths
24+
path = "./.sqlpkg/nalgeon/uuid/uuid.dylib"
25+
Dir.stub :glob, [path] do
26+
assert_equal path, Sqlpkg["nalgeon/uuid"]
27+
end
28+
end
29+
30+
def test_accessor_when_glob_returns_empty_array
31+
assert_raises Sqlpkg::ExtensionNotInstalledError do
32+
Sqlpkg["nalgeon/uuid"]
33+
end
34+
end
935
end

0 commit comments

Comments
 (0)