https://rubygems.org/gems/jp_prefecture
Convert prefecture code to prefecture name in Japan.
Based on JIS X 0402. Remove 0 when prefecture code start with 0.
Hokkaido: 01 -> 1
Tokyo: 13 -> 13
Reference(Japanese): Wikipedia: 全国地方公共団体コード
You can change prefecture code and prefecture name's mapping data. Please check this Customize mapping data
Also available as a Rails plugin
require 'jp_prefecture'
Provide prefecture code to search prefecture's data
pref = JpPrefecture::Prefecture.find 13
# => #<JpPrefecture::Prefecture:0x007fceb11927d8 @code=13, @name="東京都", @name_e="Tokyo", @name_h="とうきょうと", @name_k="トウキョウト", @zips=[1000000..2080035], @area="関東">
pref.code
# => 13
pref.name
# => "東京都"
pref.name_e
# => "Tokyo"
pref.name_h
# => "とうきょうと"
pref.name_k
# => "トウキョウト"
pref.area
# => "関東"
or
JpPrefecture::Prefecture.find code: 13
JpPrefecture::Prefecture.find name: "東京都"
JpPrefecture::Prefecture.find name: "Tokyo"
JpPrefecture::Prefecture.find name: "tokyo"
JpPrefecture::Prefecture.find name: "トウキョウト"
JpPrefecture::Prefecture.find name: "とうきょうと"
JpPrefecture::Prefecture.find name: "東京"
JpPrefecture::Prefecture.all
# => [#<JpPrefecture::Prefecture:0x007fceb119a2a8 @code=1, @name="北海道", @name_e="Hokkaido", @name_h="ほっかいどう", @name_k="ホッカイドウ", @zips=[10000..70895, 400000..996509], @area="北海道">, ...]
Include JpPrefecture to Model which ActiveRecord::Base
inherited.
app/models/place.rb:
class Place < ActiveRecord::Base
# prefecture_code:integer
include JpPrefecture
jp_prefecture :prefecture_code
end
By JpPrefecture included, prefecture
method will be generated:
place = Place.new
place.prefecture_code = 13
place.prefecture.name_e
# => "Tokyo"
Customize prefecture
method name with method_name
option:
# model
jp_prefecture :prefecture_code, method_name: :pref
place = Place.new
place.prefecture_code = 13
place.pref.name_e
# => "Tokyo"
Use collection_select
to generate selector in view:
# Selector prefecture name in English
f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name_e
# Selector prefecture name in Japanese
f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name
Set prefecture_code
column type to integer
or string
.
Example:
class AddPrefectureCodeToPlaces < ActiveRecord::Migration
def change
add_column :places, :prefecture_code, :integer
end
end
Customize mapping data with custom_mapping_path
.
custom_mapping_path = "..." # /path/to/mapping_data
JpPrefecture.setup do |config|
config.mapping_data = YAML.load_file custom_mapping_path
end
Check out prefecture.yml for data format.
Add this line in Gemfile.
gem 'jp_prefecture'
Run
$ bundle
Or install gem with gem install
$ gem install jp_prefecture
http://rdoc.info/github/chocoby/jp_prefecture/master/frames/index
- Ruby: 1.9.3 / 2.0.0 / 2.1 / 2.2 / 2.3
- Rails: 3.2 / 4.0 / 4.1 / 4.2 / 5.0
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
git clone https://github.com/chocoby/jp_prefecture.git
cd jp_prefecture
bundle install --path .bundle
bundle exec rspec
Run test in multiple ActiveRecord
versions
bundle exec appraisal install
bundle exec appraisal rake spec