jp_local_gov 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4860d4939032f21410e79a628f6000fd037f755e9997903e670e8a9195c272c9
4
- data.tar.gz: 2f263d1c09e9db4e138fbaac8bd31bc10f26692bf1bd6b2a1ee07a9c4f879578
3
+ metadata.gz: 4759f37789b0e5f5c1919b7fb23038c5ec76e8024e5043207890e79a38ad4a39
4
+ data.tar.gz: a1ef62fe017d208311ee529f1fe3d7c5903aa31fa3e4316eb68db3ff08a0695e
5
5
  SHA512:
6
- metadata.gz: af1984c55d50f4368dfda951e6cc2a18e4a564e54002da1728b1a5c12baeeaddfe89efd38bd36c759d1ce5ff5966f0870eb7e35e42315b703c112eb056cc599d
7
- data.tar.gz: ee7b9dc6f8cca9f271a173acac5052a517c9882fb34911f12503782879e29c5239aa3773ea856d5765550b5e8965ad059863f2686e49a36f7200cfa3d18e9c68
6
+ metadata.gz: 9e9a9099eab0080b5c127b41c57758fc0af22a2632aa28c763876fba3788ba8c6f1335082133b64f4decd7b0ca7a3f1c8eb70c7c8457937037f0d004fbc754b9
7
+ data.tar.gz: 769f134eaf165f276b6ba01ac5b593fddf16f65e4d65ba80745675f33a09ff0304868f5674d1160cf1a06622561e04d7efdb468e9378bdc4fe61c35a7176ac47
@@ -0,0 +1,50 @@
1
+ name: BugReport
2
+ description: File a bug report
3
+ title: "[Bug]: "
4
+ assignees: IkumaTadokoro
5
+ labels: ["bug"]
6
+ body:
7
+ - type: markdown
8
+ attributes:
9
+ value: "Thanks for taking the time to fill out this bug report!"
10
+ - type: textarea
11
+ id: description
12
+ attributes:
13
+ label: Summary
14
+ validations:
15
+ required: false
16
+ - type: textarea
17
+ id: reproduction
18
+ attributes:
19
+ label: Steps to reproduce the bug
20
+ placeholder: |
21
+ 1. ...
22
+ 2. ...
23
+ 3. ...
24
+ validations:
25
+ required: false
26
+ - type: textarea
27
+ id: expected
28
+ attributes:
29
+ label: Expected
30
+ validations:
31
+ required: false
32
+ - type: textarea
33
+ id: log
34
+ attributes:
35
+ label: Relevant log output
36
+ description: Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks.
37
+ render: shell
38
+ validations:
39
+ required: false
40
+ - type: textarea
41
+ id: environment
42
+ attributes:
43
+ label: Environment
44
+ validations:
45
+ required: false
46
+ - type: textarea
47
+ id: related_issues
48
+ attributes:
49
+ label: Related Issue
50
+ placeholder: "Ref: #3899"
@@ -0,0 +1,22 @@
1
+ name: Feature Suggestion
2
+ description: Suggestions for new feature
3
+ assignees: IkumaTadokoro
4
+ title: "[Feature]: "
5
+ labels: ["enhancement"]
6
+ body:
7
+ - type: markdown
8
+ attributes:
9
+ value: "Thanks for taking the time to fill out this feature proposal!"
10
+ - type: textarea
11
+ id: description
12
+ attributes:
13
+ label: Description of new feature
14
+ validations:
15
+ required: false
16
+ - type: textarea
17
+ id: reason
18
+ attributes:
19
+ label: Reason
20
+ description: Please fill out the reason why your idea should be adopted.
21
+ validations:
22
+ required: false
@@ -0,0 +1,15 @@
1
+ {
2
+ "problemMatcher": [
3
+ {
4
+ "owner": "codespell",
5
+ "pattern": [
6
+ {
7
+ "regexp": "^.+/(.+?):(\\d+): (.+)$",
8
+ "file": 1,
9
+ "line": 2,
10
+ "message": 3
11
+ }
12
+ ]
13
+ }
14
+ ]
15
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "problemMatcher": [
3
+ {
4
+ "owner": "misspell",
5
+ "pattern": [
6
+ {
7
+ "regexp": "^(.+):(\\d+):\\d+: (.+)$",
8
+ "file": 1,
9
+ "line": 2,
10
+ "message": 3
11
+ }
12
+ ]
13
+ }
14
+ ]
15
+ }
@@ -0,0 +1,44 @@
1
+ name: Spell Checking
2
+
3
+ on:
4
+ push:
5
+ branches: ["main"]
6
+ tags: ["**"]
7
+ pull_request:
8
+ branches: ["main"]
9
+
10
+ jobs:
11
+ codespell:
12
+ name: Check spelling all files with codespell
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ python-version: [3.8]
17
+ steps:
18
+ - uses: actions/checkout@v2
19
+ - name: Set up Python ${{ matrix.python-version }}
20
+ uses: actions/setup-python@v2
21
+ with:
22
+ python-version: ${{ matrix.python-version }}
23
+ - name: Install dependencies
24
+ run: |
25
+ python -m pip install --upgrade pip
26
+ pip install codespell
27
+ if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
28
+ - name: enable problem matcher
29
+ run: |
30
+ echo "::add-matcher::.github/codespell-problem-matcher.json"
31
+ - name: Check spelling with codespell
32
+ run: codespell --ignore-words=codespell.txt || exit 1
33
+ misspell:
34
+ name: Check spelling all files in commit with misspell
35
+ runs-on: ubuntu-latest
36
+ steps:
37
+ - uses: actions/checkout@v2
38
+ - name: Install
39
+ run: wget -O - -q https://git.io/misspell | sh -s -- -b .
40
+ - name: enable problem matcher
41
+ run: |
42
+ echo "::add-matcher::.github/misspell-problem-matcher.json"
43
+ - name: Misspell
44
+ run: git ls-files --empty-directory | xargs ./misspell -i 'enviromnent' -error
data/.gitignore CHANGED
@@ -5,7 +5,6 @@
5
5
  /doc/
