magic-lookup 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -2
- data/CHANGELOG.md +7 -0
- data/README.md +16 -4
- data/lib/magic/lookup/authors.rb +2 -2
- data/lib/magic/lookup/error.rb +11 -0
- data/lib/magic/lookup/namespaces.rb +1 -1
- data/lib/magic/lookup/version.rb +1 -1
- data/lib/magic/lookup.rb +27 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c703f8d45523eb9fe156018f415b24d1eef923ae8abe72b549a8f66944b3d02
|
4
|
+
data.tar.gz: 7f473974c2a805aa8bfe766fb1fb0a58692098338a99f09dffbde0b2a6617899
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68be2c2fb418fe82891c30e8fbc5e8a8b84bcdc3eed3eee8b696ce2cbbf8f538f92b950af599fd3710d977f15c8c0f8f9816784e55454b0995e333b8fa9d63cc
|
7
|
+
data.tar.gz: b00b16af1e3aedc9a5c900ef9144f3e669fae21d862b0d856819dfb7eac90320597c496a2ff56f1e117f265cb58ec9b337c1c173cc985868e47b87a812b6f433
|
data/.rubocop.yml
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
inherit_from:
|
1
|
+
inherit_from:
|
2
|
+
- https://github.com/Alexander-Senko/Alexander-Senko/raw/refs/heads/main/.rubocop.yml
|
3
|
+
- https://github.com/Alexander-Senko/Alexander-Senko/raw/refs/heads/main/.rubocop-rspec.yml
|
2
4
|
|
3
|
-
|
5
|
+
plugins:
|
4
6
|
- rubocop-rspec
|
5
7
|
- rubocop-rake
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+

|
1
4
|

|
@@ -17,9 +20,9 @@ Moreover, inconsistencies across these implementations lead to misunderstanding
|
|
17
20
|
|
18
21
|
So, meet
|
19
22
|
|
20
|
-
# 🔮
|
23
|
+
# 🔮 Magic Lookup
|
21
24
|
|
22
|
-
It’s meant to be
|
25
|
+
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
26
|
|
24
27
|
## Installation
|
25
28
|
|
@@ -66,9 +69,10 @@ scope_class = Scope.for(object.class) or
|
|
66
69
|
raise Magic::Lookup::Error.for(object, Scope)
|
67
70
|
```
|
68
71
|
|
69
|
-
|
72
|
+
> [!NOTE]
|
73
|
+
> `Magic::Lookup::Error` is never raised internally and is meant to be used in your code that implements the lookup logic.
|
70
74
|
|
71
|
-
## 🔮
|
75
|
+
## 🔮 Magic
|
72
76
|
|
73
77
|
### Inheritance
|
74
78
|
|
@@ -97,6 +101,14 @@ Scope.for MyModel # => MyNamespace::MyScope
|
|
97
101
|
> One can access it by running `rake` in the gem directory.
|
98
102
|
> The output is quite descriptive to get familiar with the use cases.
|
99
103
|
|
104
|
+
## Known issues
|
105
|
+
|
106
|
+
### https://github.com/Alexander-Senko/magic-lookup/issues/1
|
107
|
+
|
108
|
+
> [!IMPORTANT]
|
109
|
+
> Magic Lookup doesn’t try to autoload any classes, it searches among already loaded ones instead.
|
110
|
+
> Thus, one should preload all classes that need to be accessible via the lookup.
|
111
|
+
|
100
112
|
## Development
|
101
113
|
|
102
114
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/magic/lookup/authors.rb
CHANGED
data/lib/magic/lookup/error.rb
CHANGED
@@ -2,6 +2,17 @@
|
|
2
2
|
|
3
3
|
module Magic
|
4
4
|
module Lookup
|
5
|
+
# = Magic Lookup Error
|
6
|
+
#
|
7
|
+
# When no class is found, `nil` is returned. If you need to raise
|
8
|
+
# an exception in this case, you can use `Magic::Lookup::Error`
|
9
|
+
# like this:
|
10
|
+
#
|
11
|
+
# scope_class = Scope.for(object.class) or
|
12
|
+
# raise Magic::Lookup::Error.for(object, Scope)
|
13
|
+
#
|
14
|
+
# `Magic::Lookup::Error` is never raised internally and is meant
|
15
|
+
# to be used by a class implementing the lookup logic.
|
5
16
|
class Error < NameError
|
6
17
|
def self.for object, lookup_class
|
7
18
|
default_name = lookup_class.name_for object.class
|
data/lib/magic/lookup/version.rb
CHANGED
data/lib/magic/lookup.rb
CHANGED
@@ -5,6 +5,32 @@ require_relative 'lookup/version'
|
|
5
5
|
require 'memery'
|
6
6
|
|
7
7
|
module Magic
|
8
|
+
# = Magic Lookup
|
9
|
+
#
|
10
|
+
# These are the steps to set up an automatic class inference:
|
11
|
+
#
|
12
|
+
# 1. Define a base class extending `Magic::Lookup`.
|
13
|
+
# 2. Define `.name_for` method for that class implementing your
|
14
|
+
# lookup logic.
|
15
|
+
# 3. From the base class, inherit classes to be looked up.
|
16
|
+
#
|
17
|
+
# Example:
|
18
|
+
#
|
19
|
+
# class Scope
|
20
|
+
# extend Magic::Lookup
|
21
|
+
#
|
22
|
+
# def self.name_for object_class
|
23
|
+
# object_class.name
|
24
|
+
# .delete_suffix('Model')
|
25
|
+
# .concat('Scope')
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# class MyScope < Scope
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# Scope.for MyModel # => MyScope
|
33
|
+
# Scope.for OtherModel # => nil
|
8
34
|
module Lookup
|
9
35
|
autoload :Error, 'magic/lookup/error'
|
10
36
|
autoload :Namespaces, 'magic/lookup/namespaces'
|
@@ -13,6 +39,7 @@ module Magic
|
|
13
39
|
|
14
40
|
memoize def for object_class, namespace = nil
|
15
41
|
descendants = self.descendants # cache
|
42
|
+
.union([ self ]) # including self
|
16
43
|
.reverse # most specific first
|
17
44
|
|
18
45
|
object_class.ancestors
|
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Senko
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 2025-05-20 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: memery
|
@@ -52,7 +52,7 @@ licenses:
|
|
52
52
|
metadata:
|
53
53
|
homepage_uri: https://github.com/Alexander-Senko/magic-lookup
|
54
54
|
source_code_uri: https://github.com/Alexander-Senko/magic-lookup
|
55
|
-
changelog_uri: https://github.com/Alexander-Senko/magic-lookup/blob/v0.
|
55
|
+
changelog_uri: https://github.com/Alexander-Senko/magic-lookup/blob/v0.3.0/CHANGELOG.md
|
56
56
|
rdoc_options: []
|
57
57
|
require_paths:
|
58
58
|
- lib
|
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
requirements: []
|
70
|
-
rubygems_version: 3.6.
|
70
|
+
rubygems_version: 3.6.5
|
71
71
|
specification_version: 4
|
72
72
|
summary: Related class inference with some magic involved
|
73
73
|
test_files: []
|