Skip to content

Commit 0304477

Browse files
authored
Merge pull request #46 from peterfication/i18n-as-optional-dependency
Make i18n dependency optional
2 parents 989f1f8 + 5b3a41c commit 0304477

13 files changed

Lines changed: 196 additions & 19 deletions

File tree

.github/workflows/test.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ jobs:
2424
- run: bundle install --jobs=3 --retry=3 --path=vendor/bundle
2525
- run: bundle exec rake spec
2626
continue-on-error: ${{ matrix.entry.allowed-failure }}
27+
- name: Specs for when the i18n gem is not available
28+
run: |
29+
cd spec_i18n
30+
bundle install --jobs=3 --retry=3
31+
pwd
32+
bundle exec rake spec
33+
continue-on-error: ${{ matrix.entry.allowed-failure }}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 1.0.0 (Next)
22

3+
* [#41](https://github.com/dblock/ruby-enum/pull/41): Make i18n dependency optional - [@peterfication](https://github.com/peterfication).
34
* [#43](https://github.com/dblock/ruby-enum/pull/43): Add exhaustive case matcher - [@peterfication](https://github.com/peterfication).
45
* [#40](https://github.com/dblock/ruby-enum/pull/39): Enable new Rubocop cops and address/allowlist lints - [@petergoldstein](https://github.com/petergoldstein).
56
* [#39](https://github.com/dblock/ruby-enum/pull/39): Require Ruby >= 2.7 - [@petergoldstein](https://github.com/petergoldstein).

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Enum-like behavior for Ruby, heavily inspired by [this](http://www.rubyfleebie.c
2525
- [Duplicate enumerator keys or duplicate values](#duplicate-enumerator-keys-or-duplicate-values)
2626
- [Inheritance](#inheritance)
2727
- [Exhaustive case matcher](#exhaustive-case-matcher)
28+
- [I18n support](#i18n-support)
2829
- [Benchmarks](#benchmarks)
2930
- [Contributing](#contributing)
3031
- [Copyright and License](#copyright-and-license)
@@ -300,6 +301,15 @@ Color.Case(color, {
300301
})
301302
```
302303

304+
### I18n support
305+
306+
This gem has an optional dependency to `i18n`. If it's available, the error messages will have a nice description and can be translated. If it's not available, the errors will only contain the message keys.
307+
308+
```ruby
309+
# Add this to your Gemfile if you want to have a nice error description instead of just a message key.
310+
gem "i18n"
311+
```
312+
303313
## Benchmarks
304314

305315
Benchmark scripts are defined in the [`benchmarks`](benchmarks) folder and can be run with Rake:

lib/ruby-enum.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
# frozen_string_literal: true
22

3-
require 'i18n'
4-
53
require 'ruby-enum/version'
64
require 'ruby-enum/enum'
75
require 'ruby-enum/enum/case'
6+
require 'ruby-enum/enum/i18n_mock'
7+
8+
# Try to load the I18n gem and provide a mock if it is not available.
9+
begin
10+
require 'i18n'
11+
Ruby::Enum.i18n = I18n
12+
rescue LoadError
13+
# I18n is not available
14+
# :nocov:
15+
# Tests for this loading are in the spec_i18n folder
16+
Ruby::Enum.i18n = Ruby::Enum::I18nMock
17+
# :nocov:
18+
end
819

9-
I18n.load_path << File.join(File.dirname(__FILE__), 'config', 'locales', 'en.yml')
20+
Ruby::Enum.i18n.load_path << File.join(File.dirname(__FILE__), 'config', 'locales', 'en.yml')
1021

1122
require 'ruby-enum/errors/base'
1223
require 'ruby-enum/errors/uninitialized_constant_error'

lib/ruby-enum/enum.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
module Ruby
44
module Enum
5+
class << self
6+
# Needed for I18n mock
7+
attr_accessor :i18n
8+
end
9+
510
attr_reader :key, :value
611

712
def initialize(key, value)

lib/ruby-enum/enum/i18n_mock.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
# :nocov:
4+
module Ruby
5+
module Enum
6+
##
7+
# Mock I18n module in case the i18n gem is not available.
8+
module I18nMock
9+
def self.load_path
10+
[]
11+
end
12+
13+
def self.translate(key, _options = {})
14+
key
15+
end
16+
end
17+
end
18+
end
19+
# :nocov:

lib/ruby-enum/errors/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def compose_message(key, attributes = {})
3939
#
4040
# Returns a localized error message string.
4141
def translate(key, options)
42-
::I18n.translate("#{BASE_KEY}.#{key}", locale: :en, **options).strip
42+
Ruby::Enum.i18n.translate("#{BASE_KEY}.#{key}", locale: :en, **options).strip
4343
end
4444

4545
# Create the problem.

ruby-enum.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ Gem::Specification.new do |s|
1616
s.homepage = 'http://github.com/dblock/ruby-enum'
1717
s.licenses = ['MIT']
1818
s.summary = 'Enum-like behavior for Ruby.'
19-
s.add_dependency 'i18n'
2019
s.metadata['rubygems_mfa_required'] = 'true'
2120
end

spec/ruby-enum/enum_spec.rb

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,24 @@ class SecondSubclass < FirstSubclass
2323
expect(Colors::GREEN).to eq 'green'
2424
end
2525

26-
it 'raises UninitializedConstantError on an invalid constant' do
27-
expect { Colors::ANYTHING }.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /The constant Colors::ANYTHING has not been defined./
26+
context 'when the i18n gem is loaded' do
27+
it 'raises UninitializedConstantError on an invalid constant' do
28+
expect do
29+
Colors::ANYTHING
30+
end.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /The constant Colors::ANYTHING has not been defined./
31+
end
32+
end
33+
34+
context 'when the i18n gem is not loaded' do
35+
before do
36+
allow(described_class).to receive(:i18n).and_return(Ruby::Enum::I18nMock)
37+
end
38+
39+
it 'raises UninitializedConstantError on an invalid constant' do
40+
expect do
41+
Colors::ANYTHING
42+
end.to raise_error Ruby::Enum::Errors::UninitializedConstantError, /ruby.enum.errors.messages.uninitialized_constant.summary/
43+
end
2844
end
2945

3046
describe '#each' do
@@ -151,22 +167,54 @@ class SecondSubclass < FirstSubclass
151167
end
152168

153169
context 'when a duplicate key is used' do
154-
it 'raises DuplicateKeyError' do
155-
expect do
156-
Colors.class_eval do
157-
define :RED, 'some'
158-
end
159-
end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /The constant Colors::RED has already been defined./
170+
context 'when the i18n gem is loaded' do
171+
it 'raises DuplicateKeyError' do
172+
expect do
173+
Colors.class_eval do
174+
define :RED, 'some'
175+
end
176+
end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /The constant Colors::RED has already been defined./
177+
end
178+
end
179+
180+
context 'when the i18n gem is not loaded' do
181+
before do
182+
allow(described_class).to receive(:i18n).and_return(Ruby::Enum::I18nMock)
183+
end
184+
185+
it 'raises DuplicateKeyError' do
186+
expect do
187+
Colors.class_eval do
188+
define :RED, 'some'
189+
end
190+
end.to raise_error Ruby::Enum::Errors::DuplicateKeyError, /ruby.enum.errors.messages.duplicate_key.message/
191+
end
160192
end
161193
end
162194

163195
context 'when a duplicate value is used' do
164-
it 'raises a DuplicateValueError' do
165-
expect do
166-
Colors.class_eval do
167-
define :Other, 'red'
168-
end
169-
end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /The value red has already been defined./
196+
context 'when the i18n gem is loaded' do
197+
it 'raises a DuplicateValueError' do
198+
expect do
199+
Colors.class_eval do
200+
define :Other, 'red'
201+
end
202+
end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /The value red has already been defined./
203+
end
204+
end
205+
206+
context 'when the i18n gem is not loaded' do
207+
before do
208+
allow(described_class).to receive(:i18n).and_return(Ruby::Enum::I18nMock)
209+
end
210+
211+
it 'raises a DuplicateValueError' do
212+
expect do
213+
Colors.class_eval do
214+
define :Other, 'red'
215+
end
216+
end.to raise_error Ruby::Enum::Errors::DuplicateValueError, /ruby.enum.errors.messages.duplicate_value.summary/
217+
end
170218
end
171219
end
172220

spec_i18n/Gemfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
source 'http://rubygems.org'
4+
5+
gemspec path: '../', name: 'ruby-enum'
6+
7+
# This Gemfile should not include any gem that has i18n as a dependency.
8+
gem 'rake'
9+
gem 'rspec', '~> 3.0'

0 commit comments

Comments
 (0)