magic-decorator 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: 05eb0d1c9c1f08235269894f1ce906ca355d65239d9a708ad68078d9ebce7159
4
+ data.tar.gz: 679f113b552348de25243303de4550cda0774dd5e284bd4ec64c94c79471aecf
5
+ SHA512:
6
+ metadata.gz: fb7bd90db8afc2235cc5a1b7f572b316a5494888a34a07052a7d77a2b20abc8d07444dca8e2e8436bb0a1dd6e8e61f8ddf15c80ed54ac5ee956a99e90bfbac4d
7
+ data.tar.gz: ddac7d0a18af55a99a561812660847f6935dd849c42287dd397466c520c392742580331526b33b98efe9a64020a9622e6dee92e60e177f08c8c0a5c4851310c3
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,10 @@
1
+ ## [0.1.0] — 2024-10-13
2
+
3
+ ### Added
4
+
5
+ - `Magic::Decorator::Base` — a basic decorator class.
6
+ - `Magic::Decoratable` to be included in decoratable classes.
7
+ - `#decorate`,
8
+ - `#decorate!`,
9
+ - `#decorated`,
10
+ - `#decorated?`.
@@ -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,91 @@
1
+ # 🪄 Magic Decorator
2
+
3
+ ![Code Climate maintainability](
4
+ https://img.shields.io/codeclimate/maintainability-percentage/Alexander-Senko/magic-decorator
5
+ )
6
+ ![Code Climate coverage](
7
+ https://img.shields.io/codeclimate/coverage/Alexander-Senko/magic-decorator
8
+ )
9
+
10
+ A bit of history:
11
+ this gem was inspired by digging deeper into [Draper](https://github.com/drapergem/draper) with an eye on a refactoring.
12
+
13
+ It implements a general decorator logic. It’s not meant to be a _presenter_.
14
+
15
+ ## Installation
16
+
17
+ Install the gem and add to the application's Gemfile by executing:
18
+
19
+ $ bundle add magic-decorator
20
+
21
+ If bundler is not being used to manage dependencies, install the gem by executing:
22
+
23
+ $ gem install magic-decorator
24
+
25
+ ## Usage
26
+
27
+ `Magic::Decorator::Base` is a basic decorator class to be inherited by any other decorator.
28
+ It further inherits from [`SimpleDelegator`](
29
+ https://docs.ruby-lang.org/en/master/SimpleDelegator.html
30
+ ) and is straightforward like that.
31
+
32
+ ```ruby
33
+ class PersonDecorator < Magic::Decorator::Base
34
+ def name = "#{first_name} #{last_name}"
35
+ end
36
+
37
+ Person = Struct.new :first_name, :last_name do
38
+ include Magic::Decoratable
39
+ end
40
+
41
+ person = Person.new('John', 'Smith').decorate
42
+ person.name # => "John Smith"
43
+ ```
44
+
45
+ ### `Magic::Decoratable`
46
+
47
+ This module adds three methods to decorate an object.
48
+ Decorator class is being inferred automatically.
49
+ When no decorator is found,
50
+ * `#decorate` returns `nil`,
51
+ * `#decorate!` raises `Magic::Lookup::Error`,
52
+ * `#decorated` returns the original object.
53
+
54
+ One can test for the object is actually decorated with `#decorated?`.
55
+
56
+ ```ruby
57
+ 'with no decorator for String'.decorated.decorated? # => false
58
+ ['with a decorator for Array'].decorated.decorated? # => true
59
+ ```
60
+
61
+ #### Magic
62
+
63
+ `Decoratable` is mixed into `Object` by default. That means that effectively any object is `Decoratable`.
64
+
65
+ ### Decorator class inference
66
+
67
+ Decorators provide automatic class inference for any object based on its class name
68
+ powered by [Magic Lookup](
69
+ https://github.com/Alexander-Senko/magic-lookup
70
+ ).
71
+
72
+ For example, `MyNamespace::MyModel.new.decorate` looks for `MyNamespace::MyModelDecorator` first.
73
+ When missing, it further looks for decorators for its ancestor classes, up to `ObjectDecorator`.
74
+
75
+ ## Development
76
+
77
+ 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.
78
+
79
+ To install this gem onto your local machine, run `bundle exec rake install`.
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Alexander-Senko/magic-decorator. 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-decorator/blob/main/CODE_OF_CONDUCT.md).
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
88
+
89
+ ## Code of Conduct
90
+
91
+ Everyone interacting in the Magic Decorator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Alexander-Senko/magic-decorator/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,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magic
4
+ module Decoratable
5
+ def decorate = decorator&.new self
6
+ def decorate! = decorate || raise(Lookup::Error.for self, Decorator)
7
+ def decorated = decorate || self
8
+ def decorated? = false
9
+
10
+ private
11
+
12
+ def decorator = Decorator.for self.class
13
+ end
14
+ end
@@ -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 Decorator
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,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'delegate'
4
+
5
+ module Magic
6
+ module Decorator
7
+ class Base < SimpleDelegator
8
+ extend Lookup
9
+
10
+ class << self
11
+ def name_for(object_class) = "#{object_class}Decorator"
12
+ end
13
+
14
+ def decorated? = true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Magic
4
+ module Decorator
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'magic/lookup'
4
+
5
+ require_relative 'decorator/version'
6
+
7
+ module Magic
8
+ autoload :Decoratable, 'magic/decoratable'
9
+
10
+ Object.include Decoratable
11
+
12
+ module Decorator
13
+ autoload :Base, 'magic/decorator/base'
14
+
15
+ module_function
16
+
17
+ def for(...) = Base.for(...)
18
+ def name_for(...) = Base.name_for(...)
19
+ end
20
+ 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,13 @@
1
+ module Magic
2
+ module Decoratable
3
+ def decorate: -> Decorator?
4
+ def decorate!: -> Decorator
5
+ def decorated: -> (Decorator | self)
6
+
7
+ def decorated?: -> bool
8
+
9
+ private
10
+
11
+ def decorator: -> Class?
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Magic
2
+ module Decorator
3
+ class Base
4
+ extend Lookup
5
+
6
+ def decorated?: -> bool
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Magic
2
+ module Decorator
3
+ VERSION: String
4
+ AUTHORS: Array[Gem::Author]
5
+
6
+ def for: (Class) -> Class?
7
+ def name_for: (Module) -> String
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic-decorator
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-13 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: magic-lookup
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: 'SimpleDelegator on steroids: automatic delegation, decorator class inference,
27
+ etc.'
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/decoratable.rb
42
+ - lib/magic/decorator.rb
43
+ - lib/magic/decorator/authors.rb
44
+ - lib/magic/decorator/base.rb
45
+ - lib/magic/decorator/version.rb
46
+ - sig/gem.rbs
47
+ - sig/magic/decoratable.rbs
48
+ - sig/magic/decorator.rbs
49
+ - sig/magic/decorator/base.rbs
50
+ homepage: https://github.com/Alexander-Senko/magic-decorator
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/Alexander-Senko/magic-decorator
55
+ source_code_uri: https://github.com/Alexander-Senko/magic-decorator
56
+ changelog_uri: https://github.com/Alexander-Senko/magic-decorator/blob/v0.1.0/CHANGELOG.md
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '3.1'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.6.0.dev
72
+ specification_version: 4
73
+ summary: Decorators with some internal magic
74
+ test_files: []