era_ja 0.3.1 → 0.4.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
  SHA1:
3
- metadata.gz: ccca4808e214d0277f4dc83b3a3a7f131b619807
4
- data.tar.gz: ac2d02db9b31485f244ce04ff8938fdee6530693
3
+ metadata.gz: e63d396e7ea8b3fe5708d1612595222f744a3077
4
+ data.tar.gz: 5f157527bcb37159862adbed9adde21956243d9c
5
5
  SHA512:
6
- metadata.gz: 2f1a77b0833a94bcc00fbdbe47a051cd45e876ada349637c9a6d0eb243dd3e86b4d5b83e076b158726c24a26f4c45e52e0a571d7a8e8e042a9befd333b7e12c8
7
- data.tar.gz: f3a48f2bb1b1632e6b2bfd6e0cd8302d170a44c2e19cb8fa42d3286aa691e6988b0fcfd008c7d671deb6138cd1587d0a44b7a43ec510f7e6b412c1eabfc3a452
6
+ metadata.gz: 8c7f9a91eafad1b8a20d245fcbe3e35879b0d429b32c218a496275953d1e56f433f84be7eaf80d64c1ecccf34307069469c9a0b896744b6a9f163fcf866dc8d2
7
+ data.tar.gz: e2d0b8aecc5e2f7c46d23ebc37f306e387fb9833ccfbccc6ece0984bb8dc34a2c7f0e57266e4d5c13f8fa6466d8af2425564e401fd45e6b16c3353fd91dd78ee
@@ -18,7 +18,9 @@
18
18
 
19
19
  ### ruby version ###
20
20
 
21
- era_jaはruby1.9.3とruby2.0.0でテストをしています。
21
+ era_jaはruby2.0.0とruby2.1.x, 2.2.xでテストをしています。(xは現時点での最後のバージョンです)
22
+
23
+ **NOTE** :: era_ja0.4.0からruby1.9.3をサポートしていません。
22
24
 
23
25
  ## Usage
24
26
 
@@ -35,27 +37,39 @@ EraJa#to_eraメソッドでの追加のフォーマット文字列は以下の
35
37
  ### Timeインスタンスを和暦に変換します。
36
38
 
37
39
  ```ruby
38
- require 'era_ja/time'
39
- Time.mktime(2012,4,29).to_era # => "H24.4.29"
40
- Time.mktime(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
41
- Time.mktime(2012,4,29).to_era("%O%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
40
+ require 'era_ja'
41
+ Time.mktime(2012,4,29).to_era # => "H24.4.29"
42
+ Time.mktime(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
43
+ Time.mktime(2012,4,29).to_era("%O%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
42
44
  ```
43
45
 
44
46
  ### Dateインタンスを和暦に変換します。
45
47
 
46
48
  ```ruby
47
- require 'era_ja/date'
48
- Date.new(2012,4,29).to_era # => "H24.4.29"
49
- Date.new(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
50
- Date.new(2012,4,29).to_era("%O%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
49
+ require 'era_ja
50
+ Date.new(2012,4,29).to_era # => "H24.4.29"
51
+ Date.new(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
52
+ Date.new(2012,4,29).to_era("%O%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
51
53
  ```
52
54
 
53
55
  ### 数字を漢数字に変換します。 ###
54
56
 
55
57
 
56
58
  ```ruby
57
- Time.mktime(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
58
- Date.new(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
59
+ Time.mktime(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
60
+ Date.new(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
61
+ ```
62
+
63
+ ### Era names ###
64
+
65
+ もしデフォルトの元号文字列以外の文字列でコンバートしたい場合、 `era_names` を使用することができます。
66
+
67
+ ```ruby
68
+ require 'era_ja'
69
+ Time.mktime(2012,4,29).to_era(era_names: { heisei: ['h', '平'] }) # => "h24.4.29"
70
+ Time.mktime(2012,4,29).to_era("%O%E年%m月%d日", era_names: { heisei: ['h', '平'] }) # => "平24年4月29日"
71
+ Time.mktime(2012,4,29).to_era("%O%JE年%Jm月%Jd日", era_names: { heisei: ['h', '平'] }) # => "平二十四年四月二十九日"
72
+ # same as Date
59
73
  ```
60
74
 
61
75
  ## Support
data/README.md CHANGED
@@ -18,7 +18,9 @@ Or install it yourself as:
18
18
 
19
19
  ### ruby version ###
20
20
 
21
- era_ja was run test on ruby 1.9.3 and ruby 2.0.0
21
+ era_ja was run test on ruby 2.0.0 and ruby 2.1.x and ruby 2.2.x .(x is latest version)
22
+
23
+ **NOTE** :: era_ja does not support ruby 1.9.3 since 0.4.0
22
24
 
