magic-lookup 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 165ed4c613f3808aa064d20eecc5137a20b74ffca108e4b4edaab39e1de5c710
4
+ data.tar.gz: 8c57c00e753425063568765d62f4879a3848de727431663b4199c1e2cc119cd2
5
+ SHA512:
6
+ metadata.gz: a42291c47d5e2c2d4545f0c7cbb842c03c39ae4f683b177c339832864c1a33fc98b1cc2deadb15e16e87b069cf557fce80e5f8b70ee6bb479921b658704ae208
7
+ data.tar.gz: 04b6aaf11baa0644333869e36a0045503b996f13bb1854cc8670d2510220865310e8f01a8b406b56d424328dde9e0690d3d5d5b81cf58cd9c59079fa9eda56fa
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ inherit_from: https://github.com/Alexander-Senko/Alexander-Senko/raw/refs/heads/main/.rubocop.yml
2
+
3
+ require:
4
+ - rubocop-rspec
5
+ - rubocop-rake
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-10-07
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at Alexander.Senko@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Alexander Senko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ ![Code Climate maintainability](
2
+ https://img.shields.io/codeclimate/maintainability-percentage/Alexander-Senko/magic-lookup
3
+ )
4
+ ![Code Climate coverage](
5
+ https://img.shields.io/codeclimate/coverage/Alexander-Senko/magic-lookup
6
+ )
7
+
8
+ ## What is it for?
9
+
10
+ A bit of history: this gem was inspired by digging deeper into [Draper](https://github.com/drapergem/draper) with an eye on refactoring.
11
+
12
+ A common task for an application is to match its parts against each other, be it inferring a default model class for a controller, or looking up a decorator for a model, or whatever else.
13
+
14
+ Modern frameworks tend to use naming conventions over configuration to achieve that.
15
+ Unfortunately, every one has to implement them on its own struggling through numerous bugs.
16
+ Moreover, inconsistencies across these implementations lead to misunderstanding and endless tickets when actual behavior fails to meet user expectations based on other frameworks.
17
+
18
+ So, meet
19
+
20
+ # 🔮 Magic Lookup
21
+
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
+
24
+ ## Installation
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
+ Install the gem and add to the application's Gemfile by executing:
29
+
30
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
31
+
32
+ If bundler is not being used to manage dependencies, install the gem by executing:
33
+
34
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
35
+
36
+ ## Usage
37
+
38
+ These are the steps to set up an automatic class inference:
39
+
40
+ 1. Define a base class extending `Magic::Lookup`.
41
+ 2. Define `.name_for` method for that class implementing your lookup logic.
42
+ 3. From the base class, inherit classes to be looked up.
43
+
44
+ ```ruby
45
+ class Scope
46
+ extend Magic::Lookup
47
+
48
+ def self.name_for object_class
49
+ object_class.name
50
+ .delete_suffix('Model')
51
+ .concat('Scope')
52
+ end
53
+ end
54
+
55
+ class MyScope < Scope
56
+ end
57
+
58
+ Scope.for MyModel # => MyScope
59
+ Scope.for OtherModel # => nil
60
+ ```
61
+
62
+ ### Exception handling
63
+
64
+ When no class is found, `nil` is returned. If you need to raise an exception in this case, you can use `Magic::Lookup::Error` like this:
65
+
66
+ ```ruby
67
+ scope_class = Scope.for(object.class) or
68
+ raise Magic::Lookup::Error.for(object, Scope)
69
+ ```
70
+
71
+ `Magic::Lookup::Error` is never raised internally and is meant to be used in your code that implements the lookup logic.
72
+
73
+ ## Features
74
+
75
+ ### Inheritance
76
+
77
+ Lookup is provided not only for the class itself, but to any of its ancestors as well.
78
+
79
+ ### Namespaces
80
+
81
+ Both of matching classes — the target and its match — may be namespaced independently.
82
+
83
+ > [!TIP]
84
+ > Until a comprehensive documentation on all the use cases is released, the spec is recommended as further reading.
85
+ > One can access it by running `rake` in the gem directory.
86
+ > The output is quite descriptive to get familiar with the use cases.
87
+
88
+ ## Development
89
+
90
+ 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.
91
+
92
+ To install this gem onto your local machine, run `bundle exec rake install`.
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Alexander-Senko/magic-lookup. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/Alexander-Senko/magic-lookup/blob/main/CODE_OF_CONDUCT.md).
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
101
+
102
+ ## Code of Conduct
103
+
104
+ Everyone interacting in the Magic Lookup project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Alexander-Senko/magic-lookup/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Author ||= Struct.new(
4
+ :name,
5
+ :email,
6
+ :github,
7
+ ) do
8
+ def github_url = github && "https://github.com/#{github}"
9
+ end
10
+
11
+ module Magic
12
+ module Lookup
13
+ AUTHORS = [
14
+ Gem::Author.new(
15
+ name: 'Alexander Senko',
16
+ email: 'Alexander.Senko@gmail.com',
17
+ github: 'Alexander-Senko',
18
+ ),
19
+ ]
20
+
21
+ class << AUTHORS
22
+ def names = filter_map &:name
23
+ def emails = filter_map &:email
24
+ def github_url = filter_map(&:github_url).first
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magic
4
+ module Lookup
5
+ class Error < NameError
6
+ def self.for object, lookup_class
7
+ default_name = lookup_class.name_for object.class
8
+
9
+ new "no #{lookup_class} found for #{object.class}, default name is #{default_name}",
10
+ default_name, receiver: object
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magic
4
+ module Lookup
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lookup/version'
4
+
5
+ require 'memery'
6
+
7
+ module Magic
8
+ module Lookup
9
+ autoload :Error, 'magic/lookup/error'
10
+
11
+ include Memery
12
+
13
+ memoize def for object_class
14
+ descendants = self.descendants # cache
15
+ .reverse # most specific first
16
+
17
+ object_class.ancestors
18
+ .lazy # optimization
19
+ .filter(&:name)
20
+ .map { name_for _1 }
21
+ .filter_map do |class_name|
22
+ descendants.find { _1.name == class_name }
23
+ end
24
+ .first
25
+ end
26
+
27
+ def name_for(object_class) = raise NotImplementedError
28
+
29
+ def descendants
30
+ [
31
+ *subclasses,
32
+ *subclasses.flat_map(&__method__),
33
+ ]
34
+ end
35
+ end
36
+ end
data/sig/gem.rbs ADDED
@@ -0,0 +1,5 @@
1
+ module Gem
2
+ class Author
3
+ def github_url: () -> String?
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module Magic
2
+ module Lookup
3
+ class Error < NameError
4
+ def self.for: (any) -> Error
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Magic
2
+ module Lookup
3
+ VERSION: String
4
+ AUTHORS: Array[Gem::Author]
5
+
6
+ def for: (Class) -> Class?
7
+
8
+ def name_for: (Module) -> String
9
+
10
+ def descendants: -> Array[Class]
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic-lookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Senko
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2024-10-10 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: memery
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ description: Find a related class for an object (ex., a decorator, a presenter, a
27
+ controller, or whatever).
28
+ email:
29
+ - Alexander.Senko@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/magic/lookup.rb
42
+ - lib/magic/lookup/authors.rb
43
+ - lib/magic/lookup/error.rb
44
+ - lib/magic/lookup/version.rb
45
+ - sig/gem.rbs
46
+ - sig/magic/lookup.rbs
47
+ - sig/magic/lookup/error.rbs
48
+ homepage: https://github.com/Alexander-Senko/magic-lookup
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/Alexander-Senko/magic-lookup
53
+ source_code_uri: https://github.com/Alexander-Senko/magic-lookup
54
+ changelog_uri: https://github.com/Alexander-Senko/magic-lookup/blob/v0.1.0/CHANGELOG.md
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.2'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.6.0.dev
70
+ specification_version: 4
71
+ summary: Related class inference with some magic involved
72
+ test_files: []