selectable 0.1.1

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: 1c184f65648419452c898a1697c93557083ff7eef92f502d08b45375c3bf4c4a
4
+ data.tar.gz: bd0a463d1c73f7d5d7f8f03021fb3656931ea5547aef6b096290c5da5b6763bd
5
+ SHA512:
6
+ metadata.gz: c2b2fa9874cdbb36aeb87c1e80b96114d26015b7db824f3540bafb50566091da40b7a907cbf5e35c3a5206a4e166df9758b43c086c435eab1da7e2a5bc81eaef
7
+ data.tar.gz: 01c8595367df05440353d5195322b1708ab7e502cb658436f5cf494c24ee1049552bc5b05d7495228420257ca62a4d6784151e22fb93dc68870e9c3ee862ec8a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,24 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.0
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - spec/**/*_spec.rb
11
+
12
+ Style/StringLiterals:
13
+ Enabled: true
14
+ EnforcedStyle: double_quotes
15
+
16
+ Style/StringLiteralsInInterpolation:
17
+ Enabled: true
18
+ EnforcedStyle: double_quotes
19
+
20
+ Layout/LineLength:
21
+ Max: 120
22
+
23
+ Metrics/MethodLength:
24
+ Max: 20
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-08-18
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 TODO: Write your email address. 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 erickcantwell
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 TODO: Write your name
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,109 @@
1
+ # Selectable
2
+
3
+ Selectable aims to help match dynamic input to modular code that was written to handle specific use cases. In a nutshell, you can think of Selectable as a more powerful and dynamic case statement where functionality can be added without having to curate a list of when clauses.
4
+
5
+ Please use caution when using this gem. While it should be safe in most cases, there is inherently risk associated with accepting somewhat arbitrary user input.
6
+
7
+ ## Installation
8
+
9
+ Pull down the repo from github. In the project root execute the following commands to install dependencies, build the gem, and install it:
10
+
11
+ ```
12
+ bundle install
13
+ gem build
14
+ gem install <gemfilefrombuild>
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Basic usage:
20
+
21
+ ```
22
+ s = Selectable.new(namespace: MyModules)
23
+ selected_module = s.for('input_string')
24
+ ```
25
+
26
+ ### Complete Example
27
+
28
+ Create a YAML file with the following contents:
29
+
30
+ ```
31
+ ---
32
+ services:
33
+ service_1:
34
+ config:
35
+ option1: value1
36
+ service_2:
37
+ config:
38
+ option2: value2
39
+ service_3:
40
+ config:
41
+ option3: value3
42
+ ```
43
+
44
+
45
+
46
+ ```
47
+ #!/usr/bin/env ruby
48
+ # frozen_string_literal: true
49
+
50
+ require "selectable"
51
+ require "yaml"
52
+
53
+ module MyModules
54
+ module ModuleHandler
55
+ def self.selectable_for
56
+ ["service_1"]
57
+ end
58
+
59
+ def self.process(config)
60
+ puts "Processing #{config}"
61
+ end
62
+ end
63
+ end
64
+
65
+ module MyModules
66
+ class ClassHandler
67
+ def self.selectable_for
68
+ ["service_2"]
69
+ end
70
+
71
+ def process(config)
72
+ puts "Processing #{config}"
73
+ end
74
+ end
75
+ end
76
+
77
+ services = YAML.safe_load(File.read('service_config.yml'))
78
+
79
+ s = Selectable.new(namespace: MyModules)
80
+ puts "The following inputs are selectable:"
81
+ puts s.selectors.inspect
82
+ puts
83
+
84
+ services["services"].each do |service, config|
85
+ puts "Is #{service} selectable: #{s.selectable?(service)}"
86
+ if s.selectable?(service)
87
+ processor = s.for(service).respond_to?(:new) ? s.for(service).new : s.for(service)
88
+ processor.process(config)
89
+ end
90
+ end
91
+ ```
92
+
93
+ ## Development
94
+
95
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
96
+
97
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `selectable.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).
98
+
99
+ ## Contributing
100
+
101
+ Bug reports and pull requests are welcome on GitHub at https://github.com/erickcantwell/selectable. 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/erickcantwell/selectable/blob/main/CODE_OF_CONDUCT.md).
102
+
103
+ ## License
104
+
105
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
106
+
107
+ ## Code of Conduct
108
+
109
+ Everyone interacting in the Selectable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/erickcantwell/selectable/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 "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/lib/selectable.rb ADDED
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Error < StandardError; end
4
+ class UnSelectable < StandardError; end
5
+
6
+ # Selectable: Help load classes and modules for a specific use case based
7
+ # on user input
8
+ #
9
+ # Attributes:
10
+ # namespace: A module or class namespace to load
11
+ # downcase: Whether or not to downcase inputs.
12
+ # Default: true
13
+ class Selectable
14
+ VERSION = "0.1.1"
15
+
16
+ attr_reader :namespace, :downcase
17
+
18
+ def initialize(namespace: nil, downcase: true)
19
+ @namespace = namespace
20
+ @downcase = downcase
21
+ end
22
+
23
+ # Returns a hash of selectable modules and classes from the namespace
24
+ # provided and their mapped inputs
25
+ #
26
+ # Example:
27
+ # s = Selectable.new(namespace: MyModules
28
+ # s.selectable
29
+ def selectable
30
+ return @selectable if @selectable
31
+
32
+ selector_data = {}
33
+ namespace.constants.each do |c|
34
+ selector = namespace.const_get(c)
35
+ next unless can_be_selected(selector)
36
+
37
+ s = selector.selectable_for
38
+ raise Error, "selectable_for must be an array in #{selector}" unless s.is_a?(Array)
39
+
40
+ s.each { |x| x.downcase! if can_downcase(x) }
41
+ selector_data[selector] = s
42
+ end
43
+ @selectable = selector_data
44
+ end
45
+
46
+ # Returns a flatted array of selectable inputs
47
+ #
48
+ # Example:
49
+ # s = Selectable.new(namespace: MyModules)
50
+ # s.selectors
51
+ def selectors
52
+ selectable.values.flatten
53
+ end
54
+
55
+ # Returns true or false when given an input object
56
+ #
57
+ # Example:
58
+ # s = Selectable.new(namespace: MyModules)
59
+ # s.selectable?('someinput')
60
+ def selectable?(object)
61
+ object.downcase! if can_downcase(object)
62
+ return true if selectors.include?(object)
63
+
64
+ false
65
+ end
66
+
67
+ # Returns the selected module or class based on the input given
68
+ #
69
+ # Example:
70
+ # s = Selectable.new(namespace: MyModules)
71
+ # selected_module = s.for('input')
72
+ #
73
+ # Raises "UnSelectable" if the input is not a valid selectable input
74
+ def for(thing)
75
+ selectable.each do |obj, values|
76
+ thing.downcase! if can_downcase(thing)
77
+ return obj if values.include?(thing)
78
+ end
79
+
80
+ raise UnSelectable, "#{thing} is unselectable"
81
+ end
82
+
83
+ private
84
+
85
+ def can_be_selected(obj)
86
+ (obj.is_a?(Module) || obj.is_a?(Class)) && obj.respond_to?(:selectable_for)
87
+ end
88
+
89
+ def can_downcase(obj)
90
+ downcase && obj.respond_to?(:downcase) && !obj.frozen?
91
+ end
92
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/selectable"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "selectable"
7
+ spec.version = Selectable::VERSION
8
+ spec.authors = ["Erick Cantwell"]
9
+ spec.email = ["erick@erickcantwell.com"]
10
+
11
+ spec.summary = "Create arbitrary selectable classes and modules"
12
+ spec.description = "Load all of the classes and modules in a namespace so that they are selectable"
13
+ spec.homepage = "https://github.com/erickcantwell/selectable"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/erickcantwell/selectable.git"
19
+ spec.metadata["changelog_uri"] = "https://github.com/erickcantwell/selectable/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) ||
26
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ # spec.add_dependency "example-gem", "~> 1.0"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
@@ -0,0 +1,4 @@
1
+ module Selectable
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: selectable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Erick Cantwell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Load all of the classes and modules in a namespace so that they are selectable
14
+ email:
15
+ - erick@erickcantwell.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - LICENSE
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/selectable.rb
29
+ - selectable.gemspec
30
+ - sig/selectable.rbs
31
+ homepage: https://github.com/erickcantwell/selectable
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/erickcantwell/selectable
36
+ source_code_uri: https://github.com/erickcantwell/selectable.git
37
+ changelog_uri: https://github.com/erickcantwell/selectable/blob/main/CHANGELOG.md
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.4.19
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Create arbitrary selectable classes and modules
57
+ test_files: []