namxam-carmen 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ ---
2
+ - - Autonomous Republic of Crimea
3
+ - CR
4
+ - - Cherkashchyna
5
+ - CK
6
+ - - Chernihivshchyna
7
+ - CN
8
+ - - Chernivechchyna
9
+ - CV
10
+ - - Dnipropetrovshchyna
11
+ - DP
12
+ - - Donechchyna
13
+ - DN
14
+ - - Ivano-Frankivshchyna
15
+ - IF
16
+ - - Kharkivshchyna
17
+ - KH
18
+ - - Khersonshchyna
19
+ - KS
20
+ - - Khmel'nychchyna
21
+ - KM
22
+ - - Kyivshchyna
23
+ - KV
24
+ - - Kirovohradshchyna
25
+ - KR
26
+ - - Luhanshchyna
27
+ - LG
28
+ - - L'vivshchyna
29
+ - LV
30
+ - - Mykolaivshchyna
31
+ - MK
32
+ - - Odeshchyna
33
+ - OD
34
+ - - Poltavshchyna
35
+ - PL
36
+ - - Rivnenshchyna
37
+ - RV
38
+ - - Sumshchyna
39
+ - SM
40
+ - - Ternopil'shchyna
41
+ - TAS
42
+ - - Vinnychchyna
43
+ - VN
44
+ - - Volyn'
45
+ - VOLYN
46
+ - - Zakarpattia
47
+ - ZK
48
+ - - Zaporizhzhia
49
+ - ZP
50
+ - - Zhytomyrshchyna
51
+ - ZT
@@ -0,0 +1,124 @@
1
+ # from http://www.usps.com/ncsc/lookups/usps_abbreviations.html
2
+ ---
3
+ - - Alabama
4
+ - AL
5
+ - - Alaska
6
+ - AK
7
+ - - Arizona
8
+ - AZ
9
+ - - Arkansas
10
+ - AR
11
+ - - California
12
+ - CA
13
+ - - Colorado
14
+ - CO
15
+ - - Connecticut
16
+ - CT
17
+ - - Delaware
18
+ - DE
19
+ - - District Of Columbia
20
+ - DC
21
+ - - Florida
22
+ - FL
23
+ - - Georgia
24
+ - GA
25
+ - - Hawaii
26
+ - HI
27
+ - - Idaho
28
+ - ID
29
+ - - Illinois
30
+ - IL
31
+ - - Indiana
32
+ - IN
33
+ - - Iowa
34
+ - IA
35
+ - - Kansas
36
+ - KS
37
+ - - Kentucky
38
+ - KY
39
+ - - Louisiana
40
+ - LA
41
+ - - Maine
42
+ - ME
43
+ - - Maryland
44
+ - MD
45
+ - - Massachusetts
46
+ - MA
47
+ - - Michigan
48
+ - MI
49
+ - - Minnesota
50
+ - MN
51
+ - - Mississippi
52
+ - MS
53
+ - - Missouri
54
+ - MO
55
+ - - Montana
56
+ - MT
57
+ - - Nebraska
58
+ - NE
59
+ - - Nevada
60
+ - NV
61
+ - - New Hampshire
62
+ - NH
63
+ - - New Jersey
64
+ - NJ
65
+ - - New Mexico
66
+ - NM
67
+ - - New York
68
+ - NY
69
+ - - North Carolina
70
+ - NC
71
+ - - North Dakota
72
+ - ND
73
+ - - Ohio
74
+ - OH
75
+ - - Oklahoma
76
+ - OK
77
+ - - Oregon
78
+ - OR
79
+ - - Pennsylvania
80
+ - PA
81
+ - - Rhode Island
82
+ - RI
83
+ - - South Carolina
84
+ - SC
85
+ - - South Dakota
86
+ - SD
87
+ - - Tennessee
88
+ - TN
89
+ - - Texas
90
+ - TX
91
+ - - Utah
92
+ - UT
93
+ - - Vermont
94
+ - VT
95
+ - - Virginia
96
+ - VA
97
+ - - Washington
98
+ - WA
99
+ - - West Virginia
100
+ - WV
101
+ - - Wisconsin
102
+ - WI
103
+ - - Wyoming
104
+ - WY
105
+ - - Federated States of Micronesia
106
+ - FM
107
+ - - Guam
108
+ - GU
109
+ - - Marshall Islands
110
+ - MH
111
+ - - Northern Mariana Islands
112
+ - MP
113
+ - - Palau
114
+ - PW
115
+ - - Puerto Rico
116
+ - PR
117
+ - - Virgin Islands
118
+ - VI
119
+ - - Armed Forces Americas (except Canada)
120
+ - AA
121
+ - - Armed Forces Europe, Armed Forces Canada, Armed Forces Africa, or Armed Forces Middle East
122
+ - AE
123
+ - - Armed Forces Pacific
124
+ - AP
data/lib/carmen.rb ADDED
@@ -0,0 +1,132 @@
1
+ require 'yaml'
2
+ begin
3
+ require 'ftools'
4
+ rescue LoadError
5
+ require 'fileutils' # ftools is now fileutils in Ruby 1.9
6
+ end
7
+
8
+ module Carmen
9
+
10
+ class << self
11
+ attr_accessor :default_country, :locale
12
+ end
13
+
14
+ self.default_country = 'US'
15
+ self.locale = :en
16
+
17
+ @data_path = File.join(File.dirname(__FILE__), '../data')
18
+
19
+ DEFAULT_COUNTRIES = @countries = YAML.load_file(File.join(@data_path, 'countries.yml'))
20
+
21
+ STATES = Dir[@data_path + '/states/*.yml'].map do |file_name|
22
+ [File::basename(file_name, '.yml').upcase, YAML.load_file(file_name)]
23
+ end
24
+
25
+ # Raised when attempting to retrieve states for an unsupported country
26
+ class StatesNotSupported < RuntimeError; end
27
+
28
+ # Raised when attempting to work with a country not in the data set
29
+ class NonexistentCountry < RuntimeError; end
30
+
31
+ # Raised when attemting to switch to a locale which does not exist
32
+ class UnavailableLocale < RuntimeError; end
33
+
34
+ # Loads a localized version of the country list
35
+ def self.locale=(input)
36
+ localization = File.join(@data_path, "countries.#{input}.yml")
37
+
38
+ raise UnavailableLocale unless File.exists?(localization)
39
+
40
+ # Load localization
41
+ localized_countries = YAML.load_file(localization)
42
+
43
+ # Merge the defaults with the localized versions
44
+ @countries = DEFAULT_COUNTRIES # Reset countries to default list
45
+ positions = country_codes # for speed optimization
46
+ localized_countries.each do |c|
47
+ if index = country_codes.index(c[1])
48
+ @countries[country_codes.index(c[1])] = c
49
+ else
50
+ @countries.push(c)
51
+ end
52
+ end
53
+
54
+ @locale = input.to_s
55
+ end
56
+
57
+ # Returns the country name corresponding to the supplied country code
58
+ # Carmen::country_name('TV') => 'Tuvalu'
59
+ def self.country_name(country_code)
60
+ search_collection(@countries, country_code, 1, 0)
61
+ end
62
+
63
+ # Returns the country code corresponding to the supplied country name
64
+ # Carmen::country_code('Canada') => 'CA'
65
+ def self.country_code(country_name)
66
+ search_collection(@countries, country_name, 0, 1)
67
+ end
68
+
69
+ # Returns an array of all country codes
70
+ # Carmen::country_codes => ['AF', 'AX', 'AL', ... ]
71
+ def self.country_codes
72
+ @countries.map {|c| c[1] }
73
+ end
74
+
75
+ # Returns an array of all country codes
76
+ # Carmen::country_name => ['Afghanistan', 'Aland Islands', 'Albania', ... ]
77
+ def self.country_names
78
+ @countries.map {|c| c[0] }
79
+ end
80
+
81
+ # Returns the state name corresponding to the supplied state code within the specified country
82
+ # Carmen::state_code('New Hampshire') => 'NH'
83
+ def self.state_name(state_code, country_code = Carmen.default_country)
84
+ search_collection(self.states(country_code), state_code, 1, 0)
85
+ end
86
+
87
+ # Returns the state code corresponding to the supplied state name within the specified country
88
+ # Carmen::state_code('IL', 'US') => Illinois
89
+ def self.state_code(state_name, country_code = Carmen.default_country)
90
+ search_collection(self.states(country_code), state_name, 0, 1)
91
+ end
92
+
93
+ # Returns an array of state names within the specified country code
94
+ # Carmen::state_names('US') => ['Alabama', 'Arkansas', ... ]
95
+ def self.state_names(country_code = Carmen.default_country)
96
+ self.states(country_code).map{|name, code| name}
97
+ end
98
+
99
+ # Returns an array of state codes within the specified country code
100
+ # Carmen::state_codes('US') => ['AL', 'AR', ... ]
101
+ def self.state_codes(country_code = Carmen.default_country)
102
+ self.states(country_code).map{|name, code| code}
103
+ end
104
+
105
+ # Returns an array structure of state names and codes within the specified country code
106
+ # Carmen::states('US') => [['Alabama', 'AL'], ['Arkansas', 'AR'], ... ]
107
+ def self.states(country_code = Carmen.default_country)
108
+ raise NonexistentCountry unless country_codes.include?(country_code)
109
+ raise StatesNotSupported unless states?(country_code)
110
+ search_collection(STATES, country_code, 0, 1)
111
+ end
112
+
113
+ # Returns whether states are supported for the given country code
114
+ # Carmen::states?('US') => true
115
+ # Carmen::states?('ZZ') => false
116
+ def self.states?(country_code)
117
+ STATES.any? do |array| k,v = array
118
+ k == country_code
119
+ end
120
+ end
121
+
122
+ protected
123
+
124
+ def self.search_collection(collection, value, index_to_match, index_to_retrieve)
125
+ return nil if collection.nil?
126
+ collection.each do |m|
127
+ return m[index_to_retrieve] if m[index_to_match] == value
128
+ end
129
+ nil
130
+ end
131
+
132
+ end
@@ -0,0 +1,68 @@
1
+ module ActionView
2
+ module Helpers
3
+ module FormOptionsHelper
4
+
5
+ # Return select and option tags for the given object and method, using state_options_for_select to generate the list of option tags.
6
+ def state_select(object, method, country = Carmen.default_country, options={}, html_options={})
7
+ InstanceTag.new(object, method, self, options.delete(:object)).to_state_select_tag(country, options, html_options)
8
+ end
9
+
10
+ # Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
11
+ def country_select(object, method, priority_countries = nil, options = {}, html_options = {})
12
+ InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options)
13
+ end
14
+
15
+ # Returns a string of option tags containing the state names and codes for the specified country code, or nil
16
+ # if the states are not know for that country. Supply a state code as +selected+ to have it marked as the selected option tag.
17
+ def state_options_for_select(selected = nil, country = Carmen.default_country)
18
+ options_for_select(Carmen.states(country), selected)
19
+ end
20
+
21
+ # Returns a string of option tags for pretty much any country in the world. Supply a country name as +selected+ to
22
+ # have it marked as the selected option tag. You can also supply a list of country codes as additional parameters, so
23
+ # that they will be listed above the rest of the (long) list.
24
+ def country_options_for_select(selected = nil, *priority_country_codes)
25
+ country_options = ""
26
+
27
+ unless priority_country_codes.empty?
28
+ priority_countries = Carmen::COUNTRIES.select do |pair| name, code = pair
29
+ priority_country_codes.include?(code)
30
+ end
31
+ unless priority_countries.empty?
32
+ country_options += options_for_select(priority_countries, selected)
33
+ country_options += "\n<option value=\"\" disabled=\"disabled\">-------------</option>\n"
34
+ end
35
+ end
36
+
37
+ return country_options + options_for_select(Carmen::COUNTRIES, priority_country_codes.include?(selected) ? nil : selected)
38
+ end
39
+ end
40
+
41
+ class InstanceTag
42
+ def to_country_select_tag(priority_countries, options, html_options)
43
+ html_options = html_options.stringify_keys
44
+ add_default_name_and_id(html_options)
45
+ value = value(object)
46
+ opts = add_options(country_options_for_select(value, *priority_countries), options, value)
47
+ content_tag("select", opts, html_options)
48
+ end
49
+
50
+ def to_state_select_tag(country, options, html_options)
51
+ html_options = html_options.stringify_keys
52
+ add_default_name_and_id(html_options)
53
+ value = value(object)
54
+ opts = add_options(state_options_for_select(value, country), options, value)
55
+ content_tag("select", opts, html_options)
56
+ end
57
+ end
58
+
59
+ class FormBuilder
60
+ def country_select(method, priority_countries = nil, options = {}, html_options = {})
61
+ @template.country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
62
+ end
63
+ def state_select(method, country_code = Carmen.default_country, options = {}, html_options = {})
64
+ @template.state_select(@object_name, method, country_code, options.merge(:object => @object), html_options)
65
+ end
66
+ end
67
+ end
68
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'carmen'
2
+ require 'carmen/action_view_helpers'
@@ -0,0 +1,89 @@
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__), '../lib/carmen')
3
+
4
+ class TestCarmen < Test::Unit::TestCase
5
+
6
+
7
+ def test_country_name
8
+ assert_equal 'United States', Carmen.country_name('US')
9
+ end
10
+
11
+ def test_localized_country_name
12
+ Carmen.locale = :de
13
+ assert_equal 'Deutschland', Carmen.country_name('DE')
14
+ end
15
+
16
+ def test_country_code
17
+ assert_equal 'CA', Carmen.country_code('Canada')
18
+ end
19
+
20
+ def test_localized_country_code
21
+ Carmen.locale = :de
22
+ assert_equal 'DE', Carmen.country_code('Deutschland')
23
+ end
24
+
25
+ def test_country_codes
26
+ assert_equal 'AF', Carmen.country_codes.first
27
+ assert_equal 243, Carmen.country_codes.length
28
+ end
29
+
30
+ def test_country_names
31
+ assert_equal 'Afghanistan', Carmen.country_names.first
32
+ assert_equal 243, Carmen.country_names.length
33
+ end
34
+
35
+ def test_state_name
36
+ assert_equal 'IL', Carmen.state_code('Illinois')
37
+ assert_equal 'MB', Carmen.state_code('Manitoba', 'CA')
38
+ end
39
+
40
+ def test_state_code
41
+ assert_equal 'Arizona', Carmen.state_name('AZ')
42
+ assert_equal 'Prince Edward Island', Carmen.state_name('PE', 'CA')
43
+ end
44
+
45
+ def test_states
46
+ assert_equal 61, Carmen.states.length
47
+ assert_equal ['Alabama', 'AL'], Carmen.states.first
48
+ assert_equal 13, Carmen.states('CA').length
49
+ assert_equal ['Alberta', 'AB'], Carmen.states('CA').first
50
+ end
51
+
52
+ def test_state_names
53
+ assert_equal 61, Carmen::state_names.length
54
+ assert_equal 'Alabama', Carmen::state_names.first
55
+ assert_equal 13, Carmen.state_names('CA').length
56
+ assert_equal 'Alberta', Carmen.state_names('CA').first
57
+ end
58
+
59
+ def test_state_codes
60
+ assert_equal 61, Carmen::state_codes.length
61
+ assert_equal 'AL', Carmen::state_codes.first
62
+ assert_equal 13, Carmen.state_codes('CA').length
63
+ assert_equal 'AB', Carmen.state_codes('CA').first
64
+ end
65
+
66
+ def test_supported_states
67
+ assert Carmen::states?('US')
68
+ assert_equal Carmen::states?('ZZ'), false
69
+ end
70
+
71
+ def test_invalid_country_exception
72
+ assert_raises Carmen::NonexistentCountry do
73
+ Carmen::state_codes('ZZ')
74
+ end
75
+ end
76
+
77
+ def test_unsupported_states_exception
78
+ assert_raises Carmen::StatesNotSupported do
79
+ Carmen::state_codes('ID')
80
+ end
81
+ end
82
+
83
+ def test_unsupported_locale
84
+ assert_raises Carmen::UnavailableLocale do
85
+ Carmen.locale = :latin
86
+ end
87
+ end
88
+
89
+ end