magic-support 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +7 -0
- data/CHANGELOG.md +16 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +239 -0
- data/Rakefile +8 -0
- data/lib/magic/core_ext/kernel/optional.rb +31 -0
- data/lib/magic/core_ext/kernel.rb +27 -0
- data/lib/magic/core_ext.rb +17 -0
- data/lib/magic/support/authors.rb +15 -0
- data/lib/magic/support/version.rb +7 -0
- data/lib/magic/support.rb +11 -0
- data/lib/rspec/method.rb +90 -0
- data/lib/rubygems/author.rb +63 -0
- data/sig/lib/magic/support/author.rbs +9 -0
- data/sig/magic/core_ext/kernel.rbs +7 -0
- data/sig/magic/core_ext.rbs +7 -0
- data/sig/magic/support.rbs +7 -0
- data/sig/rspec/method.rbs +5 -0
- data/sig/rubygems/author.rbs +11 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9f7037e20104047261af68821013d1bc6b2bb56fdd45b6f598d3cd59cf0eac11
|
4
|
+
data.tar.gz: c03b20aa6bf855ea037ec6779cb7c396705c7daed59a44fff7b515b82c63b6a9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b8d8c4f7b1af05776084e0a87839b357e9653267cac8c00a0fcd6f27e975e84f2b0b581ad3bbceecb1d5c45433334c0afbb6e11cd86c676a28593f6b3354673
|
7
|
+
data.tar.gz: 6bac701a2559fa55207d088da2d004b2d7e0cc012007f352e6c9d45fdcd577aa094fb71b1afbae1b7ade2be7706a1cc4846cfe45cdfa55299156b52028d8b6f7
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
## [0.1.0] — 2024-11-28
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
#### `Kernel`
|
6
|
+
|
7
|
+
- `#optional` as a conditional `#then`.
|
8
|
+
|
9
|
+
#### Gems
|
10
|
+
|
11
|
+
- `Gem::Author` for authors info to be used primarily in gem specs.
|
12
|
+
|
13
|
+
#### RSpec
|
14
|
+
|
15
|
+
- API for method specs (`rspec/method`).
|
16
|
+
- API for delegated methods.
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
Alexander.Senko@gmail.com.
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: 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,239 @@
|
|
1
|
+
# ✨ Magic Support
|
2
|
+
|
3
|
+
![GitHub Actions Workflow Status](
|
4
|
+
https://img.shields.io/github/actions/workflow/status/Alexander-Senko/magic-support/ci.yml
|
5
|
+
)
|
6
|
+
![Code Climate maintainability](
|
7
|
+
https://img.shields.io/codeclimate/maintainability-percentage/Alexander-Senko/magic-support
|
8
|
+
)
|
9
|
+
![Code Climate coverage](
|
10
|
+
https://img.shields.io/codeclimate/coverage/Alexander-Senko/magic-support
|
11
|
+
)
|
12
|
+
|
13
|
+
Magic Support is a collection of utility classes and standard library extensions
|
14
|
+
that were found useful for my pet projects.
|
15
|
+
|
16
|
+
It’s inspired by [Active Support](
|
17
|
+
https://github.com/rails/rails/tree/main/activesupport
|
18
|
+
).
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
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.
|
23
|
+
|
24
|
+
Install the gem and add to the application's Gemfile by executing:
|
25
|
+
|
26
|
+
```bash
|
27
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
28
|
+
```
|
29
|
+
|
30
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
31
|
+
|
32
|
+
```bash
|
33
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
34
|
+
```
|
35
|
+
|
36
|
+
## Core extensions
|
37
|
+
|
38
|
+
### Loading
|
39
|
+
|
40
|
+
Magic Support is broken into small pieces so that only the desired extensions can be loaded.
|
41
|
+
It also has some convenience entry points to load related extensions in one shot, even all of them.
|
42
|
+
|
43
|
+
#### Cherry-picking
|
44
|
+
|
45
|
+
For every single method defined as a core extension a note says where such a method is defined.
|
46
|
+
|
47
|
+
That means that you can `require` it like this:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'magic/core_ext/<module>/<extension>'
|
51
|
+
```
|
52
|
+
|
53
|
+
Magic Support has been carefully revised so that cherry-picking a file loads only strictly needed dependencies, if any.
|
54
|
+
|
55
|
+
#### Loading grouped core extensions
|
56
|
+
|
57
|
+
As a rule of thumb, extensions to `SomeClass` are available in
|
58
|
+
one shot by loading `magic/core_ext/some_class`.
|
59
|
+
|
60
|
+
Thus, to load all extensions to `Kernel`:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
require 'magic/core_ext/kernel'
|
64
|
+
```
|
65
|
+
|
66
|
+
#### Loading all core extensions
|
67
|
+
|
68
|
+
You may prefer just to load all core extensions, there is a file for that:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
require 'magic/core_ext'
|
72
|
+
```
|
73
|
+
|
74
|
+
### Extensions to all objects
|
75
|
+
|
76
|
+
#### `#optional`
|
77
|
+
|
78
|
+
Yields self to the block and returns the result of the block if it’s truthy, and self otherwise.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
rand(6) # returns 0–5
|
82
|
+
.optional { it + 1 if one_based? } # returns 1–6 if 1-based, or
|
83
|
+
# the original 0–5 otherwise
|
84
|
+
```
|
85
|
+
|
86
|
+
It can be considered as a conditional `#then`.
|
87
|
+
|
88
|
+
Good usage for `#optional` is value piping in method chains with conditional processing:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
@people = Person
|
92
|
+
.optional { it.where created_at: (1.hour.ago...) if new? }
|
93
|
+
.optional { anonymize it if gdpr? }
|
94
|
+
.optional { try :decorate }
|
95
|
+
```
|
96
|
+
|
97
|
+
Defined in `core_ext/kernel/optional`.
|
98
|
+
|
99
|
+
## Gems
|
100
|
+
|
101
|
+
### Author
|
102
|
+
|
103
|
+
It holds authors info to be used primarily in gem specs.
|
104
|
+
|
105
|
+
#### Loading
|
106
|
+
|
107
|
+
Pre-install Magic Support if you plan to use `Gem::Author` in your gemspec.
|
108
|
+
|
109
|
+
```bash
|
110
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
111
|
+
```
|
112
|
+
|
113
|
+
#### Usage
|
114
|
+
|
115
|
+
1. Inherit `Gem::Author` inside your gem and add the authors’ info.
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
require 'rubygems/author'
|
119
|
+
|
120
|
+
module MyLib
|
121
|
+
class Author < Gem::Author
|
122
|
+
new(
|
123
|
+
name: 'Your Name',
|
124
|
+
email: 'Your.Name@email.service',
|
125
|
+
github: 'Your-GitHub-Username',
|
126
|
+
)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
2. You can call some helper methods now.
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
Gem::Specification.new do |spec|
|
135
|
+
spec.name = 'my_lib'
|
136
|
+
spec.version = MyLib::VERSION
|
137
|
+
spec.authors = MyLib::Author.names
|
138
|
+
spec.email = MyLib::Author.emails
|
139
|
+
spec.homepage = "#{MyLib::Author.github_url}/#{spec.name}"
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
## RSpec
|
144
|
+
|
145
|
+
### Method specs
|
146
|
+
|
147
|
+
Enables one to write specs for single methods.
|
148
|
+
|
149
|
+
> [!WARNING]
|
150
|
+
> Planed for extraction into a separate gem.
|
151
|
+
|
152
|
+
#### Loading
|
153
|
+
|
154
|
+
Require it in `spec_helper.rb`:
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
require 'rspec/method'
|
158
|
+
```
|
159
|
+
|
160
|
+
#### Usage
|
161
|
+
|
162
|
+
Include a method reference into the description.
|
163
|
+
The reference should start with either
|
164
|
+
- `.` for class methods or
|
165
|
+
- `#` for instance ones.
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
RSpec.describe MyClass do
|
169
|
+
describe '.class_method' do
|
170
|
+
its([arg1, arg2]) { is_expected.to be return_value }
|
171
|
+
end
|
172
|
+
|
173
|
+
describe '#instance_method' do
|
174
|
+
its([arg1, arg2]) { is_expected.to be return_value }
|
175
|
+
end
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
> [!NOTE]
|
180
|
+
> Though `rspec/its` is not needed, it could come useful (see [the article on method testing](
|
181
|
+
> https://zverok.space/blog/2017-11-01-rspec-method-call.html
|
182
|
+
> )).
|
183
|
+
|
184
|
+
Within examples, `subject` is set to the corresponding `Method` instance.
|
185
|
+
In cases when the method couldn’t be found (e.g., due to delegation via `method_missing`), it’s set to a `Proc` instance
|
186
|
+
instead.
|
187
|
+
Anyway, one may treat it as something _callable_.
|
188
|
+
|
189
|
+
A method name is exposed as a `Symbol` via `method_name`.
|
190
|
+
|
191
|
+
> [!NOTE]
|
192
|
+
> `subject.name` may be undefined, use `method_name` instead.
|
193
|
+
|
194
|
+
##### Delegated methods
|
195
|
+
|
196
|
+
One can use `it_behaves_like :delegated` for delegated methods.
|
197
|
+
This will ensure that calling the method calls the one of the same name on a delegate under the hood passing it the arguments.
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
RSpec.describe MyClass do
|
201
|
+
describe '#method_with_arguments' do
|
202
|
+
it_behaves_like :delegated, to: delegate, with: [arg1, arg2]
|
203
|
+
end
|
204
|
+
|
205
|
+
describe '#method_without_arguments' do
|
206
|
+
it_behaves_like :delegated, to: delegate
|
207
|
+
end
|
208
|
+
end
|
209
|
+
```
|
210
|
+
|
211
|
+
##### Module specs
|
212
|
+
|
213
|
+
It’s recommended to use class method notation, when writing specs for module functions.
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
RSpec.describe MyModule do
|
217
|
+
describe '.module_function' do
|
218
|
+
# put the examples here
|
219
|
+
end
|
220
|
+
end
|
221
|
+
```
|
222
|
+
|
223
|
+
## Development
|
224
|
+
|
225
|
+
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.
|
226
|
+
|
227
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
228
|
+
|
229
|
+
## Contributing
|
230
|
+
|
231
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Alexander-Senko/magic-support. 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-support/blob/main/CODE_OF_CONDUCT.md).
|
232
|
+
|
233
|
+
## License
|
234
|
+
|
235
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
236
|
+
|
237
|
+
## Code of Conduct
|
238
|
+
|
239
|
+
Everyone interacting in the Magic::Support project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Alexander-Senko/magic-support/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'magic/core_ext'
|
4
|
+
|
5
|
+
Magic::CoreExt.kernel do
|
6
|
+
# Yields self to the block and returns the result of the block if it’s
|
7
|
+
# truthy, and self otherwise.
|
8
|
+
#
|
9
|
+
# rand(6) # returns 0–5
|
10
|
+
# .optional { it + 1 if one_based? } # returns 1–6 if 1-based, or
|
11
|
+
# # the original 0–5 otherwise
|
12
|
+
#
|
13
|
+
# It can be considered as a conditional +then+.
|
14
|
+
#
|
15
|
+
# Good usage for +optional+ is value piping in method chains with
|
16
|
+
# conditional processing:
|
17
|
+
#
|
18
|
+
# @people = Person
|
19
|
+
# .optional { it.where created_at: (1.hour.ago...) if new? }
|
20
|
+
# .optional { anonymize it if gdpr? }
|
21
|
+
# .optional { try :decorate }
|
22
|
+
#
|
23
|
+
def optional
|
24
|
+
tap do
|
25
|
+
result = yield(self) or
|
26
|
+
next
|
27
|
+
|
28
|
+
break result
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'magic/core_ext'
|
4
|
+
|
5
|
+
module Magic # :nodoc:
|
6
|
+
module CoreExt # :nodoc:
|
7
|
+
module Kernel
|
8
|
+
# Some classes copy Kernel methods on initialization instead of
|
9
|
+
# including Kernel itself. Thus, new methods should be defined
|
10
|
+
# explicitly for them.
|
11
|
+
MODULES = [ # classes/modules including Kernel methods
|
12
|
+
::Kernel,
|
13
|
+
(Delegator if defined? Delegator),
|
14
|
+
].compact.freeze
|
15
|
+
end
|
16
|
+
|
17
|
+
module_function
|
18
|
+
|
19
|
+
def kernel(&)
|
20
|
+
Module.new(&).tap do |kernel|
|
21
|
+
Kernel::MODULES.each { _1.include kernel }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
CoreExt.require_all __FILE__
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Magic # :nodoc:
|
4
|
+
module CoreExt # :nodoc:
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def require_all scope = __FILE__
|
8
|
+
scope
|
9
|
+
.delete_suffix('.rb')
|
10
|
+
.then { Pathname _1 }
|
11
|
+
.glob('*.rb')
|
12
|
+
.each { require _1 }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
CoreExt.require_all
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../../rubygems/author'
|
4
|
+
|
5
|
+
module Magic
|
6
|
+
module Support
|
7
|
+
class Author < Gem::Author # :nodoc:
|
8
|
+
new(
|
9
|
+
name: 'Alexander Senko',
|
10
|
+
email: 'Alexander.Senko@gmail.com',
|
11
|
+
github: 'Alexander-Senko',
|
12
|
+
)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/rspec/method.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'magic/core_ext/kernel/optional'
|
5
|
+
|
6
|
+
module RSpec # :nodoc:
|
7
|
+
module Method
|
8
|
+
REFERENCE = {
|
9
|
+
scope: /[.#]/, # class/instance
|
10
|
+
name: /\S+/, # may consist of non-word characters
|
11
|
+
}
|
12
|
+
.map { |name, pattern| "(?<#{name}>#{pattern})" }
|
13
|
+
.join
|
14
|
+
.then { /(?:^|\s)#{_1}(?:$|\s)/ } # may be surrounded by some text
|
15
|
+
|
16
|
+
# @api public
|
17
|
+
# Container module for method specs.
|
18
|
+
module ExampleGroup
|
19
|
+
extend ActiveSupport::Concern
|
20
|
+
|
21
|
+
included do
|
22
|
+
metadata[:type] ||= :method
|
23
|
+
|
24
|
+
description.match(REFERENCE) in { scope:, name: } or
|
25
|
+
raise ArgumentError, "'#{description}' doesn't look like a method reference"
|
26
|
+
|
27
|
+
subject_method = instance_method :subject
|
28
|
+
|
29
|
+
while subject_method.owner.then { not _1.respond_to? :metadata or _1.metadata[:method] }
|
30
|
+
subject_method = subject_method.super_method or break
|
31
|
+
end
|
32
|
+
|
33
|
+
define_method :subject do
|
34
|
+
receiver =
|
35
|
+
case [ scope, subject_method&.bind_call(self) || super() ]
|
36
|
+
in '#', receiver
|
37
|
+
receiver
|
38
|
+
in '.', Module => receiver
|
39
|
+
receiver
|
40
|
+
in '.', receiver
|
41
|
+
receiver.class
|
42
|
+
end
|
43
|
+
|
44
|
+
receiver.method name
|
45
|
+
rescue NameError
|
46
|
+
# TODO: emit a warning
|
47
|
+
|
48
|
+
-> *args { receiver.send name, *args }
|
49
|
+
end
|
50
|
+
|
51
|
+
let(:method_name) { name.to_sym }
|
52
|
+
end
|
53
|
+
|
54
|
+
class_methods do
|
55
|
+
def subject(&)
|
56
|
+
prepend Module.new {
|
57
|
+
define_method(:subject) { super().unbind.bind instance_eval(&) }
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
next unless defined? RSpec::Its
|
62
|
+
|
63
|
+
def its_result(*args, &) = its(args, &)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
shared_context :method, description: Method::REFERENCE do
|
69
|
+
include Method::ExampleGroup
|
70
|
+
|
71
|
+
shared_context :delegated do |to:, with: nil|
|
72
|
+
before do
|
73
|
+
allow(to).to receive(method_name)
|
74
|
+
.optional { _1.with *with if with }
|
75
|
+
.and_call_original
|
76
|
+
end
|
77
|
+
|
78
|
+
it "delegates to #{to.inspect}" do
|
79
|
+
subject[*with]
|
80
|
+
|
81
|
+
expect(to).to have_received(method_name)
|
82
|
+
.once
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
configure do
|
88
|
+
_1.backtrace_exclusion_patterns << %r'/lib/rspec/method'
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Gem # :nodoc:
|
4
|
+
# # Gem Author
|
5
|
+
#
|
6
|
+
# This class holds authors info to be used primarily in gem specs.
|
7
|
+
#
|
8
|
+
# ## Usage
|
9
|
+
#
|
10
|
+
# 1. Inherit +Gem::Author+ inside your gem and add the authors’ info.
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# module MyLib
|
15
|
+
# class Author < Gem::Author
|
16
|
+
# new(
|
17
|
+
# name: 'Your Name',
|
18
|
+
# email: 'Your.Name@email.service',
|
19
|
+
# github: 'Your-GitHub-Username',
|
20
|
+
# )
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# 2. You can call some helper methods now.
|
25
|
+
#
|
26
|
+
# Example:
|
27
|
+
#
|
28
|
+
# Gem::Specification.new do |spec|
|
29
|
+
# spec.name = 'my_lib'
|
30
|
+
# spec.version = MyLib::VERSION
|
31
|
+
# spec.authors = MyLib::Author.names
|
32
|
+
# spec.email = MyLib::Author.emails
|
33
|
+
# spec.homepage = "#{MyLib::Author.github_url}/#{spec.name}"
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
Author ||= Struct.new(
|
37
|
+
:name,
|
38
|
+
:email,
|
39
|
+
:github,
|
40
|
+
) do
|
41
|
+
def self.inherited child
|
42
|
+
super
|
43
|
+
|
44
|
+
child.class_eval do
|
45
|
+
class << self
|
46
|
+
attr_reader :all
|
47
|
+
|
48
|
+
include Author::ClassMethods
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module self::ClassMethods # rubocop:disable Lint, Style
|
54
|
+
def new(...) = (@all ||= []) << super
|
55
|
+
|
56
|
+
def names = all.filter_map &:name
|
57
|
+
def emails = all.filter_map &:email
|
58
|
+
def github_url = all.filter_map(&:github_url).first
|
59
|
+
end
|
60
|
+
|
61
|
+
def github_url = github && "https://github.com/#{github}"
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: magic-support
|
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-11-28 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: activesupport
|
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: Utility classes and Ruby extensions beyond Active Support.
|
27
|
+
email:
|
28
|
+
- Alexander.Senko@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- ".rspec"
|
34
|
+
- ".rubocop.yml"
|
35
|
+
- CHANGELOG.md
|
36
|
+
- CODE_OF_CONDUCT.md
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/magic/core_ext.rb
|
41
|
+
- lib/magic/core_ext/kernel.rb
|
42
|
+
- lib/magic/core_ext/kernel/optional.rb
|
43
|
+
- lib/magic/support.rb
|
44
|
+
- lib/magic/support/authors.rb
|
45
|
+
- lib/magic/support/version.rb
|
46
|
+
- lib/rspec/method.rb
|
47
|
+
- lib/rubygems/author.rb
|
48
|
+
- sig/lib/magic/support/author.rbs
|
49
|
+
- sig/magic/core_ext.rbs
|
50
|
+
- sig/magic/core_ext/kernel.rbs
|
51
|
+
- sig/magic/support.rbs
|
52
|
+
- sig/rspec/method.rbs
|
53
|
+
- sig/rubygems/author.rbs
|
54
|
+
homepage: https://github.com/Alexander-Senko/magic-support
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata:
|
58
|
+
source_code_uri: https://github.com/Alexander-Senko/magic-support
|
59
|
+
changelog_uri: https://github.com/Alexander-Senko/magic-support/blob/v0.1.0/CHANGELOG.md
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.2'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubygems_version: 3.6.0.dev
|
75
|
+
specification_version: 4
|
76
|
+
summary: Active Support extended
|
77
|
+
test_files: []
|