model_un 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in model_un.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ model_un (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ rspec (2.4.0)
11
+ rspec-core (~> 2.4.0)
12
+ rspec-expectations (~> 2.4.0)
13
+ rspec-mocks (~> 2.4.0)
14
+ rspec-core (2.4.0)
15
+ rspec-expectations (2.4.0)
16
+ diff-lcs (~> 1.1.2)
17
+ rspec-mocks (2.4.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ model_un!
24
+ rspec
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ ModelUN - Quick and easy country and US State abbreviations
2
+ ===========================================================
3
+
4
+ ModelUN is a tiny little Ruby library for abbreviating and, er, de-abbreviating Countries and US State names.
5
+
6
+ Brought to you by [Uh Huh Yeah](http://uhhuhyeah.com/) and [Climber](http://www.climber.com/)
7
+
8
+ Installation
9
+ -------------
10
+
11
+ gem install model_un
12
+
13
+
14
+ ModelUN API
15
+ ===========
16
+
17
+ Convert a US State to it's abbreviated name
18
+ -------------------------------------------
19
+
20
+ my_state = 'California'
21
+ my_state_abbr = ModelUN.convert_state_name(my_state) # returns 'CA'
22
+
23
+ Convert a US State abbreviation to it's full name
24
+ -------------------------------------------------
25
+
26
+ my_state_abbr = 'CA'
27
+ my_state_name = ModelUN.convert_state_abbr(my_state_abbr) # returns 'California'
28
+
29
+ Or, a shortcut for either method
30
+ --------------------------------
31
+
32
+ my_state = 'California'
33
+ my_state_abbr = ModelUN.convert(my_state) # returns 'CA'
34
+
35
+ my_state_abbr = 'CA'
36
+ my_state_name = ModelUN.convert(my_state_abbr) # returns 'California'
37
+
38
+
39
+ Current version: v0.1 (February 2011)
40
+ =====================================
41
+
42
+ * Supports conversion of US State names (including Guam, Marshal and Virgin Islands)
43
+ * [Country conversions to be added in next release]
44
+
45
+ Known issues
46
+ ------------
47
+
48
+ * No known issues. Please report bugs to [david@uhhuhyeah.com](mailto:david@uhhuhyeah.com?subject=ModelUN), or fee free for send me a patch.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,189 @@
1
+ module ModelUN
2
+ US_STATE_ABBR = [
3
+ 'AL',
4
+ 'AK',
5
+ 'AS',
6
+ 'AZ',
7
+ 'AR',
8
+ 'CA',
9
+ 'CO',
10
+ 'CT',
11
+ 'DE',
12
+ 'DC',
13
+ 'FM',
14
+ 'FL',
15
+ 'GA',
16
+ 'GU',
17
+ 'HI',
18
+ 'ID',
19
+ 'IL',
20
+ 'IN',
21
+ 'IA',
22
+ 'KS',
23
+ 'KY',
24
+ 'LA',
25
+ 'ME',
26
+ 'MH',
27
+ 'MD',
28
+ 'MA',
29
+ 'MI',
30
+ 'MN',
31
+ 'MS',
32
+ 'MO',
33
+ 'MT',
34
+ 'NE',
35
+ 'NV',
36
+ 'NH',
37
+ 'NJ',
38
+ 'NM',
39
+ 'NY',
40
+ 'NC',
41
+ 'ND',
42
+ 'MP',
43
+ 'OH',
44
+ 'OK',
45
+ 'OR',
46
+ 'PW',
47
+ 'PA',
48
+ 'PR',
49
+ 'RI',
50
+ 'SC',
51
+ 'SD',
52
+ 'TN',
53
+ 'TX',
54
+ 'UT',
55
+ 'VT',
56
+ 'VI',
57
+ 'VA',
58
+ 'WA',
59
+ 'WV',
60
+ 'WI',
61
+ 'WY'
62
+ ]
63
+
64
+
65
+ US_STATE_NAMES = [
66
+ 'ALABAMA',
67
+ 'ALASKA',
68
+ 'AMERICAN SAMOA',
69
+ 'ARIZONA',
70
+ 'ARKANSAS',
71
+ 'CALIFORNIA',
72
+ 'COLORADO',
73
+ 'CONNECTICUT',
74
+ 'DELAWARE',
75
+ 'DISTRICT OF COLUMBIA',
76
+ 'FEDERATED STATES OF MICRONESIA',
77
+ 'FLORIDA',
78
+ 'GEORGIA',
79
+ 'GUAM',
80
+ 'HAWAII',
81
+ 'IDAHO',
82
+ 'ILLINOIS',
83
+ 'INDIANA',
84
+ 'IOWA',
85
+ 'KANSAS',
86
+ 'KENTUCKY',
87
+ 'LOUISIANA',
88
+ 'MAINE',
89
+ 'MARSHALL ISLANDS',
90
+ 'MARYLAND',
91
+ 'MASSACHUSETTS',
92
+ 'MICHIGAN',
93
+ 'MINNESOTA',
94
+ 'MISSISSIPPI',
95
+ 'MISSOURI',
96
+ 'MONTANA',
97
+ 'NEBRASKA',
98
+ 'NEVADA',
99
+ 'NEW HAMPSHIRE',
100
+ 'NEW JERSEY',
101
+ 'NEW MEXICO',
102
+ 'NEW YORK',
103
+ 'NORTH CAROLINA',
104
+ 'NORTH DAKOTA',
105
+ 'NORTHERN MARIANA ISLANDS',
106
+ 'OHIO',
107
+ 'OKLAHOMA',
108
+ 'OREGON',
109
+ 'PALAU',
110
+ 'PENNSYLVANIA',
111
+ 'PUERTO RICO',
112
+ 'RHODE ISLAND',
113
+ 'SOUTH CAROLINA',
114
+ 'SOUTH DAKOTA',
115
+ 'TENNESSEE',
116
+ 'TEXAS',
117
+ 'UTAH',
118
+ 'VERMONT',
119
+ 'VIRGIN ISLANDS',
120
+ 'VIRGINIA',
121
+ 'WASHINGTON',
122
+ 'WEST VIRGINIA',
123
+ 'WISCONSIN',
124
+ 'WYOMING',
125
+ ]
126
+
127
+ US_STATES = [
128
+ {'full' => 'ALABAMA', 'abbr' => 'AL'},
129
+ {'full' => 'ALASKA', 'abbr' => 'AK'},
130
+ {'full' => 'AMERICAN SAMOA', 'abbr' => 'AS'},
131
+ {'full' => 'ARIZONA', 'abbr' => 'AZ'},
132
+ {'full' => 'ARKANSAS', 'abbr' => 'AR'},
133
+ {'full' => 'CALIFORNIA', 'abbr' => 'CA'},
134
+ {'full' => 'COLORADO', 'abbr' => 'CO'},
135
+ {'full' => 'CONNECTICUT', 'abbr' => 'CT'},
136
+ {'full' => 'DELAWARE', 'abbr' => 'DE'},
137
+ {'full' => 'DISTRICT OF COLUMBIA', 'abbr' => 'DC'},
138
+ {'full' => 'FEDERATED STATES OF MICRONESIA', 'abbr' => 'FM'},
139
+ {'full' => 'FLORIDA', 'abbr' => 'FL'},
140
+ {'full' => 'GEORGIA', 'abbr' => 'GA'},
141
+ {'full' => 'GUAM', 'abbr' => 'GU'},
142
+ {'full' => 'HAWAII', 'abbr' => 'HI'},
143
+ {'full' => 'IDAHO', 'abbr' => 'ID'},
144
+ {'full' => 'ILLINOIS', 'abbr' => 'IL'},
145
+ {'full' => 'INDIANA', 'abbr' => 'IN'},
146
+ {'full' => 'IOWA', 'abbr' => 'IA'},
147
+ {'full' => 'KANSAS', 'abbr' => 'KS'},
148
+ {'full' => 'KENTUCKY', 'abbr' => 'KY'},
149
+ {'full' => 'LOUISIANA', 'abbr' => 'LA'},
150
+ {'full' => 'MAINE', 'abbr' => 'ME'},
151
+ {'full' => 'MARSHALL ISLANDS', 'abbr' => 'MH'},
152
+ {'full' => 'MARYLAND', 'abbr' => 'MD'},
153
+ {'full' => 'MASSACHUSETTS', 'abbr' => 'MA'},
154
+ {'full' => 'MICHIGAN', 'abbr' => 'MI'},
155
+ {'full' => 'MINNESOTA', 'abbr' => 'MN'},
156
+ {'full' => 'MISSISSIPPI', 'abbr' => 'MS'},
157
+ {'full' => 'MISSOURI', 'abbr' => 'MO'},
158
+ {'full' => 'MONTANA', 'abbr' => 'MT'},
159
+ {'full' => 'NEBRASKA', 'abbr' => 'NE'},
160
+ {'full' => 'NEVADA', 'abbr' => 'NV'},
161
+ {'full' => 'NEW HAMPSHIRE', 'abbr' => 'NH'},
162
+ {'full' => 'NEW JERSEY', 'abbr' => 'NJ'},
163
+ {'full' => 'NEW MEXICO', 'abbr' => 'NM'},
164
+ {'full' => 'NEW YORK', 'abbr' => 'NY'},
165
+ {'full' => 'NORTH CAROLINA', 'abbr' => 'NC'},
166
+ {'full' => 'NORTH DAKOTA', 'abbr' => 'ND'},
167
+ {'full' => 'NORTHERN MARIANA ISLANDS', 'abbr' => 'MP'},
168
+ {'full' => 'OHIO', 'abbr' => 'OH'},
169
+ {'full' => 'OKLAHOMA', 'abbr' => 'OK'},
170
+ {'full' => 'OREGON', 'abbr' => 'OR'},
171
+ {'full' => 'PALAU', 'abbr' => 'PW'},
172
+ {'full' => 'PENNSYLVANIA', 'abbr' => 'PA'},
173
+ {'full' => 'PUERTO RICO', 'abbr' => 'PR'},
174
+ {'full' => 'RHODE ISLAND', 'abbr' => 'RI'},
175
+ {'full' => 'SOUTH CAROLINA', 'abbr' => 'SC'},
176
+ {'full' => 'SOUTH DAKOTA', 'abbr' => 'SD'},
177
+ {'full' => 'TENNESSEE', 'abbr' => 'TN'},
178
+ {'full' => 'TEXAS', 'abbr' => 'TX'},
179
+ {'full' => 'UTAH', 'abbr' => 'UT'},
180
+ {'full' => 'VERMONT', 'abbr' => 'VT'},
181
+ {'full' => 'VIRGIN ISLANDS', 'abbr' => 'VI'},
182
+ {'full' => 'VIRGINIA', 'abbr' => 'VA'},
183
+ {'full' => 'WASHINGTON', 'abbr' => 'WA'},
184
+ {'full' => 'WEST VIRGINIA', 'abbr' => 'WV'},
185
+ {'full' => 'WISCONSIN', 'abbr' => 'WI'},
186
+ {'full' => 'WYOMING', 'abbr' => 'WY'}
187
+ ]
188
+
189
+ end
@@ -0,0 +1,3 @@
1
+ module ModelUN
2
+ VERSION = "0.1"
3
+ end
data/lib/model_un.rb ADDED
@@ -0,0 +1,40 @@
1
+ module ModelUN
2
+ require "model_un/data"
3
+ require 'yaml'
4
+
5
+ class << self
6
+
7
+ def convert(string)
8
+ if string.length == 2
9
+ self.convert_state_abbr(string.upcase)
10
+ else
11
+ self.convert_state_name(string.upcase)
12
+ end
13
+ end
14
+
15
+ def convert_state_abbr(state_abbr)
16
+ self.convert_state_abbreviation(state_abbr)
17
+ end
18
+
19
+ def convert_state_abbreviation(state_abbr)
20
+ begin
21
+ return US_STATE_NAMES[US_STATE_ABBR.index(state_abbr)].downcase.gsub(/\b\w/){$&.upcase}
22
+ rescue Exception
23
+ return state_abbr
24
+ end
25
+ end
26
+
27
+ def convert_state_name(state_name)
28
+ begin
29
+ return US_STATE_ABBR[US_STATE_NAMES.index(state_name)]
30
+ rescue Exception
31
+ return state_name.downcase.gsub(/\b\w/){$&.upcase}
32
+ end
33
+ end
34
+
35
+ def export_states_list
36
+ {'US States' => US_STATES}.to_yaml.gsub(/\n/,'').gsub(/-/, '').strip
37
+ end
38
+
39
+ end
40
+ end
data/model_un.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "model_un/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "model_un"
7
+ s.version = ModelUN::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Uh Huh Yeah"]
10
+ s.email = ["david@uhhuhyeah.com"]
11
+ s.homepage = "https://github.com/uhhuhyeah/model_un"
12
+ s.summary = %q{Quick and easy country and US State abbreviations}
13
+ s.description = %q{Converts US State names and (most) country names to their normally accepted abbreviations and back.}
14
+
15
+ s.add_development_dependency "rspec"
16
+ s.rubyforge_project = "model_un"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,30 @@
1
+ require 'model_un'
2
+
3
+ describe ModelUN do
4
+
5
+ it "should be able to read the data hash" do
6
+ ModelUN::US_STATE_NAMES.should_not == nil
7
+ ModelUN::US_STATE_ABBR.should_not == nil
8
+ end
9
+
10
+ it "should return an abbreviation for a full state" do
11
+ state = 'California'
12
+ ModelUN.convert(state).should == 'CA'
13
+ end
14
+
15
+ it "should return a full state for an abbreviation" do
16
+ abbr = 'CA'
17
+ ModelUN.convert(abbr).should == 'California'
18
+ end
19
+
20
+ it "should return the input if no match on abbreviation" do
21
+ abbr = 'XX'
22
+ ModelUN.convert(abbr).should == 'XX'
23
+ end
24
+
25
+ it "should return the input if no match on full name" do
26
+ state = 'Foobar'
27
+ ModelUN.convert(state).should == state
28
+ end
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: model_un
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Uh Huh Yeah
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-05 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Converts US State names and (most) country names to their normally accepted abbreviations and back.
35
+ email:
36
+ - david@uhhuhyeah.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - Gemfile.lock
47
+ - README.md
48
+ - Rakefile
49
+ - lib/model_un.rb
50
+ - lib/model_un/data.rb
51
+ - lib/model_un/version.rb
52
+ - model_un.gemspec
53
+ - spec/model_un_spec.rb
54
+ has_rdoc: true
55
+ homepage: https://github.com/uhhuhyeah/model_un
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: model_un
84
+ rubygems_version: 1.3.7
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Quick and easy country and US State abbreviations
88
+ test_files:
89
+ - spec/model_un_spec.rb