All Projects → chocoby → Jp_prefecture

chocoby / Jp_prefecture

Licence: mit
Convert japan prefecture code (JIS X 0402 based) into prefecture name.

Programming Languages

ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Jp prefecture

Foreman
an application that automates the lifecycle of servers
Stars: ✭ 2,102 (+1030.11%)
Mutual labels:  rails
Awesome Blogs
한국에 있는 좋은 개발자들의 블로그들을 편리하게 구독할 수 있도록 하나의 주소로 묶어서 RSS 피드로 제공해줍니다.
Stars: ✭ 178 (-4.3%)
Mutual labels:  rails
Calreact
React and Rails 5 calendar appointment app
Stars: ✭ 183 (-1.61%)
Mutual labels:  rails
Cve 2019 5418
CVE-2019-5418 - File Content Disclosure on Ruby on Rails
Stars: ✭ 169 (-9.14%)
Mutual labels:  rails
Wei
微信服务号裂变引擎,提供一套简单明了的DSL,快速配置和接入服务号裂变。 wechat fission platform, for technological dimensionality reduction.
Stars: ✭ 177 (-4.84%)
Mutual labels:  rails
Ruby2 Rails4 Bootstrap Heroku
A starter application based on Ruby 2.4, Rails 4.2 and Bootstrap for Sass 3, deployable on Heroku
Stars: ✭ 181 (-2.69%)
Mutual labels:  rails
Awesome Opensource Apps
🏠ℹ️ Curated list of awesome open source crafted web & mobile applications - Learn, Fork, Contribute & Most Importantly Enjoy!
Stars: ✭ 2,199 (+1082.26%)
Mutual labels:  rails
Thin
A very fast & simple Ruby web server
Stars: ✭ 2,170 (+1066.67%)
Mutual labels:  rails
React Native Turbolinks
React Native adapter for building hybrid apps with Turbolinks 5
Stars: ✭ 177 (-4.84%)
Mutual labels:  rails
The construct
A Modern Rails Template
Stars: ✭ 183 (-1.61%)
Mutual labels:  rails
Workflow core
[Deprecated, use flor_core instead] A Rails engine which providing essential infrastructure of workflow. It's based on Workflow Nets.
Stars: ✭ 171 (-8.06%)
Mutual labels:  rails
Rspec Rails Examples
RSpec cheatsheet & Rails app: Learn how to expertly test Rails apps from a model codebase
Stars: ✭ 2,089 (+1023.12%)
Mutual labels:  rails
Motorhead
A Rails Engine framework that helps safe and rapid feature prototyping
Stars: ✭ 182 (-2.15%)
Mutual labels:  rails
Acli
Action Cable command-line client
Stars: ✭ 169 (-9.14%)
Mutual labels:  rails
Ooor
Odoo Ruby JSON client. Emulates ActiveRecord enough (as much as Mongoid; Implements ActiveModel) to make Rails development with an Odoo datastore straightforward
Stars: ✭ 184 (-1.08%)
Mutual labels:  rails
Turbolinks render
Support for `render` with Turbolinks in Rails controllers
Stars: ✭ 168 (-9.68%)
Mutual labels:  rails
Eshop Prices
Nintendo Switch - Full game list with prices
Stars: ✭ 179 (-3.76%)
Mutual labels:  rails
Diaspora
A privacy-aware, distributed, open source social network.
Stars: ✭ 12,937 (+6855.38%)
Mutual labels:  rails
Saml idp
Ruby SAML Identity Provider, best used with Rails (though not required)
Stars: ✭ 184 (-1.08%)
Mutual labels:  rails
Honeybadger Ruby
Ruby gem for reporting errors to honeybadger.io
Stars: ✭ 182 (-2.15%)
Mutual labels:  rails

JpPrefecture

English

Gem Version GitHub Actions Coveralls

https://rubygems.org/gems/jp_prefecture

jp_prefecture とは

都道府県コードと都道府県名を変換するライブラリです。

JIS X 0402 で定義されている都道府県コードをベースに、 ゼロから始まるものはゼロを削除して使用しています。

