Skip to content

Commit

Permalink
Add feature toggle for countries
Browse files Browse the repository at this point in the history
  • Loading branch information
ulm committed Jul 6, 2018
1 parent 0e3f9ba commit b5f8edc
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 35 deletions.
12 changes: 12 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ c.in_eu? # => false

## Currencies

To enable currencies extension please add the following to countries initializer.

``` ruby
ISO3166.configuration.enable_currency_extension!
```

Please note that it requires you to add "money" dependency to your gemfile.

``` ruby
gem "money", "~> 6.9"
```

**WARNING** if you have a top level class named `Money` you will conflict with this gem. If this is a large issue we will add a feature to turn currency features off.

Countries now uses the [Money](https://github.com/RubyMoney/money) gem. What this means is you now get back a Money::Currency object that gives you access to all the currency information.
Expand Down
2 changes: 1 addition & 1 deletion countries.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
gem.license = 'MIT'

gem.add_dependency('i18n_data', '~> 0.8.0')
gem.add_dependency('money', '~> 6.9')
gem.add_dependency('i18n', '~> 1.0.1')
gem.add_dependency('unicode_utils', '~> 1.4')
gem.add_dependency('sixarm_ruby_unaccent', '~> 1.1')
gem.add_development_dependency('rspec', '>= 3')
Expand Down
5 changes: 5 additions & 0 deletions lib/countries/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def initialize
@loaded_locales = []
end

def enable_currency_extension!
require 'countries/country/currency_methods'
ISO3166::Country.prepend(ISO3166::CountryCurrencyMethods)
end

private

def default_locales
Expand Down
4 changes: 0 additions & 4 deletions lib/countries/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ def <=>(other)
to_s <=> other.to_s
end

def currency
Money::Currency.find(data['currency_code'])
end

def start_of_week
data['start_of_week']
end
Expand Down
10 changes: 10 additions & 0 deletions lib/countries/country/currency_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'money'

module ISO3166
# Optional extension which allows you to get back a Money::Currency object with all the currency info
module CountryCurrencyMethods
def currency
Money::Currency.find(data['currency_code'])
end
end
end
3 changes: 2 additions & 1 deletion lib/countries/iso3166.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'yaml'
require 'json'
require 'i18n_data'
require 'money'
require 'i18n'

require 'countries/kwarg_struct'
require 'countries/configuration'
Expand Down
56 changes: 56 additions & 0 deletions spec/00_global_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
describe 'global Country class', order: :defined do
context "when loaded via 'iso3166' existence" do
subject { defined?(Country) }

it { is_expected.to be_falsey }
end

context "when loaded via 'global'" do
before { require 'countries/global' }

it 'does not pollute global namespace with Money gem' do
expect(defined?(Money)).not_to be
expect(ISO3166::Country.new('DE').respond_to?(:currency)).not_to be
end

describe 'existence' do
subject { defined?(Country) }

it { is_expected.to be_truthy }
end

describe 'superclass' do
subject { Country.superclass }

it { is_expected.to eq(ISO3166::Country) }
end

describe 'to_s' do
it 'should return the country name' do
expect(Country.new('GB').to_s).to eq('United Kingdom of Great Britain and Northern Ireland')
end
end

describe 'currency' do
it { expect(Country.new('GB').respond_to?(:currency)).not_to be }

context 'I enable currency extension via config' do
before { ISO3166.configuration.enable_currency_extension! }

it 'should add currency to global country' do
expect(Country.new('GB').currency).to be
end
end
end

context 'I enable currency extension via config' do
before { ISO3166.configuration.enable_currency_extension! }

it 'should add currency to global country' do
expect(Country.new('GB').currency).to be
expect(defined?(Money)).to be
expect(ISO3166::Country.new('DE').currency).to be
end
end
end
end
1 change: 1 addition & 0 deletions spec/country_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'spec_helper'
NUM_OF_COUNTRIES = 249
describe ISO3166::Country do
before { ISO3166.configuration.enable_currency_extension! }
let(:country) { ISO3166::Country.search('US') }

it 'allows to create a country object from a symbol representation of the alpha2 code' do
Expand Down
29 changes: 0 additions & 29 deletions spec/global_spec.rb

This file was deleted.

0 comments on commit b5f8edc

Please sign in to comment.