Created
September 6, 2012 08:15
-
-
Save lulalala/3652906 to your computer and use it in GitHub Desktop.
台灣住址行政區域分類
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TaiwaneseAddress | |
| def initialize(address) | |
| @original_address = address.clone() | |
| @levels = prepare_level() | |
| @levels.each do |level| | |
| level.keys.each do |region| | |
| if match = address[/^(.{1,4}#{region})/,1] | |
| level[region] = match | |
| address.gsub!(match,'') | |
| address.strip! | |
| end | |
| end | |
| end | |
| @road_address = address | |
| end | |
| # 顯示地址,隱藏指定級數以下的行政區域 | |
| # 預設隱藏四級以下。 | |
| def hide_region_below_level(level = 4) | |
| new_address = "" | |
| @levels.first(level - 1).each do |level| | |
| level.values.each do |match| | |
| next if match.blank? | |
| new_address << match | |
| end | |
| end | |
| new_address << @road_address | |
| end | |
| private | |
| def prepare_level | |
| levels = [] | |
| levels[0] = %w{省 市} | |
| levels[1] = %w{縣 市} | |
| levels[2] = %w{鄉 鎮 市 區} | |
| levels[3] = %w{村 里} | |
| levels[4] = %w{鄰} | |
| levels.map! do |level| | |
| Hash[level.map {|x| [x, nil]}] | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment