Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

都道府県のマップ情報を再設定可能にする #8

Merged
merged 2 commits into from
Jul 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions data/prefecture.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# JIS X 0402 で定義されている都道府県コードと都道府県名をベースに
# コードがゼロから始まるもの(09 など)を 1 ケタにしたもの
---
1:
:name: 北海道
:name_e: hokkaido
2:
:name: 青森県
:name_e: aomori
3:
:name: 岩手県
:name_e: iwate
4:
:name: 宮城県
:name_e: miyagi
5:
:name: 秋田県
:name_e: akita
6:
:name: 山形県
:name_e: yamagata
7:
:name: 福島県
:name_e: fukushima
8:
:name: 茨城県
:name_e: ibaraki
9:
:name: 栃木県
:name_e: tochigi
10:
:name: 群馬県
:name_e: gunma
11:
:name: 埼玉県
:name_e: saitama
12:
:name: 千葉県
:name_e: chiba
13:
:name: 東京都
:name_e: tokyo
14:
:name: 神奈川県
:name_e: kanagawa
15:
:name: 新潟県
:name_e: niigata
16:
:name: 富山県
:name_e: toyama
17:
:name: 石川県
:name_e: ishikawa
18:
:name: 福井県
:name_e: fukui
19:
:name: 山梨県
:name_e: yamanashi
20:
:name: 長野県
:name_e: nagano
21:
:name: 岐阜県
:name_e: gifu
22:
:name: 静岡県
:name_e: shizuoka
23:
:name: 愛知県
:name_e: aichi
24:
:name: 三重県
:name_e: mie
25:
:name: 滋賀県
:name_e: shiga
26:
:name: 京都府
:name_e: kyoto
27:
:name: 大阪府
:name_e: osaka
28:
:name: 兵庫県
:name_e: hyogo
29:
:name: 奈良県
:name_e: nara
30:
:name: 和歌山県
:name_e: wakayama
31:
:name: 鳥取県
:name_e: tottori
32:
:name: 島根県
:name_e: shimane
33:
:name: 岡山県
:name_e: okayama
34:
:name: 広島県
:name_e: hiroshima
35:
:name: 山口県
:name_e: yamaguchi
36:
:name: 徳島県
:name_e: tokushima
37:
:name: 香川県
:name_e: kagawa
38:
:name: 愛媛県
:name_e: ehime
39:
:name: 高知県
:name_e: kochi
40:
:name: 福岡県
:name_e: fukuoka
41:
:name: 佐賀県
:name_e: saga
42:
:name: 長崎県
:name_e: nagasaki
43:
:name: 熊本県
:name_e: kumamoto
44:
:name: 大分県
:name_e: oita
45:
:name: 宮崎県
:name_e: miyazaki
46:
:name: 鹿児島県
:name_e: kagoshima
47:
:name: 沖縄県
:name_e: okinawa
11 changes: 11 additions & 0 deletions lib/jp_prefecture.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# coding: utf-8
require "jp_prefecture/base"
require "jp_prefecture/config"
require "jp_prefecture/prefecture"
require "jp_prefecture/version"

module JpPrefecture
@config = Config.new

def self.included(model_class)
model_class.extend Base
end

def self.setup
yield @config
end

def self.config
@config
end
end
8 changes: 8 additions & 0 deletions lib/jp_prefecture/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-

module JpPrefecture
class Config
attr_accessor :mapping_data
end
end

60 changes: 9 additions & 51 deletions lib/jp_prefecture/mapping.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,15 @@
# coding: utf-8
require 'yaml'

module JpPrefecture
# コードと都道府県のマッピング
module Mapping
# JIS X 0402 で定義されている都道府県コードと都道府県名をベースに
# コードがゼロから始まるもの(09 など)を 1 ケタにしたもの
PREFECTURE_CODE_NAME = {
1 => { name: '北海道', name_e: 'hokkaido' },
2 => { name: '青森県', name_e: 'aomori' },
3 => { name: '岩手県', name_e: 'iwate' },
4 => { name: '宮城県', name_e: 'miyagi' },
5 => { name: '秋田県', name_e: 'akita' },
6 => { name: '山形県', name_e: 'yamagata' },
7 => { name: '福島県', name_e: 'fukushima' },
8 => { name: '茨城県', name_e: 'ibaraki' },
9 => { name: '栃木県', name_e: 'tochigi' },
10 => { name: '群馬県', name_e: 'gunma' },
11 => { name: '埼玉県', name_e: 'saitama' },
12 => { name: '千葉県', name_e: 'chiba' },
13 => { name: '東京都', name_e: 'tokyo' },
14 => { name: '神奈川県', name_e: 'kanagawa' },
15 => { name: '新潟県', name_e: 'niigata' },
16 => { name: '富山県', name_e: 'toyama' },
17 => { name: '石川県', name_e: 'ishikawa' },
18 => { name: '福井県', name_e: 'fukui' },
19 => { name: '山梨県', name_e: 'yamanashi' },
20 => { name: '長野県', name_e: 'nagano' },
21 => { name: '岐阜県', name_e: 'gifu' },
22 => { name: '静岡県', name_e: 'shizuoka' },
23 => { name: '愛知県', name_e: 'aichi' },
24 => { name: '三重県', name_e: 'mie' },
25 => { name: '滋賀県', name_e: 'shiga' },
26 => { name: '京都府', name_e: 'kyoto' },
27 => { name: '大阪府', name_e: 'osaka' },
28 => { name: '兵庫県', name_e: 'hyogo' },
29 => { name: '奈良県', name_e: 'nara' },
30 => { name: '和歌山県', name_e: 'wakayama' },
31 => { name: '鳥取県', name_e: 'tottori' },
32 => { name: '島根県', name_e: 'shimane' },
33 => { name: '岡山県', name_e: 'okayama' },
34 => { name: '広島県', name_e: 'hiroshima' },
35 => { name: '山口県', name_e: 'yamaguchi' },
36 => { name: '徳島県', name_e: 'tokushima' },
37 => { name: '香川県', name_e: 'kagawa' },
38 => { name: '愛媛県', name_e: 'ehime' },
39 => { name: '高知県', name_e: 'kochi' },
40 => { name: '福岡県', name_e: 'fukuoka' },
41 => { name: '佐賀県', name_e: 'saga' },
42 => { name: '長崎県', name_e: 'nagasaki' },
43 => { name: '熊本県', name_e: 'kumamoto' },
44 => { name: '大分県', name_e: 'oita' },
45 => { name: '宮崎県', name_e: 'miyazaki' },
46 => { name: '鹿児島県', name_e: 'kagoshima' },
47 => { name: '沖縄県', name_e: 'okinawa' },
}

filepath = File.join(File.dirname(__FILE__), '../../data/prefecture.yml')
@data = YAML.load_file(filepath)

def self.data
JpPrefecture.config.mapping_data || @data
end
end
end
7 changes: 3 additions & 4 deletions lib/jp_prefecture/prefecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
module JpPrefecture
# 都道府県のコードと名前を扱うクラス
class Prefecture
include JpPrefecture::Mapping

attr_accessor :code, :name, :name_e

Expand Down Expand Up @@ -62,7 +61,7 @@ def self.find(args)
end
end

names = PREFECTURE_CODE_NAME[code]
names = Mapping.data[code]

return unless names

Expand All @@ -84,7 +83,7 @@ def self.find(args)
#
# @return [Array] 都道府県クラスの配列
def self.all
PREFECTURE_CODE_NAME.map do |pref|
Mapping.data.map do |pref|
names = pref[1]
self.build(pref[0], names[:name], names[:name_e])
end
Expand All @@ -94,7 +93,7 @@ def self.all

# 名前から都道府県コードを検索
def self.find_code_by_name(name)
result = PREFECTURE_CODE_NAME.select { |_, v|
result = Mapping.data.select { |_, v|
v.has_value?(name.downcase)
}.first

Expand Down
18 changes: 18 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding: utf-8
require 'spec_helper'

describe JpPrefecture::Config do
describe '#mapping_data' do
context 'カスタマイズされたprefecture.ymlを使うとき' do
before do
filepath = File.join(File.dirname(__FILE__), 'fixtures/prefecture.yml')
@config = described_class.new
@config.mapping_data = YAML.load_file filepath
end

it '都道府県+海外の数が48であること' do
@config.mapping_data.count.should eq 48
end
end
end
end
Loading