23
25
  ## Usage
24
26
 
@@ -36,26 +38,38 @@ EraJa#to_era of extra format string is follow.
36
38
  ### Time instance to japanese era
37
39
 
38
40
  ```ruby
39
- require 'era_ja'
40
- Time.mktime(2012,4,29).to_era # => "H24.4.29"
41
- Time.mktime(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
42
- Time.mktime(2012,4,29).to_era("%O%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
41
+ require 'era_ja'
42
+ Time.mktime(2012,4,29).to_era # => "H24.4.29"
43
+ Time.mktime(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
44
+ Time.mktime(2012,4,29).to_era("%O%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
43
45
  ```
44
46
 
45
47
  ### Date instance to japanese era
46
48
 
47
49
  ```ruby
48
- require 'era_ja'
49
- Date.new(2012,4,29).to_era # => "H24.4.29"
50
- Date.new(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
51
- Date.new(2012,4,29).to_era("%O%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
50
+ require 'era_ja'
51
+ Date.new(2012,4,29).to_era # => "H24.4.29"
52
+ Date.new(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
53
+ Date.new(2012,4,29).to_era("%O%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
52
54
  ```
53
55
 
54
56
  ### Change number to kansuuzi ###
55
57
 
56
58
  ```ruby
57
- Time.mktime(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
58
- Date.new(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
59
+ Time.mktime(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
60
+ Date.new(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
61
+ ```
62
+
63
+ ### Era names ###
64
+
65
+ If you want to convert custom ara string, you can set `era_names` .
66
+
67
+ ```ruby
68
+ require 'era_ja'
69
+ Time.mktime(2012,4,29).to_era(era_names: { heisei: ['h', '平'] }) # => "h24.4.29"
70
+ Time.mktime(2012,4,29).to_era("%O%E年%m月%d日", era_names: { heisei: ['h', '平'] }) # => "平24年4月29日"
71
+ Time.mktime(2012,4,29).to_era("%O%JE年%Jm月%Jd日", era_names: { heisei: ['h', '平'] }) # => "平二十四年四月二十九日"
72
+ # same as Date
59
73
  ```
60
74
 
61
75
  ## Support
@@ -3,7 +3,12 @@ require 'date'
3
3
 
4
4
  module EraJa
5
5
  module Conversion
6
- ERA = [["M", "明治"], ["T", "大正"], ["S", "昭和"], ["H", "平成"]]
6
+ ERA_NAME_DEFAULTS = {
7
+ meiji: ["M", "明治"],
8
+ taisho: ["T", "大正"],
9
+ showa: ["S", "昭和"],
10
+ heisei: ["H", "平成"]
11
+ }
7
12
 
8
13
  # Convert to Japanese era.
9
14
  # @param [String] format_string
@@ -13,8 +18,13 @@ module EraJa
13
18
  # * %O - era(kanzi)
14
19
  # * %E - era year
15
20
  # * %J - kanzi number
21
+ # @param [Hash] era_names
22
+ # If you want to convert custom era string(ex `平`, `h`), you can set this argument.
23
+ # key is `:meiji' or `:taisho' or `:showa` or `:heisei`.
24
+ # value is ["alphabet era name"(ex `h`, `s`)(related to `%o`), "multibyte ara name"(ex `平`, `昭`)(related to `%O`)].
25
+ # this argument is same as one element of ERA_NAME_DEFAULTS.
16
26
  # @return [String]
17
- def to_era(format = "%o%E.%m.%d")
27
+ def to_era(format = "%o%E.%m.%d", era_names: ERA_NAME_DEFAULTS)
18
28
  @era_format = format.dup
19
29
  @era_format.gsub!(/%J/, "%J%")
20
30
  @era_format.sub!(/%o/i) { |m| m + ' ' }
@@ -24,28 +34,28 @@ module EraJa
24
34
  if self.to_time < ::Time.mktime(1868,9,8)
25
35
  raise "#to_era is expeted later in 1868,9,8"
26
36
  elsif self.to_time < ::Time.mktime(1912,7,30)
27
- str_time = era_year(year - 1867, 'M')
37
+ str_time = era_year(year - 1867, :meiji, era_names)
28
38
  elsif self.to_time < ::Time.mktime(1926,12,25)
29
- str_time = era_year(year - 1911, 'T')
39
+ str_time = era_year(year - 1911, :taisho, era_names)
30
40
  elsif self.to_time < ::Time.mktime(1989,1,8)
