funicular-japan-prefectures 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +57 -0
- data/assets/japan_prefectures.css +5 -0
- data/lib/components/japan_prefectures_component.rb +43 -0
- data/lib/japan_prefectures/data.rb +103 -0
- data/lib/japan_prefectures.rb +7 -0
- metadata +60 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 286339601e2529f1b9f84c0c61562f9056420fa8de21f32a97dd1367a15bc561
|
|
4
|
+
data.tar.gz: fbb177160994c32b467bb9f5cc29ae65f910115bea29638ff0e03f66e6bd25d9
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 31061d41820918942e0e1950c9767478df499129202833f8e55e137330a404456d90a95d29dbd9225860fa540e818a61be9824152d38c203ba25cd6734b94442
|
|
7
|
+
data.tar.gz: edf980708191a82dd4e81afe7ea1279fb34dbf4adff0018b80daffa2aa3e6fd225bd2faceab9dc2b92f84c51245a5a02c4fb8d7328ed1469b44158babc74dbdb
|
data/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# funicular-japan-prefectures
|
|
2
|
+
|
|
3
|
+
A Japan prefecture dropdown for
|
|
4
|
+
[Funicular](https://github.com/picoruby/funicular), packaged as a
|
|
5
|
+
plugin gem. Ships the JIS X 0401 code table (1..47) as data shared by
|
|
6
|
+
the client component and server-side lookups.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
# Gemfile
|
|
12
|
+
group :funicular do
|
|
13
|
+
gem "funicular-japan-prefectures"
|
|
14
|
+
end
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The component sources are prepended to your client bundle by
|
|
18
|
+
`funicular:compile`, and `funicular_plugin_include_tags` serves the
|
|
19
|
+
stylesheet.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
component Funicular::Plugins::JapanPrefectures::Component,
|
|
25
|
+
value: prefecture_id, # optional preselected JIS code
|
|
26
|
+
select_id: "address-prefecture",
|
|
27
|
+
select_class: "border rounded px-3 py-2",
|
|
28
|
+
prompt: "選択してください",
|
|
29
|
+
on_change: ->(id) { @prefecture_id = id }
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- Controlled component: the parent owns `value` and re-renders after
|
|
33
|
+
`on_change`, which receives the Integer JIS code (nil for the blank
|
|
34
|
+
prompt option).
|
|
35
|
+
- `prompt` — label of the blank option (default: 都道府県).
|
|
36
|
+
- `select_id` / `select_class` — presentation knobs.
|
|
37
|
+
|
|
38
|
+
## Server-side data
|
|
39
|
+
|
|
40
|
+
The code table has no Funicular dependency, so Rails code (mailers,
|
|
41
|
+
PDFs, plain ERB forms) can use the same source of truth:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
require "japan_prefectures/data"
|
|
45
|
+
|
|
46
|
+
Funicular::Plugins::JapanPrefectures.name_of(13) # => "東京都"
|
|
47
|
+
Funicular::Plugins::JapanPrefectures.id_of("東京都") # => 13
|
|
48
|
+
Funicular::Plugins::JapanPrefectures.valid_id?(48) # => false
|
|
49
|
+
Funicular::Plugins::JapanPrefectures.select_options # => [["北海道", 1], ...]
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Test
|
|
53
|
+
|
|
54
|
+
```console
|
|
55
|
+
bundle install && npm install
|
|
56
|
+
bundle exec rake test
|
|
57
|
+
```
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Prefecture dropdown over the bundled JIS X 0401 list.
|
|
2
|
+
#
|
|
3
|
+
# component Funicular::Plugins::JapanPrefectures::Component,
|
|
4
|
+
# value: prefecture_id, # optional preselected JIS code
|
|
5
|
+
# select_id: "address-prefecture",
|
|
6
|
+
# select_class: "...",
|
|
7
|
+
# prompt: "選択してください", # blank option label
|
|
8
|
+
# on_change: ->(id) { ... } # Integer JIS code, nil for blank
|
|
9
|
+
#
|
|
10
|
+
# Controlled component: the parent owns the value and re-renders with a
|
|
11
|
+
# new one after on_change. The option list is fixed and lives in
|
|
12
|
+
# lib/japan_prefectures/data.rb.
|
|
13
|
+
class JapanPrefecturesComponent < Funicular::Component
|
|
14
|
+
def handle_change(_event)
|
|
15
|
+
node = @refs[:select]
|
|
16
|
+
raw = node ? node[:value].to_s : ""
|
|
17
|
+
callback = props[:on_change]
|
|
18
|
+
callback.call(raw.empty? ? nil : raw.to_i) if callback
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def render
|
|
22
|
+
value = props[:value].to_s
|
|
23
|
+
select(
|
|
24
|
+
ref: :select,
|
|
25
|
+
id: props[:select_id],
|
|
26
|
+
class: "funicular-japan-prefectures__select #{props[:select_class]}",
|
|
27
|
+
onchange: :handle_change
|
|
28
|
+
) do
|
|
29
|
+
option(value: "", selected: value.empty?) { props[:prompt] || "都道府県" }
|
|
30
|
+
prefectures = Funicular::Plugins::JapanPrefectures::PREFECTURES
|
|
31
|
+
i = 0
|
|
32
|
+
size = prefectures.size
|
|
33
|
+
while i < size
|
|
34
|
+
pref = prefectures[i]
|
|
35
|
+
pref_id = pref["id"].to_s
|
|
36
|
+
option(value: pref_id, key: pref_id, selected: pref_id == value) do
|
|
37
|
+
pref["name"]
|
|
38
|
+
end
|
|
39
|
+
i += 1
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# JIS X 0401 prefecture codes (1..47) with Japanese names. This file
|
|
2
|
+
# has no Funicular dependency so server-side (CRuby) code can require
|
|
3
|
+
# "japan_prefectures/data" for id/name lookups, while the same source
|
|
4
|
+
# ships to the client bundle as the component's option list.
|
|
5
|
+
module Funicular
|
|
6
|
+
module Plugins
|
|
7
|
+
module JapanPrefectures
|
|
8
|
+
PREFECTURES = [
|
|
9
|
+
{ "id" => 1, "name" => "北海道" },
|
|
10
|
+
{ "id" => 2, "name" => "青森県" },
|
|
11
|
+
{ "id" => 3, "name" => "岩手県" },
|
|
12
|
+
{ "id" => 4, "name" => "宮城県" },
|
|
13
|
+
{ "id" => 5, "name" => "秋田県" },
|
|
14
|
+
{ "id" => 6, "name" => "山形県" },
|
|
15
|
+
{ "id" => 7, "name" => "福島県" },
|
|
16
|
+
{ "id" => 8, "name" => "茨城県" },
|
|
17
|
+
{ "id" => 9, "name" => "栃木県" },
|
|
18
|
+
{ "id" => 10, "name" => "群馬県" },
|
|
19
|
+
{ "id" => 11, "name" => "埼玉県" },
|
|
20
|
+
{ "id" => 12, "name" => "千葉県" },
|
|
21
|
+
{ "id" => 13, "name" => "東京都" },
|
|
22
|
+
{ "id" => 14, "name" => "神奈川県" },
|
|
23
|
+
{ "id" => 15, "name" => "新潟県" },
|
|
24
|
+
{ "id" => 16, "name" => "富山県" },
|
|
25
|
+
{ "id" => 17, "name" => "石川県" },
|
|
26
|
+
{ "id" => 18, "name" => "福井県" },
|
|
27
|
+
{ "id" => 19, "name" => "山梨県" },
|
|
28
|
+
{ "id" => 20, "name" => "長野県" },
|
|
29
|
+
{ "id" => 21, "name" => "岐阜県" },
|
|
30
|
+
{ "id" => 22, "name" => "静岡県" },
|
|
31
|
+
{ "id" => 23, "name" => "愛知県" },
|
|
32
|
+
{ "id" => 24, "name" => "三重県" },
|
|
33
|
+
{ "id" => 25, "name" => "滋賀県" },
|
|
34
|
+
{ "id" => 26, "name" => "京都府" },
|
|
35
|
+
{ "id" => 27, "name" => "大阪府" },
|
|
36
|
+
{ "id" => 28, "name" => "兵庫県" },
|
|
37
|
+
{ "id" => 29, "name" => "奈良県" },
|
|
38
|
+
{ "id" => 30, "name" => "和歌山県" },
|
|
39
|
+
{ "id" => 31, "name" => "鳥取県" },
|
|
40
|
+
{ "id" => 32, "name" => "島根県" },
|
|
41
|
+
{ "id" => 33, "name" => "岡山県" },
|
|
42
|
+
{ "id" => 34, "name" => "広島県" },
|
|
43
|
+
{ "id" => 35, "name" => "山口県" },
|
|
44
|
+
{ "id" => 36, "name" => "徳島県" },
|
|
45
|
+
{ "id" => 37, "name" => "香川県" },
|
|
46
|
+
{ "id" => 38, "name" => "愛媛県" },
|
|
47
|
+
{ "id" => 39, "name" => "高知県" },
|
|
48
|
+
{ "id" => 40, "name" => "福岡県" },
|
|
49
|
+
{ "id" => 41, "name" => "佐賀県" },
|
|
50
|
+
{ "id" => 42, "name" => "長崎県" },
|
|
51
|
+
{ "id" => 43, "name" => "熊本県" },
|
|
52
|
+
{ "id" => 44, "name" => "大分県" },
|
|
53
|
+
{ "id" => 45, "name" => "宮崎県" },
|
|
54
|
+
{ "id" => 46, "name" => "鹿児島県" },
|
|
55
|
+
{ "id" => 47, "name" => "沖縄県" }
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
# Returns the Japanese name for a JIS code, or nil. Accepts
|
|
59
|
+
# Integer or String ids so form values pass straight through.
|
|
60
|
+
def self.name_of(id)
|
|
61
|
+
return nil if id.nil?
|
|
62
|
+
wanted = id.to_s
|
|
63
|
+
i = 0
|
|
64
|
+
size = PREFECTURES.size
|
|
65
|
+
while i < size
|
|
66
|
+
return PREFECTURES[i]["name"] if PREFECTURES[i]["id"].to_s == wanted
|
|
67
|
+
i += 1
|
|
68
|
+
end
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Returns the JIS code for an exact Japanese name, or nil.
|
|
73
|
+
def self.id_of(name)
|
|
74
|
+
return nil if name.nil?
|
|
75
|
+
wanted = name.to_s
|
|
76
|
+
i = 0
|
|
77
|
+
size = PREFECTURES.size
|
|
78
|
+
while i < size
|
|
79
|
+
return PREFECTURES[i]["id"] if PREFECTURES[i]["name"] == wanted
|
|
80
|
+
i += 1
|
|
81
|
+
end
|
|
82
|
+
nil
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# [[name, id], ...] pairs in JIS order, ready for Rails'
|
|
86
|
+
# options_for_select on the server side.
|
|
87
|
+
def self.select_options
|
|
88
|
+
result = []
|
|
89
|
+
i = 0
|
|
90
|
+
size = PREFECTURES.size
|
|
91
|
+
while i < size
|
|
92
|
+
result << [ PREFECTURES[i]["name"], PREFECTURES[i]["id"] ]
|
|
93
|
+
i += 1
|
|
94
|
+
end
|
|
95
|
+
result
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def self.valid_id?(id)
|
|
99
|
+
!name_of(id).nil?
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: funicular-japan-prefectures
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- HASUMI Hitoshi
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: funicular
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.4.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 0.4.0
|
|
26
|
+
description: A prefecture dropdown backed by the JIS X 0401 code table (1..47), packaged
|
|
27
|
+
as a Funicular plugin gem. The data module is CRuby-safe for server-side lookups.
|
|
28
|
+
email:
|
|
29
|
+
- hasumikin@gmail.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- README.md
|
|
35
|
+
- assets/japan_prefectures.css
|
|
36
|
+
- lib/components/japan_prefectures_component.rb
|
|
37
|
+
- lib/japan_prefectures.rb
|
|
38
|
+
- lib/japan_prefectures/data.rb
|
|
39
|
+
homepage: https://github.com/hasumikin/funicular-japan-prefectures
|
|
40
|
+
licenses:
|
|
41
|
+
- MIT
|
|
42
|
+
metadata: {}
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: 3.2.0
|
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
requirements: []
|
|
57
|
+
rubygems_version: 4.0.10
|
|
58
|
+
specification_version: 4
|
|
59
|
+
summary: Japan prefecture select component for Funicular
|
|
60
|
+
test_files: []
|