joeldrapper / ecase

Exhaustive case statements for Ruby.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ecase

Ecase allows you to write exhaustive case statements that ensure all cases are covered.

Basic usage

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.

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.

class Car
  VALID_COLORS = [:red, :green, :blue]

  attr_reader :color

  def color_hex
    ecase color, VALID_COLORS do
      on(:red) { "#ff0000" }
      on(:green) { "#00ff00" }
      on(:blue) { "#0000ff" }
    end
  end
end

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.

LiteralEnums

If you're using ecases with the literal_enums gem, you can pass the enum type as the second argument, since enums are Enumerable.

class Message < Enum
  Greeting()
  Farewell()
end

def say(message)
  ecase message, Message do
    on(Greeting) { "Hello, nice to meet you." }
    on(Farewell) { "Goodbye 👋" }
  end
end

Advanced usage

You can also use an Ecase as part of your own API to demand a case definition.

def some_method(&block)
  Ecase.new(list_of_things_that_must_be_defined, &block)
end

You can also return an Ecase instance which is Enumerable, yielding a condition and block.

Installation

Add this line to your application's Gemfile:

gem 'ecase'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install ecase

Development

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.

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.

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Ecase project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

Exhaustive case statements for Ruby.

License:MIT License


Languages

Language:Ruby 97.9%Language:Shell 2.1%