magic-lookup 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -3
- data/README.md +18 -6
- data/lib/magic/lookup/namespaces.rb +21 -0
- data/lib/magic/lookup/version.rb +1 -1
- data/lib/magic/lookup.rb +6 -2
- data/sig/magic/lookup.rbs +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89553c6220236db6f6b88312bb5a8097718f383a107ab2a85e7c7e9c8ee9ebf1
|
4
|
+
data.tar.gz: 865c7c71973d94e809374270428b2532960a3b4a8a64dfd9393927942d140141
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6496b67373aada9b47a0e6af847dca2485da0bb2d683b9eeed46dfbb15f55dedb78cf522da5ed03bc428de427ec60701629e377c1d9a786fe98a46a64b13347b
|
7
|
+
data.tar.gz: 9b9fe4f56b6d5ed85e957ec1bba331574a4f38677c0cf5e9c2225808ba00edac760256978b51ebd644a70bf68aee771bbc8dd0ffca519d38c46c6a2565ea30df
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
|
-
## [
|
1
|
+
## [0.2.0] — 2024-10-19
|
2
2
|
|
3
|
-
|
3
|
+
### Added
|
4
4
|
|
5
|
-
-
|
5
|
+
- Optional `namespace` parameter to `Magic::Lookup#for` for class lookups within a namespace.
|
6
|
+
- `Magic::Lookup#namespaces` to set default lookup namespaces.
|
7
|
+
|
8
|
+
|
9
|
+
## [0.1.0] — 2024-10-10
|
10
|
+
|
11
|
+
### Added
|
12
|
+
|
13
|
+
- `Magic::Lookup#for` for name-based class lookups.
|
14
|
+
- `Magic::Lookup::Error` to be used when lookup fails.
|
15
|
+
- `Magic::Lookup::Error.for` factory helper.
|
data/README.md
CHANGED
@@ -19,19 +19,17 @@ So, meet
|
|
19
19
|
|
20
20
|
# 🔮 Magic Lookup
|
21
21
|
|
22
|
-
It
|
22
|
+
It’s meant to be The One to Rule Them All — the library to provide a generic name-based lookup for a plenty of cases.
|
23
23
|
|
24
24
|
## Installation
|
25
25
|
|
26
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
27
|
-
|
28
26
|
Install the gem and add to the application's Gemfile by executing:
|
29
27
|
|
30
|
-
$ bundle add
|
28
|
+
$ bundle add magic-lookup
|
31
29
|
|
32
30
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
33
31
|
|
34
|
-
$ gem install
|
32
|
+
$ gem install magic-lookup
|
35
33
|
|
36
34
|
## Usage
|
37
35
|
|
@@ -70,7 +68,7 @@ scope_class = Scope.for(object.class) or
|
|
70
68
|
|
71
69
|
`Magic::Lookup::Error` is never raised internally and is meant to be used in your code that implements the lookup logic.
|
72
70
|
|
73
|
-
##
|
71
|
+
## 🔮 Magic
|
74
72
|
|
75
73
|
### Inheritance
|
76
74
|
|
@@ -80,6 +78,20 @@ Lookup is provided not only for the class itself, but to any of its ancestors as
|
|
80
78
|
|
81
79
|
Both of matching classes — the target and its match — may be namespaced independently.
|
82
80
|
|
81
|
+
One can specify a namespace to look in:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
Scope.for MyModel # => MyScope
|
85
|
+
Scope.for MyModel, MyNamespace # => MyNamespace::MyScope
|
86
|
+
```
|
87
|
+
|
88
|
+
Multiple default lookup namespaces may be set for the base class:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
Scope.namespaces << MyNamespace # => [nil, MyNamespace]
|
92
|
+
Scope.for MyModel # => MyNamespace::MyScope
|
93
|
+
```
|
94
|
+
|
83
95
|
> [!TIP]
|
84
96
|
> Until a comprehensive documentation on all the use cases is released, the spec is recommended as further reading.
|
85
97
|
> One can access it by running `rake` in the gem directory.
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Magic
|
4
|
+
module Lookup
|
5
|
+
module Namespaces
|
6
|
+
attr_writer :namespaces
|
7
|
+
|
8
|
+
def namespaces = @namespaces ||= [ nil ]
|
9
|
+
|
10
|
+
def for object_class, *namespaces
|
11
|
+
return super unless namespaces.empty?
|
12
|
+
|
13
|
+
self.namespaces
|
14
|
+
.reverse # recently added first
|
15
|
+
.lazy # optimization
|
16
|
+
.filter_map { super object_class, _1 }
|
17
|
+
.first
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/magic/lookup/version.rb
CHANGED
data/lib/magic/lookup.rb
CHANGED
@@ -6,11 +6,12 @@ require 'memery'
|
|
6
6
|
|
7
7
|
module Magic
|
8
8
|
module Lookup
|
9
|
-
autoload :Error,
|
9
|
+
autoload :Error, 'magic/lookup/error'
|
10
|
+
autoload :Namespaces, 'magic/lookup/namespaces'
|
10
11
|
|
11
12
|
include Memery
|
12
13
|
|
13
|
-
memoize def for object_class
|
14
|
+
memoize def for object_class, namespace = nil
|
14
15
|
descendants = self.descendants # cache
|
15
16
|
.reverse # most specific first
|
16
17
|
|
@@ -18,12 +19,15 @@ module Magic
|
|
18
19
|
.lazy # optimization
|
19
20
|
.filter(&:name)
|
20
21
|
.map { name_for _1 }
|
22
|
+
.map { [ *namespace, _1 ] * '::' }
|
21
23
|
.filter_map do |class_name|
|
22
24
|
descendants.find { _1.name == class_name }
|
23
25
|
end
|
24
26
|
.first
|
25
27
|
end
|
26
28
|
|
29
|
+
prepend Namespaces
|
30
|
+
|
27
31
|
def name_for(object_class) = raise NotImplementedError
|
28
32
|
|
29
33
|
def descendants
|
data/sig/magic/lookup.rbs
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magic-lookup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Senko
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2024-10-
|
10
|
+
date: 2024-10-19 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: memery
|
@@ -41,6 +41,7 @@ files:
|
|
41
41
|
- lib/magic/lookup.rb
|
42
42
|
- lib/magic/lookup/authors.rb
|
43
43
|
- lib/magic/lookup/error.rb
|
44
|
+
- lib/magic/lookup/namespaces.rb
|
44
45
|
- lib/magic/lookup/version.rb
|
45
46
|
- sig/gem.rbs
|
46
47
|
- sig/magic/lookup.rbs
|
@@ -51,7 +52,7 @@ licenses:
|
|
51
52
|
metadata:
|
52
53
|
homepage_uri: https://github.com/Alexander-Senko/magic-lookup
|
53
54
|
source_code_uri: https://github.com/Alexander-Senko/magic-lookup
|
54
|
-
changelog_uri: https://github.com/Alexander-Senko/magic-lookup/blob/v0.
|
55
|
+
changelog_uri: https://github.com/Alexander-Senko/magic-lookup/blob/v0.2.0/CHANGELOG.md
|
55
56
|
rdoc_options: []
|
56
57
|
require_paths:
|
57
58
|
- lib
|