6
6
  /pkg/
7
7
  /spec/reports/
8
- /tmp/
9
8
  vendor/bundle
10
9
 
11
10
  # rspec failure tracking
data/.rubocop.yml CHANGED
@@ -9,6 +9,14 @@ Style/StringLiteralsInInterpolation:
9
9
  Enabled: true
10
10
  EnforcedStyle: double_quotes
11
11
 
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - spec/**/*
15
+
16
+ Metrics/ClassLength:
17
+ Exclude:
18
+ - spec/**/*
19
+
12
20
  Layout/LineLength:
13
21
  Max: 120
14
22
 
data/CHANGELOG.md CHANGED
@@ -1,4 +1,35 @@
1
- ## [Unreleased]
1
+ # [0.2.0](https://github.com/IkumaTadokoro/jp_local_gov/compare/v0.1.0...v0.2.0) (2022-01-14)
2
+
3
+ ### chore
4
+
5
+ * Add RSpec Runner for JetBrains IDE ([83d8c86](https://github.com/IkumaTadokoro/jp_local_gov/commit/83d8c8649f69506c9a08acf275960480316eb988))
6
+ * Make JpLocalGov::Data::Importer#prefecture_capital? to return true/false ([82daafd](https://github.com/IkumaTadokoro/jp_local_gov/commit/82daafd19b47e52ff2c1c341a243aef7e5dffae4))
7
+
8
+ ### ci
9
+
10
+ * Add spell checking GitHub Actions Workflow ([fc23bd1](https://github.com/IkumaTadokoro/jp_local_gov/commit/fc23bd1a3c10cc49f8aaf4e869c0062b50255f03))
11
+
12
+ ### docs
13
+
14
+ * 💅 ([61c6d66](https://github.com/IkumaTadokoro/jp_local_gov/commit/61c6d66a43b75339036656a1b110dec97156177e))
15
+ * Add how to use JpLocalGov.valid_code? ([2e14d71](https://github.com/IkumaTadokoro/jp_local_gov/commit/2e14d71c146d823e4bc3fa268da2e3a0c5f7ebe1))
16
+ * Add issue template ([9f416a3](https://github.com/IkumaTadokoro/jp_local_gov/commit/9f416a335247c3be5c52e450b914bd73c380092d)), closes [#47](https://github.com/IkumaTadokoro/jp_local_gov/issues/47)
17
+
18
+ ### feat
19
+
20
+ * Add JpLocalGov::Random module ([bae16d7](https://github.com/IkumaTadokoro/jp_local_gov/commit/bae16d7b8ebeff1871b2b5586f0b916253c462e7)), closes [#65](https://github.com/IkumaTadokoro/jp_local_gov/issues/65)
21
+ * Add JpLocalGov.all_xxx ([f522686](https://github.com/IkumaTadokoro/jp_local_gov/commit/f52268612a5c41435e4a20f3759c9d7d70686a62)), closes [#54](https://github.com/IkumaTadokoro/jp_local_gov/issues/54)
22
+ * Make JpLocalGov.valid_code? public ([e46b4e7](https://github.com/IkumaTadokoro/jp_local_gov/commit/e46b4e79f3fd0098bd2b509123f866a92c01ee37)), closes [#55](https://github.com/IkumaTadokoro/jp_local_gov/issues/55)
23
+
24
+ ### fix
25
+
26
+ * Fix a task(`jp_local_gov:data:update_all`) ([e21125c](https://github.com/IkumaTadokoro/jp_local_gov/commit/e21125cda74c62e7b45b528ee0f908cfbab6b039)), closes [#60](https://github.com/IkumaTadokoro/jp_local_gov/issues/60)
27
+
28
+ ### style
29
+
30
+ * Add rubocop-rake and rubocop-rspec ([eb5f7d2](https://github.com/IkumaTadokoro/jp_local_gov/commit/eb5f7d22066a65d41d112e60dc8fc7c9b8b71c47))
31
+ * omit parentheses for internal DSL(RSpec) ([3c23ec5](https://github.com/IkumaTadokoro/jp_local_gov/commit/3c23ec5a19460e2c8ed496b61536ebd800cfeb03))
32
+ * Remove Metrics rule for spec ([e362565](https://github.com/IkumaTadokoro/jp_local_gov/commit/e3625658376a6b0fd8ac5cea97b0d857cd6f6e7b))
2
33
 
3
34
  ## [0.1.0] - 2021-11-28
4
35
 
data/Gemfile CHANGED
@@ -15,6 +15,8 @@ group :development do
15
15
  gem "rbs"
16
16
  gem "rubocop"
17
17
  gem "rubocop-fjord"
18
+ gem "rubocop-rake"
19
+ gem "rubocop-rspec"
18
20
  gem "sqlite3"
19
21
  gem "steep"
20
22
  end
data/README.md CHANGED
@@ -1,8 +1,25 @@
1
+ [![Gem Version](https://badge.fury.io/rb/jp_local_gov.svg)](https://badge.fury.io/rb/jp_local_gov)
2
+ [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/IkumaTadokoro/jp_local_gov/blob/main/LICENSE.txt)
3
+ ![Gem](https://img.shields.io/gem/dt/jp_local_gov)
4
+
1
5
  # JpLocalGov
2
6
 
3
7
  Convert local government code based on JIS X 0402 to local government name in Japan.
4
8
  Reference(Japanese): [全国地方公共団体コード \- Wikipedia](https://ja.wikipedia.org/wiki/%E5%85%A8%E5%9B%BD%E5%9C%B0%E6%96%B9%E5%85%AC%E5%85%B1%E5%9B%A3%E4%BD%93%E3%82%B3%E3%83%BC%E3%83%89)
5
9
 
10
+ ### Issues
11
+
12
+ If you find any bugs or concerns, or have suggestions for new features, please feel free to add them as an [GitHub Issue](https://github.com/IkumaTadokoro/jp_local_gov/issues/new/choose)!
13
+
14
+ There are issue templates for bug and feature. If neither, please use the blank template.
15
+
16
+ ### Special Thanks
17
+
18
+ This gem is based on and inspired by [chocoby/jp_prefecture](https://github.com/chocoby/jp_prefecture).
19
+ And I referenced [kufu/jpostcode-data](https://github.com/kufu/jpostcode-data) for the method of data acquisition.
20
+
21
+ Thanks!😁
22
+
6
23
  ## Installation
7
24
 
8
25
  Add this line to your application's Gemfile:
@@ -60,7 +77,7 @@ This search function is an exact match search.
60
77
 
61
78
  ```ruby
62
79
  misato = JpLocalGov.where(city: "美郷町")
63
- # => [#<JpLocalGov::LocalGov:0x00007fb1c594cb08 @code="054348", @prefecture_code="05", @prefecture="秋田県", @prefecture_kana="アキタケン", @city="美郷町", @city_kana="ミサトチョウ", @prefecture_capital=false>, #<JpLocalGov::LocalGov:8 @code="324485", @prefecture_code="32", @prefecture="島根県", @prefecture_kana="シマネケン", @city="美郷町", @city_kana="ミサトチョウ", @prefecture_capital=false>, #<JpLocalGov::LocalGov:0x00007fb1c1a3ce40 @code="454311", @prefectuefecture="宮崎県", @prefecture_kana="ミヤザキケン", @city="美郷町", @city_kana="ミサトチョウ", @prefecture_capital=false>]
80
+ # => [#<JpLocalGov::LocalGov:0x00007fb1c594cb08 @code="054348", @prefecture_code="05", @prefecture="秋田県", @prefecture_kana="アキタケン", @city="美郷町", @city_kana="ミサトチョウ", @prefecture_capital=false>, #<JpLocalGov::LocalGov:8 @code="324485", @prefecture_code="32", @prefecture="島根県", @prefecture_kana="シマネケン", @city="美郷町", @city_kana="ミサトチョウ", @prefecture_capital=false>, #<JpLocalGov::LocalGov:0x00007fb1c1a3ce40 @code="454311", @prefecture="宮崎県", @prefecture_kana="ミヤザキケン", @city="美郷町", @city_kana="ミサトチョウ", @prefecture_capital=false>]
64
81
  misato.map { "#{_1.prefecture}:#{_1.city}" }
65
82
  # => ["秋田県:美郷町", "島根県:美郷町", "宮崎県:美郷町"]
66
83
 
@@ -84,13 +101,22 @@ The following attributes can be specified for the condition.
84
101
  | city_kana | String | "チヨダク" |
85
102
  | prefecture_capital | true or false | false |
86
103
 
104
+ ### All data
105
+
106
+ You can get all local governments using `JpLocalGov.all`
107
+
108
+ ```ruby
109
+ JpLocalGov.all
110
+ # => [#<JpLocalGov::LocalGov:0x00007fdf3a9c6758 @code="011002", @prefecture_code="01", @prefecture="北海道", @prefecture_kana="ホッカイドウ", @city="札幌市na="サッポロシ", @prefecture_capital=true>, #<JpLocalGov::LocalGov:0x00007fdf3a9c6730 @code="011011",...
111
+ ```
112
+
87
113
  ### Usage on Rails (ActiveRecord)
88
114
 
89
115
  Include JpLocalGov to Model which ActiveRecord::Base inherited.
90
116
 
91
117
  ```ruby
92
- # app/models/insurrance_fees.rb:
93
- class Place < ActiveRecord::Base
118
+ # app/models/insurance_fee.rb:
119
+ class InsuranceFee < ActiveRecord::Base
94
120
  # local_gov_code:String
95
121
 
96
122
  include JpLocalGov
@@ -110,13 +136,43 @@ insurance_fee.local_government.city
110
136
  In Migration file, set `local_gov_code` column type to `string`.
111
137
 
112
138
  ```ruby
113
- class AddLocalGovCodeToinsuranceFees < ActiveRecord::Migration
139
+ class AddLocalGovCodeToInsuranceFees < ActiveRecord::Migration
114
140
  def change
115
141
  add_column :insurance_fees, :local_gov_code, :string
116
142
  end
117
143
  end
118
144
  ```
119
145
 
146
+ ### Validation
147
+
148
+ You can use `JpLocalGov.valid_code?(local_gov_code)` in `validate` method.
149
+
150
+ ```ruby
151
+ class InsuranceFee < ApplicationRecord
152
+ include JpLocalGov
153
+ jp_local_gov :local_gov_code
154
+
155
+ validate :valid_code?
156
+
157
+ def valid_code?
158
+ unless JpLocalGov.valid_code?(local_gov_code)
159
+ errors.add(:local_gov_code, "is not valid code")
160
+ end
161
+ end
162
+ end
163
+ ```
164
+
165
+ This method inspect code by [check digits defined in JISX0402](https://www.soumu.go.jp/main_content/000137948.pdf).
166
+ (And also check code is String.)
167
+
168
+ ### View Template
169
+
170
+ Use `collection_select` to generate selector in view:
171
+
172
+ ```ruby
173
+ f.collection_select :local_gov_code, JpLocalGov.all, :code, :city # e.g. code: 131016, city: "千代田区"
174
+ ```
175
+
120
176
  ## Development
121
177
 
122
178
  ### Steps
@@ -138,9 +194,62 @@ end
138
194
  | `bin/lint` | Run Rubocop |
139
195
  | `bin/steep` | Run `steep stats` and `steep check` |
140
196
 
197
+ ### Running Test via JetBrains IDE (e.g. RubyMine)
198
+
199
+ This gem use 'Appraisal' to inspect several versions of Rails.
200
+
201
+ So you should run rspec by `bundle exec appraisal rspec` and you won't be able to run spec via IDE (only run via your
202
+ terminal).
203
+
204
+ If you use JetBrains IDE and you want to run spec via IDE, try to configure the following steps.
205
+
206
+ 1. Open 「Edit Configurations...」
207
+ 2. Open 「Edit Configuration templates...」
208
+ 3. Select 「RSpec」
209
+ 4. Check 「Use custom RSpec runner script:」 and fill the script with `[CLONE_DIR]/bin/spec_runner.rb`
210
+ 5. Click「APPLY」, then you can run spec via IDE!!🎉
211
+
212
+ If you have already run spec via IDE before configuration, delete the existing configuration and try to configure the
213
+ above steps
214
+
215
+ ## Update local government data
216
+
217
+ This Gem update local government data (format: JSON) automatically once a month by [GitHub Actions](https://github.com/IkumaTadokoro/jp_local_gov/actions/workflows/auto-update.yml).
218
+
219
+ - Data source: https://www.soumu.go.jp/denshijiti/code.html
220
+ - Output: https://github.com/IkumaTadokoro/jp_local_gov/tree/main/data/json
221
+
222
+ Fetch script is written in Ruby and Rake Task. So you can run update data at local by the following command.
223
+
224
+ ```ruby
225
+ bundle exec rake jp_local_gov:data:update_all
226
+ ```
227
+
141
228
  ## Contributing
142
229
 
143
- Bug reports and pull requests are welcome on GitHub at https://github.com/IkumaTadokoro/jp_local_gov. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/IkumaTadokoro/jp_local_gov/blob/main/CODE_OF_CONDUCT.md).
230
+ Bug reports and pull requests are welcome on GitHub at https://github.com/IkumaTadokoro/jp_local_gov. This project is
231
+ intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
232
+ the [code of conduct](https://github.com/IkumaTadokoro/jp_local_gov/blob/main/CODE_OF_CONDUCT.md).
233
+
234
+ ### Spell Checking
235
+
236
+ We are running [misspell](https://github.com/client9/misspell) which is mainly written in
237
+ [Golang](https://golang.org/) to check spelling with [GitHub Actions](../.github/workflows/spell-checking.yml). Correct
238
+ commonly misspelled English words quickly with `misspell`. `misspell` is different from most other spell checkers
239
+ because it doesn't use a custom dictionary. You can run `misspell` locally against all files with:
240
+
241
+ $ find . -type f | xargs ./misspell -error
242
+
243
+ Notable `misspell` help options or flags are:
244
+
245
+ * `-i` string: ignore the following corrections, comma separated
246
+ * `-w`: Overwrite file with corrections (default is just to display)
247
+
248
+ We also run [codespell](https://github.com/codespell-project/codespell) with GitHub Actions to check spelling and
249
+ [codespell](https://pypi.org/project/codespell/) runs against a [small custom dictionary](../codespell.txt).
250
+ `codespell` is written in [Python](https://www.python.org/) and you can run it with:
251
+
252
+ $ codespell --ignore-words=codespell.txt
144
253
 
145
254
  ## License
146
255
 
@@ -148,4 +257,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
148
257
 
149
258
  ## Code of Conduct
150
259
 
151
- Everyone interacting in the JpLocalGov project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/IkumaTadokoro/jp_local_gov/blob/main/CODE_OF_CONDUCT.md).
260
+ Everyone interacting in the JpLocalGov project's codebases, issue trackers, chat rooms and mailing lists is expected to
261
+ follow the [code of conduct](https://github.com/IkumaTadokoro/jp_local_gov/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ # !/usr/bin/env ruby
4
+
5
+ # Most of the code here is copied from /lib/appraisal/cli.rb in the appraisal gem library
6
+ require "rubygems"
7
+ require "bundler/setup"
8
+ require "appraisal"
9
+ require "appraisal/cli"
10
+
11
+ begin
12
+ `bundle exec appraisal list`.split(/\R/) do |appraisal_name|
13
+ cmd = [appraisal_name, "rspec"] + ARGV
14
+ Appraisal::CLI.start(cmd)
15
+ end
16
+ rescue Appraisal::AppraisalsNotFound => e
17
+ puts e.message
18
+ exit 127
19
+ end
data/codespell.txt ADDED
File without changes
data/data/importer.rb CHANGED
@@ -56,7 +56,7 @@ module JpLocalGov
56
56
 
57
57
  def prefecture_capital?(prefecture, city)
58
58
  prefecture_capital_list = JSON.parse(File.open(File.expand_path("prefecture_capital.json", __dir__)).read)
59
- prefecture_capital_list.find do |prefecture_capital|
59
+ prefecture_capital_list.any? do |prefecture_capital|
60
60
  prefecture_capital["prefecture"] == prefecture && prefecture_capital["city"] == city
61
61
  end
62
62
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../jp_local_gov"
4
+
5
+ module JpLocalGov
6
+ module Random
7
+ DATA_DIR = "#{File.dirname(__FILE__)}/../../data/json/".freeze
8
+ RANDOMIZE_TARGET = %w[code city city_kana prefecture prefecture_code prefecture_kana].freeze
9
+
10
+ module_function
11
+
12
+ RANDOMIZE_TARGET.each do |target|
13
+ define_method target.to_s do
14
+ sample_data.send(target)
15
+ end
16
+ end
17
+
18
+ def sample_data
19
+ file_name = ("1".."47").to_a.sample&.rjust(2, "0")
20
+ json_file = "#{DATA_DIR}#{file_name}.json"
21
+ data = JSON.parse(File.read(json_file), { symbolize_names: true })
22
+ JpLocalGov::LocalGov.new(data[data.keys.sample])
23
+ end
24
+
25
+ private_class_method :sample_data
26
+ private_constant :DATA_DIR, :RANDOMIZE_TARGET
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JpLocalGov
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/jp_local_gov.rb CHANGED
@@ -9,6 +9,8 @@ module JpLocalGov
9
9
  DATA_DIR = "#{File.dirname(__FILE__)}/../data/json/".freeze
10
10
  CHECK_DIGITS_INDEX = 5
11
11
  CHECK_BASE = 11
12
+ PREFECTURE_RANGE = 1..47
13
+ VALID_CODE_LENGTH = 6
12
14
 
13
15
  module_function
14
16
 
@@ -17,7 +19,7 @@ module JpLocalGov
17
19
  end
18
20
 
19
21
  def find(local_gov_code)
20
- return nil unless local_gov_code.is_a?(String) && valid_code?(local_gov_code)
22
+ return nil unless valid_code?(local_gov_code)
21
23
 
22
24
  json_file = "#{DATA_DIR}#{local_gov_code[0..1]}.json"
23
25
  data = JSON.parse(File.open(json_file).read, { symbolize_names: true })
@@ -30,9 +32,9 @@ module JpLocalGov
30
32
  def where(conditions)
31
33
  return nil unless conditions.is_a?(Hash)
32
34
 
33
- json_files = [*1..47].map { format("%02<number>d", number: _1) }.map { "#{DATA_DIR}#{_1}.json" }
35
+ json_files = prefecture_code_list.map { "#{DATA_DIR}#{_1}.json" }
34
36
  results = json_files.map do |json_file|
35
- data = JSON.parse(File.open(json_file).read, { symbolize_names: true })
37
+ data = JSON.parse(File.read(json_file), { symbolize_names: true })
36
38
  build_local_gov(data, conditions)
37
39
  end.flatten.compact
38
40
  return nil if results.empty?
@@ -40,6 +42,30 @@ module JpLocalGov
40
42
  results
41
43
  end
42
44
 
45
+ # Inspect code by check digits defined in JISX0402
46
+ # https://www.soumu.go.jp/main_content/000137948.pdf
47
+ def valid_code?(code)
48
+ unless code.is_a?(String) && code.length == VALID_CODE_LENGTH && prefecture_code_list.include?(code[0..1])
49
+ return false
50
+ end
51
+
52
+ sub_total = code.chars
53
+ .take(CHECK_DIGITS_INDEX)
54
+ .map.with_index { |digit, index| digit.to_i * (CHECK_DIGITS_INDEX - index + 1) }
55
+ .sum
56
+ candidate = (CHECK_BASE - sub_total % CHECK_BASE) % 10
57
+ check_digits = sub_total > CHECK_BASE ? candidate : CHECK_BASE - sub_total
58
+ code[CHECK_DIGITS_INDEX] == check_digits.to_s
59
+ end
60
+
61
+ def all
62
+ json_files = prefecture_code_list.map { "#{DATA_DIR}#{_1}.json" }
63
+ json_files.flat_map do |json_file|
64
+ data = JSON.parse(File.read(json_file), { symbolize_names: true })
65
+ data.values.map { |value| JpLocalGov::LocalGov.new(value) }
66
+ end
67
+ end
68
+
43
69
  def build_local_gov(data, conditions)
44
70
  data.values
45
71
  .select { |target| filter(target, conditions) }
@@ -51,18 +77,10 @@ module JpLocalGov
51
77
  conditions.map { |condition| target[condition[0]] == condition[1] }.all?
52
78
  end
53
79
 
54
- # Inspect code by check digits defined in JISX0402
55
- # https://www.soumu.go.jp/main_content/000137948.pdf
56
- def valid_code?(code)
57
- sub_total = code.chars
58
- .take(CHECK_DIGITS_INDEX)
59
- .map.with_index { |digit, index| digit.to_i * (CHECK_DIGITS_INDEX - index + 1) }
60
- .sum
61
- candidate = (CHECK_BASE - sub_total % CHECK_BASE) % 10
62
- check_digits = sub_total > CHECK_BASE ? candidate : CHECK_BASE - sub_total
63
- code[CHECK_DIGITS_INDEX] == check_digits.to_s
80
+ def prefecture_code_list
81
+ [*PREFECTURE_RANGE].map { format("%02<number>d", number: _1) }
64
82
  end
65
83
 
66
- private_class_method :valid_code?, :build_local_gov, :filter
84
+ private_class_method :build_local_gov, :filter, :prefecture_code_list
67
85
  private_constant :CHECK_DIGITS_INDEX, :CHECK_BASE
68
86
  end
@@ -0,0 +1,9 @@
1
+ module JpLocalGov
2
+ module Random
3
+ DATA_DIR: String
4
+
5
+ RANDOMIZE_TARGET: Array[String]
6
+
7
+ def self?.sample_data: () -> JpLocalGov::LocalGov
8
+ end
9
+ end
data/sig/jp_local_gov.rbs CHANGED
@@ -5,15 +5,23 @@ module JpLocalGov
5
5
 
6
6
  CHECK_BASE: Integer
7
7
 
8
+ PREFECTURE_RANGE: Range[Integer]
9
+
10
+ VALID_CODE_LENGTH: Integer
11
+
8
12
  def self?.included: (Class | Module model_class) -> (Class | Module)
9
13
 
10
14
  def self?.find: (String local_gov_code) -> (nil | JpLocalGov::LocalGov)
11
15
 
12
16
  def self?.where: (Hash[Symbol, untyped] conditions) -> (nil | Array[JpLocalGov::LocalGov])
13
17
 
18
+ def self?.valid_code?: (String code) -> bool
19
+
20
+ def self.all: () -> Array[JpLocalGov::LocalGov]
21
+
14
22
  def self?.build_local_gov: (Hash[Symbol, untyped] data, Hash[Symbol, String] conditions) -> (nil | Array[JpLocalGov::LocalGov])
15
23
 
16
24
  def self?.filter: (Hash[Symbol, untyped] target, Hash[Symbol, String] conditions) -> bool
17
25
 
18
- def self?.valid_code?: (String code) -> bool
26
+ def prefecture_code_list: () -> Array[String]
19
27
  end
data/tmp/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *
2
+ !.gitignore
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jp_local_gov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ikuma-t
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-22 00:00:00.000000000 Z
11
+ date: 2022-01-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Convert japan local government code (JIS X 0402 based) into the local
14
14
  government name.
@@ -18,10 +18,15 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - ".github/ISSUE_TEMPLATE/bug-report.yml"
22
+ - ".github/ISSUE_TEMPLATE/feature-suggestion.yml"
23
+ - ".github/codespell-problem-matcher.json"
21
24
  - ".github/dependabot.yml"
25
+ - ".github/misspell-problem-matcher.json"
22
26
  - ".github/workflows/auto-update.yml"
23
27
  - ".github/workflows/lint.yml"
24
28
  - ".github/workflows/main.yml"
29
+ - ".github/workflows/spell-checking.yml"
25
30
  - ".github/workflows/steep.yml"
26
31
  - ".github/workflows/test.yml"
27
32
  - ".gitignore"
@@ -40,8 +45,10 @@ files:
40
45
  - bin/generate_rbs
41
46
  - bin/lint
42
47
  - bin/setup
48
+ - bin/spec_runner.rb
43
49
  - bin/steep
44
50
  - bin/test
51
+ - codespell.txt
45
52
  - data/base.rb
46
53
  - data/exporter.rb
47
54
  - data/importer.rb
@@ -99,11 +106,14 @@ files:
99
106
  - lib/jp_local_gov.rb
100
107
  - lib/jp_local_gov/base.rb
101
108
  - lib/jp_local_gov/local_gov.rb
109
+ - lib/jp_local_gov/random.rb
102
110
  - lib/jp_local_gov/version.rb
103
111
  - sig/jp_local_gov.rbs
104
112
  - sig/jp_local_gov/base.rbs
105
113
  - sig/jp_local_gov/local_gov.rbs
114
+ - sig/jp_local_gov/random.rbs
106
115
  - sig/jp_local_gov/version.rbs
116
+ - tmp/.gitignore
107
117
  homepage: https://github.com/IkumaTadokoro/jp_local_gov
108
118
  licenses:
109
119
  - MIT