From b5f8edc1852f64742e25bdee9f75fe51368b0136 Mon Sep 17 00:00:00 2001 From: ulm Date: Fri, 6 Jul 2018 19:33:41 +0300 Subject: [PATCH] Add feature toggle for countries --- README.markdown | 12 +++++ countries.gemspec | 2 +- lib/countries/configuration.rb | 5 ++ lib/countries/country.rb | 4 -- lib/countries/country/currency_methods.rb | 10 ++++ lib/countries/iso3166.rb | 3 +- spec/00_global_spec.rb | 56 +++++++++++++++++++++++ spec/country_spec.rb | 1 + spec/global_spec.rb | 29 ------------ 9 files changed, 87 insertions(+), 35 deletions(-) create mode 100644 lib/countries/country/currency_methods.rb create mode 100644 spec/00_global_spec.rb delete mode 100644 spec/global_spec.rb diff --git a/README.markdown b/README.markdown index 9e08d078d..628f14898 100644 --- a/README.markdown +++ b/README.markdown @@ -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. diff --git a/countries.gemspec b/countries.gemspec index 3875b82af..927d9bdb0 100644 --- a/countries.gemspec +++ b/countries.gemspec @@ -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') diff --git a/lib/countries/configuration.rb b/lib/countries/configuration.rb index fd679f376..d4dd41b41 100644 --- a/lib/countries/configuration.rb +++ b/lib/countries/configuration.rb @@ -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 diff --git a/lib/countries/country.rb b/lib/countries/country.rb index df713d392..e85baad27 100644 --- a/lib/countries/country.rb +++ b/lib/countries/country.rb @@ -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 diff --git a/lib/countries/country/currency_methods.rb b/lib/countries/country/currency_methods.rb new file mode 100644 index 000000000..8edd7f945 --- /dev/null +++ b/lib/countries/country/currency_methods.rb @@ -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 diff --git a/lib/countries/iso3166.rb b/lib/countries/iso3166.rb index c9159b03f..33234224a 100644 --- a/lib/countries/iso3166.rb +++ b/lib/countries/iso3166.rb @@ -1,6 +1,7 @@ require 'yaml' +require 'json' require 'i18n_data' -require 'money' +require 'i18n' require 'countries/kwarg_struct' require 'countries/configuration' diff --git a/spec/00_global_spec.rb b/spec/00_global_spec.rb new file mode 100644 index 000000000..6868568ea --- /dev/null +++ b/spec/00_global_spec.rb @@ -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 diff --git a/spec/country_spec.rb b/spec/country_spec.rb index afbc66e9e..c61cbe5ee 100644 --- a/spec/country_spec.rb +++ b/spec/country_spec.rb @@ -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 diff --git a/spec/global_spec.rb b/spec/global_spec.rb deleted file mode 100644 index 3c448633c..000000000 --- a/spec/global_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -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' } - - 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 - end -end