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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89553c6220236db6f6b88312bb5a8097718f383a107ab2a85e7c7e9c8ee9ebf1
4
- data.tar.gz: 865c7c71973d94e809374270428b2532960a3b4a8a64dfd9393927942d140141
3
+ metadata.gz: 6c703f8d45523eb9fe156018f415b24d1eef923ae8abe72b549a8f66944b3d02
4
+ data.tar.gz: 7f473974c2a805aa8bfe766fb1fb0a58692098338a99f09dffbde0b2a6617899
5
5
  SHA512:
6
- metadata.gz: 6496b67373aada9b47a0e6af847dca2485da0bb2d683b9eeed46dfbb15f55dedb78cf522da5ed03bc428de427ec60701629e377c1d9a786fe98a46a64b13347b
7
- data.tar.gz: 9b9fe4f56b6d5ed85e957ec1bba331574a4f38677c0cf5e9c2225808ba00edac760256978b51ebd644a70bf68aee771bbc8dd0ffca519d38c46c6a2565ea30df
6
+ metadata.gz: 68be2c2fb418fe82891c30e8fbc5e8a8b84bcdc3eed3eee8b696ce2cbbf8f538f92b950af599fd3710d977f15c8c0f8f9816784e55454b0995e333b8fa9d63cc
7
+ data.tar.gz: b00b16af1e3aedc9a5c900ef9144f3e669fae21d862b0d856819dfb7eac90320597c496a2ff56f1e117f265cb58ec9b337c1c173cc985868e47b87a812b6f433
data/.rubocop.yml CHANGED
@@ -1,5 +1,7 @@
1
- inherit_from: https://github.com/Alexander-Senko/Alexander-Senko/raw/refs/heads/main/.rubocop.yml
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
- require:
5
+ plugins:
4
6
  - rubocop-rspec
5
7
  - rubocop-rake
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.3.0] — 2025-05-20
2
+
3
+ ### Added
4
+
5
+ - `Magic::Lookup#for` to consider `self` a part of a lookup scope.
6
+
7
+
1
8
  ## [0.2.0] — 2024-10-19
2
9
 
3
10
  ### Added
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ ![GitHub Actions Workflow Status](
2
+ https://img.shields.io/github/actions/workflow/status/Alexander-Senko/magic-lookup/ci.yml
3
+ )
1
4
  ![Code Climate maintainability](
2
5
  https://img.shields.io/codeclimate/maintainability-percentage/Alexander-Senko/magic-lookup
3
6
  )
@@ -17,9 +20,9 @@ Moreover, inconsistencies across these implementations lead to misunderstanding
17
20
 
18
21
  So, meet
19
22
 
20
- # 🔮Magic Lookup
23
+ # 🔮 Magic Lookup
21
24
 
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.
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
- `Magic::Lookup::Error` is never raised internally and is meant to be used in your code that implements the lookup logic.
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
- ## 🔮Magic
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.
@@ -9,8 +9,8 @@ Gem::Author ||= Struct.new(
9
9
  end
10
10
 
11
11
  module Magic
12
- module Lookup
13
- AUTHORS = [
12
+ module Lookup # :nodoc:
13
+ AUTHORS = [ # rubocop:disable Style/MutableConstant
14
14
  Gem::Author.new(
15
15
  name: 'Alexander Senko',
16
16
  email: 'Alexander.Senko@gmail.com',
@@ -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
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Magic
4
4
  module Lookup
5
- module Namespaces
5
+ module Namespaces # :nodoc:
6
6
  attr_writer :namespaces
7
7
 
8
8
  def namespaces = @namespaces ||= [ nil ]
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Magic
4
4
  module Lookup
5
- VERSION = '0.2.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
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.2.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: 2024-10-19 00:00:00.000000000 Z
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.2.0/CHANGELOG.md
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.0.dev
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: []