carmen 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
 
3
3
  This library will work as a drop in replacement for the official Rails country_select and its various forks. The idea is to provide a single library for all geographic naming concerns, and unify them under a consistent API.
4
4
 
5
- States are supported for the following countries: Australia, Brazil, Canada, Cuba, Denmark, Germany, Italy, Mexico, Norway, Spain, Ukraine, and United States.
5
+ States are supported for the following countries: Australia, Brazil, Canada, Cuba, Denmark, Germany, Italy, Mexico, New Zealand, Norway, Spain, Ukraine, and United States.
6
6
 
7
7
  == Installation
8
8
 
@@ -44,6 +44,22 @@ Methods that take a country code argument will use the default country if none i
44
44
 
45
45
  Carmen.default_country = 'CA'
46
46
 
47
+ = Adding country and state exclusion to Carmen
48
+
49
+ This fork lets you exclude specific countries and/or states from Carmen. Simply set the appropriate class variables and the exclusion will take effect.
50
+
51
+ == Excluding Countries
52
+
53
+ Countries to exclude are specified as an array of country codes:
54
+
55
+ Carmen.excluded_countries = [ 'AF', 'ST', 'ZW', ... ]
56
+
57
+ == Excluding States
58
+
59
+ States to exclude are specified via a hash, with keys being the country's code, and values being the states to exclude from that country:
60
+
61
+ Carmen.excluded_states = { 'US' => [ 'AA', 'AP', 'PW', ... ], 'DE' => [ 'BW', 'TH' ], ... }
62
+
47
63
  == Localization
48
64
 
49
65
  You can switch between different localizations of the countries list, by setting the locale value (default is :en):
@@ -64,6 +80,7 @@ Currently included localizations are: English (:en), German (:de)
64
80
  * Split the Rails-specific view helpers out into a separate gem (carmen-rails)
65
81
 
66
82
  == Changelog
83
+ * 0.2.2 Added state and country exclusion (kalafut)
67
84
  * 0.2.1 Added regions for New Zealand (yehezkielbs)
68
85
  * 0.2.0 Merge in Maximilian Schulz's locale fork, refactor internals to better support locales, and update documentation.
69
86
  Remove Carmen::STATES and Carmen::COUNTRIES constants in favor of module instance variables and proper accessors.
@@ -94,6 +111,7 @@ Maximilian Schulz (namxam)
94
111
  Mani Tadayon (bowsersenior)
95
112
  Andriy Tyurnikov (andriytyurnikov)
96
113
  lonestarsoftware
114
+ kalafut
97
115
  yehezkielbs
98
116
 
99
117
  If I missed your name in this list, please let me know and I will add it. I tried to find everyone that has sent in patches or found bugs, but I may have missed a few folks.
data/Rakefile CHANGED
@@ -13,6 +13,7 @@ begin
13
13
  gem.authors = ["Jim Benton"]
14
14
  gem.add_development_dependency "mocha"
15
15
  gem.add_development_dependency "rails"
16
+ gem.add_development_dependency "hanna"
16
17
  end
17
18
  Jeweler::GemcutterTasks.new
