ecase 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: 958bdcccde592678024d9bcddfb7192ccbc2510891b3b1848406ebdd76a8f9bb
4
+ data.tar.gz: 04b8522bcd3d4d190b8bb904dffd560c2b0c47ed6f7c41eb73dfec358a2a76e9
5
+ SHA512:
6
+ metadata.gz: d1cc85a117e5707a11ae4a41f20818f1eb1e625d22bfffab6fafa54cba590ac839cf5d4c2abe68150cdbc0264123e988bb291c10dc327c7fe98648900062e7eb
7
+ data.tar.gz: 225344c403adde9c58157280bd05d3075f317fc7820504895c9e3ede44e2d6904feaa99599269826bb2d2ca4f7222b88b9a2707e6c5a89254c4097a53d2141be
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-03-10
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 joel.drapper@shopify.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/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ecase.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ecase (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.15.0)
10
+ rake (13.0.6)
11
+
12
+ PLATFORMS
13
+ arm64-darwin-20
14
+ arm64-darwin-21
15
+
16
+ DEPENDENCIES
17
+ ecase!
18
+ minitest (~> 5.0)
19
+ rake (~> 13.0)
20
+
21
+ BUNDLED WITH
22
+ 2.3.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Joel Drapper
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,88 @@
1
+ # Ecase
2
+
3
+ Ecase allows you to write exhaustive case statements that ensure all cases are covered.
4
+
5
+ ### Basic usage
6
+ You can use an `ecase` pretty much the same way you'd use a normal `case` statement. The difference is you must pass an Enumerable list of expected cases that must be covered by the statement.
7
+
8
+ Let's say you have a class `Car` with an attribute `color` that corresponds to the name of the paint: `:red`, `:green` or `blue`. You want to define a method `color_hex` that returns the correct HEX code for the color. By using an ecase, you can guarantee the statement has a definition for each possible color.
9
+
10
+ ```ruby
11
+ class Car
12
+ VALID_COLORS = [:red, :green, :blue]
13
+
14
+ attr_reader :color
15
+
16
+ def color_hex
17
+ ecase paint, VALID_COLORS do
18
+ on(:red) { "#ff0000" }
19
+ on(:green) { "#00ff00" }
20
+ on(:blue) { "#0000ff" }
21
+ end
22
+ end
23
+ end
24
+ ```
25
+
26
+ If we add `:green` to the list of `VALID_COLORS`, the ecase will raise an `Ecase::Error` complaining that we missed a case. It can do this because we passed in a list of expected cases.
27
+
28
+ ### LiteralEnums
29
+ If you're using ecases with the `literal_enums` gem, you can pass the enum type as the second argument, since enums are Enumerable.
30
+
31
+ ```ruby
32
+ class Message < Enum
33
+ Greeting()
34
+ Farewell()
35
+ end
36
+
37
+ def say(message)
38
+ ecase message, Message do
39
+ on(Greeting) { "Hello, nice to meet you." }
40
+ on(Farewell) { "Goodbye 👋" }
41
+ end
42
+ end
43
+ ```
44
+
45
+ ### Advanced usage
46
+ You can also use an Ecase as part of your own API to demand a case definition.
47
+
48
+ ```ruby
49
+ def some_method(&block)
50
+ Enum.new(list_of_things_that_must_be_defined, &block)
51
+ end
52
+ ```
53
+
54
+ You can also return an Ecase instance which is Enumerable, yielding a `condition` and `block`.
55
+
56
+ ## Installation
57
+
58
+ Add this line to your application's Gemfile:
59
+
60
+ ```ruby
61
+ gem 'ecase'
62
+ ```
63
+
64
+ And then execute:
65
+
66
+ $ bundle install
67
+
68
+ Or install it yourself as:
69
+
70
+ $ gem install ecase
71
+
72
+ ## Development
73
+
74
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
75
+
76
+ 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).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ecase. 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/[USERNAME]/ecase/blob/master/CODE_OF_CONDUCT.md).
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
85
+
86
+ ## Code of Conduct
87
+
88
+ Everyone interacting in the Ecase project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ecase/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Ecase
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ecase.rb ADDED
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+ require "set"
3
+
4
+ require_relative "ecase/version"
5
+
6
+ class Ecase
7
+ Error = Class.new(StandardError)
8
+
9
+ include Enumerable
10
+
11
+ def initialize(expected_cases, &block)
12
+ @expected_cases = expected_cases.to_a
13
+ @handled_cases = {}
14
+
15
+ instance_exec(&block)
16
+
17
+ freeze
18
+
19
+ ensure_no_missing_cases
20
+ ensure_no_excess_cases
21
+ end
22
+
23
+ def each(&block)
24
+ @handled_cases.each(&block)
25
+ end
26
+
27
+ def [](candidate)
28
+ @handled_cases.fetch(candidate) do
29
+ types.find { |k, _v| candidate.is_a? k }&.last ||
30
+ raise(Error, "No cases matching: #{candidate}.")
31
+ end
32
+ end
33
+
34
+ def conditions
35
+ @handled_cases.keys
36
+ end
37
+
38
+ def blocks
39
+ @handled_cases.values
40
+ end
41
+
42
+ protected
43
+
44
+ def on(*conditions, &block)
45
+ conditions.each do |condition|
46
+ if @handled_cases.key?(condition)
47
+ raise Error, "You already handled the case: #{condition}."
48
+ else
49
+ @handled_cases[condition] = block
50
+ end
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def types
57
+ @handled_cases.lazy.select do |key, _value|
58
+ key.is_a?(Class) || key.is_a?(Module)
59
+ end
60
+ end
61
+
62
+ def ensure_no_missing_cases
63
+ if missing_cases.any?
64
+ raise Error, "You're missing case(s): #{missing_cases.join(', ')}."
65
+ end
66
+ end
67
+
68
+ def ensure_no_excess_cases
69
+ if excess_cases.any?
70
+ raise Error, "You defined illegal case(s): #{excess_cases.join(', ')}."
71
+ end
72
+ end
73
+
74
+ def missing_cases
75
+ @expected_cases - @handled_cases.keys
76
+ end
77
+
78
+ def excess_cases
79
+ @handled_cases.keys - @expected_cases
80
+ end
81
+ end
82
+
83
+ def ecase(value, expected_cases, &block)
84
+ Ecase.new(expected_cases, &block)[value].call
85
+ end
data/sig/ecase.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ecase
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ecase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel Drapper
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Exhaustive case statments that ensure all possible cases are covered.
14
+ email:
15
+ - joel@drapper.me
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - CODE_OF_CONDUCT.md
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/ecase.rb
28
+ - lib/ecase/version.rb
29
+ - sig/ecase.rbs
30
+ homepage: https://github.com/joeldrapper/ecase
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://github.com/joeldrapper/ecase
35
+ source_code_uri: https://github.com/joeldrapper/ecase
36
+ changelog_uri: https://github.com/joeldrapper/ecase
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 3.1.1
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.3.7
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Exhaustive case statements for Ruby.
56
+ test_files: []