inclusive 1.0.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: 3e972fc6385483574fb08cfcc3a4cf320b162b8e7bd726a1074e917a308114bc
4
+ data.tar.gz: 6d1fed490b2da13b72acaaa16ebacaea3623fdd4d98904523ee8e6d6f7428ef7
5
+ SHA512:
6
+ metadata.gz: 22c5f46c10692394cff02977e1017b84009ff19f268357703b84c3ce1c2d06ca4c5f8a839dcedf98cb9dfeedb7a0f070bd27ef923df0bebcb57977cabc5bd4cc
7
+ data.tar.gz: 9e22d54b55eb4c10ecac7b1c5962dca0772ec66a06d3728e22a06a1c663afbf971524095b1516fb7ec80cc9bb3429cb9c5346f7711af3aaf2c1f57c25e8404f7
data/.rubocop.yml ADDED
@@ -0,0 +1,30 @@
1
+ require:
2
+ - rubocop-minitest
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.1
7
+ NewCops: enable
8
+
9
+ Naming/VariableNumber:
10
+ Enabled: false
11
+
12
+ Style/EndlessMethod:
13
+ Enabled: false
14
+
15
+ Style/CommentedKeyword:
16
+ Enabled: false
17
+
18
+ Style/StringLiterals:
19
+ Enabled: true
20
+ EnforcedStyle: double_quotes
21
+
22
+ Style/StringLiteralsInInterpolation:
23
+ Enabled: true
24
+ EnforcedStyle: double_quotes
25
+
26
+ Layout/LineLength:
27
+ Max: 120
28
+
29
+ Metrics/MethodLength:
30
+ Max: 20
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.4
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [1.0.0] - 2024-06-30
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 jared@jaredwhite.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 Jared White & Bridgetown maintainers
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,136 @@
1
+ # Inclusive
2
+
3
+ Compose globally-scoped Ruby modules into local packages. This makes it easy to access utility methods (aka functions) without having to type lengthy namespaces or remember which modules provide with functions at each call site (or alternatively include modules in your classes which pollute the method space).
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add inclusive
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install inclusive
14
+
15
+ ## Usage
16
+
17
+ To start with, you can write your "package" (aka a standard Ruby module) containing a set of discrete functions. It's recommended you namespace your packages within higher-level modules.
18
+
19
+ ```ruby
20
+ module MyOrg
21
+ module MyPackages
22
+ module WorkHardAtIt
23
+ def just_do_it = puts "Don't let your dreams be dreams"
24
+ end
25
+ end
26
+ end
27
+ ```
28
+
29
+ Then in any standard Ruby class, you can include the `Inclusive` module and use the `packages` class helper to "import" the package:
30
+
31
+ ```ruby
32
+ require "inclusive"
33
+ # require the file(s) containing your package(s)
34
+
35
+ class GetToThePoint
36
+ include Inclusive
37
+
38
+ packages def work_hard = [MyOrg::MyPackages::WorkHardAtIt]
39
+
40
+ def nothing_is_impossible
41
+ work_hard.just_do_it # this will print out the motivational speech
42
+ end
43
+ end
44
+ ```
45
+
46
+ The import syntax is an array because you can import multiple packages. The imported packages will "compose" together, meaning the methods from the various package modules will all be available simultaneously.
47
+
48
+ In addition to creating instance methods using the `packages` class helper, you can use the `packages` method inline:
49
+
50
+ ```ruby
51
+ def some_method
52
+ my_math = packages[MyOrg::Math]
53
+
54
+ my_math.multiply_by_100(5)
55
+ end
56
+ ```
57
+
58
+ This approach isn't recommended unless you're in a context where using the class helper is impossible, such as a template (ERB, etc.) or a block which is executed by a framework. You can also call the `packages` method directly on the `Inclusive` module:
59
+
60
+ ```ruby
61
+ my_math = Inclusive.packages(MyOrg::Math)
62
+ ```
63
+
64
+ If you want to be able to call a package method directly on its own module, you can extend your module and use the `public_function` helper:
65
+
66
+ ```ruby
67
+ module Packages
68
+ module MyPackage
69
+ extend Inclusive::Public
70
+
71
+ def some_method
72
+ # code
73
+ end
74
+
75
+ public_function :some_method
76
+ end
77
+ end
78
+ ```
79
+
80
+ Now in addition to using package imports via Inclusive, you can call the module method directly:
81
+
82
+ ```ruby
83
+ Packages::MyPackage.some_method
84
+ ```
85
+
86
+ This is only recommended if you need to mantain an existing module's legacy behavior in a codebase while incrementally adopting Inclusive.
87
+
88
+ ### Packages Are Duplicated
89
+
90
+ One of the aspects of Inclusive which make it more useful than merely using standard Ruby modules is each imported package is actually a cloned module. This means a module can actually contain internal state, much like an object:
91
+
92
+ ```ruby
93
+ module Packages
94
+ module Ownership
95
+ attr_accessor :owner
96
+
97
+ def owner_classname
98
+ owner.class.name
99
+ end
100
+ end
101
+ end
102
+
103
+ class SomeObject
104
+ def try_out_ownership
105
+ ownership = packages[Package::Ownership].tap { _1.owner = self }
106
+
107
+ puts ownership.owner_classname # this will be `SomeObject`
108
+ end
109
+ end
110
+
111
+ class SomeOtherObject
112
+ def try_out_ownership
113
+ ownership = packages[Package::Ownership].tap { _1.owner = self }
114
+
115
+ puts ownership.owner_classname # this will be `SomeOtherObject`
116
+ end
117
+ end
118
+ ```
119
+
120
+ ## Development
121
+
122
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
123
+
124
+ To install this gem onto your local machine, run `bin/rake install`. To release a new version, update the version number in `version.rb`, and then run `bin/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).
125
+
126
+ ## Contributing
127
+
128
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bridgetownrb/inclusive. 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/bridgetownrb/inclusive/blob/main/CODE_OF_CONDUCT.md).
129
+
130
+ ## License
131
+
132
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
133
+
134
+ ## Code of Conduct
135
+
136
+ Everyone interacting in the Inclusive project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bridgetownrb/inclusive/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Inclusive
4
+ VERSION = "1.0.0"
5
+ end
data/lib/inclusive.rb ADDED
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ require_relative "inclusive/version"
6
+
7
+ # Include this in any class so you have access to the `packages` method (both class and instance).
8
+ module Inclusive
9
+ # You can extend a module with this in order to use `public_function`, which is the same as
10
+ # Ruby's native `module_function` except the instance method retains public visibility—a necessity
11
+ # for packaging via Inclusive.
12
+ module Public
13
+ def public_function(method_name)
14
+ module_function method_name
15
+ public method_name # reset back to public visibility
16
+ end
17
+ end
18
+
19
+ extend Inclusive::Public
20
+
21
+ # This is the base module which will be cloned for each individual import scenario. You should
22
+ # never need to reference this module directly.
23
+ module ModuleWithPackages
24
+ # Use this for the inline packages syntax.
25
+ #
26
+ # @example importing packages
27
+ # utils = packages[UtilitiesPackage, Another::Package]
28
+ # utils.make_it(:so)
29
+ def self.[](*packages)
30
+ packages.each { |package| extend_with_package(package) }
31
+ self
32
+ end
33
+
34
+ def self.extend_with_package(package)
35
+ @package_names ||= Set.new
36
+ @package_methods ||= Set.new
37
+
38
+ warn_on_overwritten_methods(package)
39
+ package.instance_methods.each { @package_methods << _1 }
40
+ @package_names << package.name
41
+
42
+ extend package
43
+ end
44
+
45
+ def self.warn_on_overwritten_methods(package)
46
+ overwriting_methods = @package_methods.select { package.instance_methods.include? _1 }
47
+ return unless overwriting_methods.length.positive?
48
+
49
+ warn "Inclusive <#{@package_names.join(", ")}> - The following methods will be overridden by " \
50
+ "'#{package.name}':\n #{overwriting_methods.join(", ")}"
51
+ end
52
+
53
+ def self.to_s = "#{name}<#{@package_names.join(", ")}>"
54
+
55
+ def self.inspect = to_s
56
+
57
+ def self.loaded_packages
58
+ singleton_class.included_modules.reverse.select { @package_names.include? _1.name }
59
+ end
60
+
61
+ def self.package_methods
62
+ loaded_packages.flat_map(&:instance_methods)
63
+ end
64
+ end
65
+
66
+ # This will extend your class by default if you `include Inclusive`, but if for some reason you
67
+ # don't want the `packages` instance method in your class, you can simply
68
+ # `extend Inclusive::Class`
69
+ module Class
70
+ # Use this as a decorator for an instance method which you will use to access your package
71
+ # imports.
72
+ #
73
+ # @example method definition
74
+ # packages def utilities = [Package1, Package2]
75
+ #
76
+ # def some_logic_here
77
+ # utilities.do_stuff # from Package1
78
+ # utilities.convert(x) # from Package2
79
+ # end
80
+ #
81
+ # This new method will be set to private, unless you use `public_packages` instead of `packages`.
82
+ #
83
+ # @return [void]
84
+ def packages(method_name)
85
+ old_method_name = :"__old_#{method_name}__"
86
+ ivar_name = :"@_#{method_name}"
87
+
88
+ alias_method old_method_name, method_name
89
+
90
+ define_method method_name do
91
+ return instance_variable_get(ivar_name) if instance_variable_defined?(ivar_name)
92
+
93
+ packages_to_extend = send(old_method_name)
94
+ ModuleWithPackages.dup.tap do |mod|
95
+ mod.module_eval do
96
+ def self.name = "ModuleWithPackages" # preserve module name
97
+ end
98
+ packages_to_extend.each { |package| mod.extend_with_package(package) }
99
+ instance_variable_set(ivar_name, mod)
100
+ end
101
+ end
102
+
103
+ private method_name unless __callee__ == :public_packages
104
+ end
105
+
106
+ alias public_packages packages
107
+ end
108
+
109
+ def self.included(klass)
110
+ klass.extend self::Class
111
+ end
112
+
113
+ # Returns a blank module ready for package imports, useful for inline package access when an
114
+ # instance method is not suitable (perhaps you're in a template or block context).
115
+ #
116
+ # @return [ModuleWithPackages]
117
+ def packages = ModuleWithPackages.dup.tap do |mod|
118
+ mod.module_eval do
119
+ def self.name = "ModuleWithPackages" # preserve module name
120
+ end
121
+ end
122
+
123
+ public_function :packages
124
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inclusive
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bridgetown Team
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: maintainers@bridgetownrb.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".rubocop.yml"
20
+ - ".ruby-version"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/inclusive.rb
27
+ - lib/inclusive/version.rb
28
+ homepage: https://github.com/bridgetownrb/inclusive
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ homepage_uri: https://github.com/bridgetownrb/inclusive
33
+ source_code_uri: https://github.com/bridgetownrb/inclusive
34
+ rubygems_mfa_required: 'true'
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.1.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.3.26
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Compose globally-scoped Ruby modules into local packages
54
+ test_files: []