diff --git a/Gemfile.lock b/Gemfile.lock index afda651..65147c3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - enumbler (0.6.7) + enumbler (0.6.8) activerecord (>= 5.2.3, < 6.1) activesupport (>= 5.2.3, < 6.1) diff --git a/README.md b/README.md index 2d7ecc9..850e1cc 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,18 @@ class Feeling < ApplicationRecord end ``` +## Core ext + +Adds case equality power to the `Symbol` class allowing you to use case methods directly against an enabled instance: + +```ruby +case Color.black +when :black + 'not surprised' +when :blue, :purple + 'very surprised' +end + ## Development 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. diff --git a/lib/enumbler.rb b/lib/enumbler.rb index dc00f64..b0248f1 100644 --- a/lib/enumbler.rb +++ b/lib/enumbler.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'enumbler/core_ext/symbol/case_equality_operator' + require 'enumbler/collection' require 'enumbler/enumble' require 'enumbler/enabler' diff --git a/lib/enumbler/core_ext/symbol/case_equality_operator.rb b/lib/enumbler/core_ext/symbol/case_equality_operator.rb new file mode 100644 index 0000000..f1fd501 --- /dev/null +++ b/lib/enumbler/core_ext/symbol/case_equality_operator.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +# Use case equality operator with an enabled class. +# +# case House.black +# when :black +# 'this is true' +# when :blue, :purple +# 'this is not' +# end +class Symbol + def ===(other) + super || + other.class.included_modules.include?(Enumbler::Enabler) && + other.enumble.enum == self + + # Calling #enumble on a new instance that has not been defined raises an + # error, so catching that edge case here + rescue Enumbler::Error + false + end +end diff --git a/lib/enumbler/version.rb b/lib/enumbler/version.rb index 10a54a3..04cc1ba 100644 --- a/lib/enumbler/version.rb +++ b/lib/enumbler/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Enumbler - VERSION = '0.6.7' + VERSION = '0.6.8' end diff --git a/spec/enumbler_spec.rb b/spec/enumbler_spec.rb index eafe548..0b54709 100644 --- a/spec/enumbler_spec.rb +++ b/spec/enumbler_spec.rb @@ -340,4 +340,31 @@ class Palette < ApplicationRecord end end end + + describe 'core_ext/symbol', :seed do + def test(enumbled_instance) + case enumbled_instance + when :black + 'it is black' + when :white, :dark_brown + 'white or brown' + else + 'nope' + end + end + + it 'provides a case quality operator' do + expect(test(Color.black)).to eq 'it is black' + expect(test(Color.dark_brown)).to eq 'white or brown' + expect(test(Color.infinity)).to eq 'nope' + end + + it 'does not raise an error when the other object is not enabled as an enumbler' do + expect { test(Color) }.not_to raise_error + expect { test(Color.new) }.not_to raise_error + + expect(test(Color.new)).to eq 'nope' + expect(test(Color)).to eq 'nope' + end + end end