18
19
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{carmen}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jim Benton"]
12
- s.date = %q{2010-11-19}
12
+ s.date = %q{2010-12-28}
13
13
  s.description = %q{A collection of geographis country and state names for Ruby. Also includes replacements for Rails' country_select and state_select plugins}
14
14
  s.email = %q{jim@autonomousmachine.com}
15
15
  s.extra_rdoc_files = [
@@ -63,13 +63,16 @@ Gem::Specification.new do |s|
63
63
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
64
64
  s.add_development_dependency(%q<mocha>, [">= 0"])
65
65
  s.add_development_dependency(%q<rails>, [">= 0"])
66
+ s.add_development_dependency(%q<hanna>, [">= 0"])
66
67
  else
67
68
  s.add_dependency(%q<mocha>, [">= 0"])
68
69
  s.add_dependency(%q<rails>, [">= 0"])
70
+ s.add_dependency(%q<hanna>, [">= 0"])
69
71
  end
70
72
  else
71
73
  s.add_dependency(%q<mocha>, [">= 0"])
72
74
  s.add_dependency(%q<rails>, [">= 0"])
75
+ s.add_dependency(%q<hanna>, [">= 0"])
73
76
  end
74
77
  end
75
78
 
@@ -8,11 +8,13 @@ end
8
8
  module Carmen
9
9
 
10
10
  class << self
11
- attr_accessor :default_country, :default_locale
11
+ attr_accessor :default_country, :default_locale, :excluded_countries, :excluded_states
12
12
  end
13
13
 
14
14
  self.default_country = 'US'
15
15
  self.default_locale = :en
16
+ self.excluded_countries = []
17
+ self.excluded_states = {}
16
18
 
17
19
  @data_path = File.join(File.dirname(__FILE__), '..', 'data')
18
20
 
@@ -47,9 +49,10 @@ module Carmen
47
49
  @countries[locale] = YAML.load_file(localized_data)
48
50
  end
49
51
 
50
- # Return data
51
- @countries[locale]
52
+ # Return data after filtering excluded countries
53
+ @countries[locale].reject { |c| excluded_countries.include?( c[1] ) }
52
54
  end
55
+
53
56
 
54
57
  # Returns the country name corresponding to the supplied country code, optionally using the specified locale.
55
58
  # Carmen::country_name('TR') => 'Turkey'
@@ -108,7 +111,14 @@ module Carmen
108
111
  def self.states(country_code = Carmen.default_country, options={})
109
112
  raise NonexistentCountry.new("Country not found for code #{country_code}") unless country_codes.include?(country_code)
110
113
  raise StatesNotSupported unless states?(country_code)
111
- search_collection(@states, country_code, 0, 1)
114
+
115
+ results = search_collection(@states, country_code, 0, 1)
116
+
117
+ if excluded_states[country_code]
118
+ results.reject { |s| excluded_states[country_code].include?(s[1]) }
119
+ else
120
+ results
121
+ end
112
122
  end
113
123
 
114
124
  # Returns whether states are supported for the given country code
@@ -134,4 +144,4 @@ module Carmen
134
144
  nil
135
145
  end
136
146
 
137
- end
147
+ end
@@ -96,6 +96,33 @@ class TestCarmen < Test::Unit::TestCase
96
96
  assert_equal Carmen::states?('ZZ'), false
97
97
  end
98
98
 
99
+ def test_excluded_countries
100
+ Carmen.excluded_countries = [ 'US', 'DE' ]
101
+ countries = Carmen.countries
102
+ assert !countries.include?( ["United States", "US"] )
103
+ assert !countries.include?( ["Germany", "DE"] )
104
+ assert countries.include?( ["Portugal", "PT"] )
105
+ Carmen.excluded_countries = [ ]
106
+ end
107
+
108
+ def test_excluded_states
109
+ Carmen.excluded_states = { 'US' => ['IL', 'OR'], 'DE' => ['BE', 'HH'] }
110
+ states = Carmen.states
111
+ assert !states.include?( ["Illinois", "IL"] )
112
+ assert !states.include?( ["Oregon", "OR"] )
113
+ assert states.include?( ["Kentucky", "KY"] )
114
+
115
+ states = Carmen.states("DE")
116
+ assert !states.include?( ["Berlin", "BE"] )
117
+ assert !states.include?( ["Hamburg", "HH"] )
118
+ assert states.include?( ["Rheinland-Pfalz", "RP"] )
119
+
120
+ states = Carmen.states("ES")
121
+ assert states.include?( ["Cantabria", "CAN"] )
122
+
123
+ Carmen.excluded_states = {}
124
+ end
125
+
99
126
  def test_invalid_country_exception
100
127
  assert_raises Carmen::NonexistentCountry do
101
128
  Carmen::state_codes('ZZ')
@@ -113,5 +140,7 @@ class TestCarmen < Test::Unit::TestCase
113
140
  Carmen.countries(:locale => :latin)
114
141
  end
115
142
  end
143
+
144
+
116
145
 
117
146
  end
@@ -1,2 +1,3 @@
1
1
  require 'test/unit'
2
+ require 'rubygems'
2
3
  require File.expand_path('../lib/carmen', File.dirname(__FILE__))
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carmen
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jim Benton
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-19 00:00:00 -06:00
18
+ date: 2010-12-28 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -46,6 +46,20 @@ dependencies:
46
46
  version: "0"
47
47
  type: :development
48
48
  version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: hanna
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
49
63
  description: A collection of geographis country and state names for Ruby. Also includes replacements for Rails' country_select and state_select plugins
50
64
  email: jim@autonomousmachine.com
51
65
  executables: []