北海道: 01 -> 1
東京都: 13 -> 13

参考: Wikipedia: 全国地方公共団体コード

都道府県コードと都道府県名のマッピングは変更することもできます。 詳しくは「都道府県のマッピング情報を変更する」の項目を参照してください。

また、Rails のプラグインとして使用することもできます。

使い方

ライブラリの読み込み

require 'jp_prefecture'

都道府県コードから都道府県を検索

都道府県コードを渡すと、都道府県コードから都道府県を検索します:

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
# => "関東"
pref.type
# => "都"

以下のように書くことも可能です:

JpPrefecture::Prefecture.find(code: 13)

都道府県を検索

前方一致で都道府県を検索します:

# 漢字表記
JpPrefecture::Prefecture.find(name: "東京都")
JpPrefecture::Prefecture.find(name: "東京")

# 英語表記
JpPrefecture::Prefecture.find(name_e: "Tokyo")
JpPrefecture::Prefecture.find(name_e: "tokyo")

# ひらがな表記
JpPrefecture::Prefecture.find(name_h: "とうきょうと")

# カタカナ表記
JpPrefecture::Prefecture.find(name_k: "トウキョウト")

マッピングのすべての項目を検索します (推奨しません):

JpPrefecture::Prefecture.find(all_fields: "東京")

都道府県の一覧を取得

JpPrefecture::Prefecture.all
# => [#<JpPrefecture::Prefecture:0x007fceb119a2a8 @code=1, @name="北海道", @name_e="Hokkaido", @name_h="ほっかいどう", @name_k="ホッカイドウ", @zips=[10000..70895, 400000..996509], @area="北海道">, ...]

Rails (ActiveRecord) で使用する

ActiveRecord::Base を継承した Model で、都道府県コードを扱うことができます。

app/models/place.rb:

class Place < ActiveRecord::Base
  # prefecture_code:integer

  include JpPrefecture
  jp_prefecture :prefecture_code
end

prefecture というメソッドが生成され、都道府県コード、都道府県名が参照できるようになります:

place = Place.new
place.prefecture_code = 13
place.prefecture.name
# => "東京都"

生成されるメソッド名は method_name というオプションで指定することができます:

# model
jp_prefecture :prefecture_code, method_name: :pref

place = Place.new
place.prefecture_code = 13
place.pref.name
# => "東京都"

テンプレートで使用する

collection_select を使用して、都道府県のセレクトボックスを生成することができます。:

f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name

# 英語表記で出力
f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name_e

マイグレーション

カラムのタイプは integerstring で作成してください。

マイグレーションのサンプル:

class AddPrefectureCodeToPlaces < ActiveRecord::Migration
  def change
    add_column :places, :prefecture_code, :integer
  end
end

都道府県のマッピング情報を変更する

デフォルトのマッピング情報以外のものを使用したい場合、以下のようにカスタマイズされた マッピングデータを指定することができます:

custom_mapping_path = "/path/to/mapping_data.yml"

JpPrefecture.setup do |config|
  config.mapping_data = YAML.load_file(custom_mapping_path)
end

マッピングデータのフォーマットについては prefecture.yml を参考にしてください。

郵便番号の情報を変更する

custom_zip_mapping_path = "/path/to/zip_mapping_data.yml"

JpPrefecture.setup do |config|
  config.zip_mapping_data = YAML.load_file(custom_zip_mapping_path)
end

データのフォーマットについては zip.yml を参考にしてください。

インストール

以下の行を Gemfile に記述してから:

gem 'jp_prefecture'

bundle を実行してください。

または、手動でインストールしてください:

$ gem install jp_prefecture

ドキュメント

http://rdoc.info/github/chocoby/jp_prefecture/master/frames/index

サポートしているバージョン

  • Ruby: 2.4 - 3.0
  • Rails: 5.0 - 6.1

これより古い Ruby/Rails バージョンを使用する場合は、v0.11.0 を利用してください。

Contributing

CONTRIBUTING.md を確認してください。

GitHub

https://github.com/chocoby/jp_prefecture

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].