Skip to content

Commit

Permalink
Merge branch 'hotfix/0.6.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
thornomad committed Sep 2, 2020
2 parents 45817fc + fad375f commit 08267bb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lib/enumbler.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
22 changes: 22 additions & 0 deletions lib/enumbler/core_ext/symbol/case_equality_operator.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/enumbler/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Enumbler
VERSION = '0.6.7'
VERSION = '0.6.8'
end
27 changes: 27 additions & 0 deletions spec/enumbler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 08267bb

Please sign in to comment.