31
- str_time = era_year(year - 1925, 'S')
41
+ str_time = era_year(year - 1925, :showa, era_names)
32
42
  else
33
- str_time = era_year(year - 1988, 'H')
43
+ str_time = era_year(year - 1988, :heisei, era_names)
34
44
  end
35
45
  end
36
46
  str_time.gsub(/%J(\d+)/) { to_kanzi($1) }
37
47
  end
38
48
 
39
49
  private
40
- def era_year(year, era)
41
- strftime(@era_format).sub(/(%J)?%E /) { format_year(year, $1) }.sub(/%o /i) { format_era(era) }
50
+ def era_year(year, era, era_names)
51
+ strftime(@era_format).sub(/(%J)?%E /) { format_year(year, $1) }.sub(/%o /i) { format_era(era, era_names) }
42
52
  end
43
53
 
44
- def format_era(string)
54
+ def format_era(era, era_names)
45
55
  if @era_format =~ /%o/
46
- return "#{ERA.detect{ |i| i[0] == string}[0]}"
56
+ return era_names.fetch(era)[0]
47
57
  elsif @era_format =~ /%O/
48
- return "#{ERA.detect{ |i| i[0] == string}[1]}"
58
+ return era_names.fetch(era)[1]
49
59
  end
50
60
  end
51
61
 
@@ -1,3 +1,3 @@
1
1
  module EraJa
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -9,6 +9,25 @@ RSpec.shared_examples "should equal 'H24.04.29'" do
9
9
  end
10
10
  end
11
11
 
12
+ RSpec.shared_examples "should equal 'h24.04.29'" do
13
+ context "with era_names option" do
14
+ context "when era_names is correct" do
15
+ let(:era_names) { { heisei: ["h", "平成"] } }
16
+ it { expect(subject.to_era(era_names: era_names)).to eq "h24.04.29" }
17
+ end
18
+
19
+ context "when era_names have no heisei values" do
20
+ let(:era_names) { { showa: ["s", "昭和"] } }
21
+ it { expect { subject.to_era(era_names: era_names) }.to raise_error }
22
+ end
23
+ end
24
+
25
+ context "with '%o%E.%m.%d' and era_names option" do
26
+ let(:era_names) { { heisei: ["h", "平成"] } }
27
+ it { expect(subject.to_era("%o%E.%m.%d", era_names: era_names)).to eq "h24.04.29" }
28
+ end
29
+ end
30
+
12
31
  RSpec.shared_examples "should equal '平成24年04月29日'" do
13
32
  context "with '%O%E年%m月%d日'" do
14
33
  it { expect(subject.to_era("%O%E年%m月%d日")).to eq "平成24年04月29日" }
@@ -33,6 +52,22 @@ RSpec.shared_examples "should equal '平成240429'" do
33
52
  end
34
53
  end
35
54
 
55
+ RSpec.shared_examples "should equal '平240429'" do
56
+ context "with '%O%E%m%d' and era_names option" do
57
+ let(:format) { "%O%E%m%d" }
58
+
59
+ context "when era_names is correct" do
60
+ let(:era_names) { { heisei: ["H", "平"] } }
61
+ it { expect(subject.to_era(format, era_names: era_names)).to eq '平240429' }
62
+ end
63
+
64
+ context "when era_names have no heisei values" do
65
+ let(:era_names) { { showa: ["S", "昭"] } }
66
+ it { expect { subject.to_era(format, era_names: era_names) }.to raise_error }
67
+ end
68
+ end
69
+ end
70
+
36
71
  RSpec.shared_examples "should equal '平成二十四年四月二十九日'" do
37
72
  context "with '%O%JE年%Jm月%Jd日'" do
38
73
  it { expect(subject.to_era('%O%JE年%Jm月%Jd日')).to eq '平成二十四年四月二十九日' }
@@ -103,10 +138,12 @@ end
103
138
 
104
139
  RSpec.shared_examples "2012,4,29" do
105
140
  include_examples "should equal 'H24.04.29'"
141
+ include_examples "should equal 'h24.04.29'"
106
142
  include_examples "should equal '平成24年04月29日'"
107
143
  include_examples "should equal '24.04.29'"
108
144
  include_examples "should equal '2404'"
109
145
  include_examples "should equal '平成240429'"
146
+ include_examples "should equal '平240429'"
110
147
  include_examples "should equal '2012年04月29日'"
111
148
  include_examples "should equal '平成二十四年四月二十九日'"
112
149
  include_examples "should equal '二千十二年四月二十九日'"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: era_ja
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tomi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-25 00:00:00.000000000 Z
11
+ date: 2015-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake