japanese_calendar 0.4.0 → 0.4.1

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: d6bd70c70fd16b249bd21b5d7859ead4d393887170bbd28734cc58df5b6397dd
4
- data.tar.gz: d1c54d42d05800862841d3f22c1b4be0ddb2f1282dfd7222abcfe36cbdbcf5ff
3
+ metadata.gz: bdf782fc21c829f1030ccefee7ce182481f7a3e6754c07b2c4a51a079f12f41c
4
+ data.tar.gz: 25712c4bf33b306c450df16a235bfb9879dfb7f9d8dcd0a5b4cfee12093935c5
5
5
  SHA512:
6
- metadata.gz: 736514a1cbce60600b39dc1a96aeb816b2c7f31d8297f720ca903d851eca7454f2c4785e5a514d189a7ada6f515c9f5648ed72c305b09e07771f4efbcde7f0c7
7
- data.tar.gz: d3b12a19c5a160d85483410f79fc9a7974ebacf64e2225c0aa28f7c56b0293769fe4c6b7f63f69a9629bb1863dba04c3178fda888d88a6c4b0f17988cd7307d7
6
+ metadata.gz: 7eb284628002807ea4170ed56a644ba45b606cd84eb9a2102aed165a57dcae533f9d625c4152538f285d8920206cef657d2f5d156a95db50390511e88741e4f5
7
+ data.tar.gz: 9b522edc955386a0b02dcfff9e586d9503cf98ba37ebe4e9bf86d044f1fd46ea223abcba5a4082f3b36f9383c1544dbca382b94f24a9965f72f19b45dc27a5b9
data/.travis.yml CHANGED
@@ -1,16 +1,21 @@
1
- sudo: false
1
+ env:
2
+ global:
3
+ - secure: lSqmXKeSjlvn9vtmPhEFZCHdeVjyD0toqByvIJVH8/M7KZP9EBkG6p0l/QsGQM+Tid7EjLeDrKkZE7LytvhyS9/0lESpXl20RtQWqOhd+03qdTKRO5SgL6BwonV7dv3VfGUpkwuCOUSJpOPN/xHu1QidyHZuQ0GpKP4gIZkNIten1Rm5hxJ1MxHzg7pu6F6XkyHeamOcoUeqE1eDRRz/g0+Vc7EzG+jAzd3gNu3fHW9LZ8qHh0f7M86U/nuIbQmldvhwbXK3HZk4DLbBg7nVj74yhskNXAWI61dirzHRJkETxaH8ZK04jkQj5yNdD5K+mprh3hj0gSVn2cWqljd8bWe7uutQ7yeye9c3wza0gj5qVekZwLemwFvlIL0JicPE9fjhmOfliYIA/y6ruvTL+JIxCToNDiLlB0XY4KVl1c+mOmSopmdUrx22OA4n15pdOnXMNs7VPTuKu1JgvZlWY2Pkh3QIT5+5iTqgkt0b0OjX0lHnUZZW26v8Rt4eeAyObGsbtxEWUt2tLvV7szVJw8KY6kYCb8dDvgedV3xrzHMkoh3O+UcZj42UnKXOLDL8kJO0RS1j5AbpURFnTlcqtAKppkMigboCJtWh/KJNajWvBeBs2PKZmE7L7PZVLU0Igw03zLi+LpP0FXN2SFqcecO09Ozl7OcjWH1QTZmCjig=
2
4
  language: ruby
3
5
  rvm:
4
6
  - 2.3.8
5
7
  - 2.4.6
6
8
  - 2.5.5
7
9
  - 2.6.2
8
- before_install: gem install bundler -v 2.0.1
9
- addons:
10
- code_climate:
11
- repo_token: c542293583b86ca95015675c334c9fea63c14825709f7c9eb4c72a0c8cfc71b4
12
- after_success:
13
- - bundle exec codeclimate-test-reporter
10
+ before_install:
11
+ - gem update --system
12
+ - gem install bundler
13
+ before_script:
14
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
15
+ - chmod +x ./cc-test-reporter
16
+ - ./cc-test-reporter before-build
17
+ after_script:
18
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
14
19
  notifications:
15
20
  slack:
16
21
  secure: K+I4otkeX0yMPQing/KbcB5A/FKWo4aNFeRl7PuQ9hB1K7f2rRr0s2uvkfFsXYYFJrxWNhNnPoiiJNAmOBzmFljcIAGm5k5qPfmjFeVTFsv1lEZjfAa2UbCKgCGi79P48BvZ4zXO1I/uZsNUBkVPAfxZSq+aG3tudPXmXOEeTjMMNE6czYr49qrLsQPh7ezx2431Zm+qMqzSoqW7aOzZjgZvuc8IllrrBWO2cstD/8LkLrYVrY+MJbxM8kT6ZXbCXDisu/jSN/ChIVCFfEmYjavdkCrIyMN3gqFzwBnjFtco9YlPRqyA4DOWFzXmIzOSh77rEskF+noaPS2dUMt6g/32GQbwRR+x/YvJkqizyOwLky2H7aVw2pCgZnIGIshXxZPIUwxriML/O0MpkKRDnFcc8kzeL1IOqQauxRxq9woSZ68pFyVP4dc5JT1cuMGJK0/vWCUKnPSyV9wjUNJQXHV907Fxh+aW398JpPUEeAsjMO8erwWd0w8q4qYDEamoc7+pjz+HEJJjlVYseDR3CN3bpR3vcizpIPHTUlTTRMNY+KTbPRMam+9ZLJzv+SFVZ58hjJQK8aIW0tc51bhP2cuzNfmX2YcYEJ6DQvCWdnj7V6gKOHCB/BebfIUmdvRGyew2mrZSUfXPTAAlnwiRhf5IDzIulR0GusgQiqa5tmU=
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  source 'https://rubygems.org'
3
4
 
4
5
  # Specify your gem's dependencies in japanese_calendar.gemspec
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Japanese Calendar [![Gem Version](https://badge.fury.io/rb/japanese_calendar.svg)](https://badge.fury.io/rb/japanese_calendar) [![Build Status](https://travis-ci.org/RyoYamamotoJP/japanese_calendar.svg?branch=master)](https://travis-ci.org/RyoYamamotoJP/japanese_calendar) [![Code Climate](https://codeclimate.com/github/RyoYamamotoJP/japanese_calendar/badges/gpa.svg)](https://codeclimate.com/github/RyoYamamotoJP/japanese_calendar) [![Test Coverage](https://codeclimate.com/github/RyoYamamotoJP/japanese_calendar/badges/coverage.svg)](https://codeclimate.com/github/RyoYamamotoJP/japanese_calendar/coverage)
1
+ # Japanese Calendar [![Gem Version](https://badge.fury.io/rb/japanese_calendar.svg)](https://badge.fury.io/rb/japanese_calendar) [![Build Status](https://travis-ci.org/RyoYamamotoJP/japanese_calendar.svg?branch=master)](https://travis-ci.org/RyoYamamotoJP/japanese_calendar) [![Maintainability](https://api.codeclimate.com/v1/badges/661b3d2765caae7906c6/maintainability)](https://codeclimate.com/github/RyoYamamotoJP/japanese_calendar/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/661b3d2765caae7906c6/test_coverage)](https://codeclimate.com/github/RyoYamamotoJP/japanese_calendar/test_coverage)
2
2
 
3
3
  Japanese calendar utility for Ruby.
4
4
 
data/Rakefile CHANGED
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
- require "bundler/gem_tasks"
3
- require "rspec/core/rake_task"
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
4
5
 
5
6
  RSpec::Core::RakeTask.new(:spec)
6
7
 
7
- task :default => :spec
8
+ task default: :spec
data/bin/console CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require "bundler/setup"
5
- require "japanese_calendar"
4
+ require 'bundler/setup'
5
+ require 'japanese_calendar'
6
6
 
7
7
  # You can add fixtures and/or initialization code here to make experimenting
8
8
  # with your gem easier. You can also use a different console, if you like.
@@ -11,5 +11,5 @@ require "japanese_calendar"
11
11
  # require "pry"
12
12
  # Pry.start
13
13
 
14
- require "irb"
14
+ require 'irb'
15
15
  IRB.start
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require "japanese_calendar"
4
+ require 'japanese_calendar'
@@ -1,32 +1,31 @@
1
- # coding: utf-8
2
1
  # frozen_string_literal: true
3
- lib = File.expand_path('../lib', __FILE__)
2
+
3
+ lib = File.expand_path('lib')
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require 'japanese_calendar/version'
6
6
 
7
7
  Gem::Specification.new do |spec|
8
- spec.name = "japanese_calendar"
8
+ spec.name = 'japanese_calendar'
9
9
  spec.version = JapaneseCalendar::VERSION
10
- spec.authors = ["Ryo Yamamoto"]
11
- spec.email = ["email@ryoyamamoto.jp"]
10
+ spec.authors = ['Ryo Yamamoto']
11
+ spec.email = ['email@ryoyamamoto.jp']
12
12
 
13
- spec.summary = "Japanese calendar utility for Ruby"
14
- spec.homepage = "https://github.com/RyoYamamotoJP/japanese_calendar"
15
- spec.license = "MIT"
13
+ spec.summary = 'Japanese calendar utility for Ruby'
14
+ spec.homepage = 'https://github.com/RyoYamamotoJP/japanese_calendar'
15
+ spec.license = 'MIT'
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
18
  f.match(%r{^(test|spec|features)/})
19
19
  end
20
- spec.bindir = "exe"
20
+ spec.bindir = 'exe'
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
- spec.require_paths = ["lib"]
22
+ spec.require_paths = ['lib']
23
23
 
24
- spec.required_ruby_version = ">= 2.3"
24
+ spec.required_ruby_version = '>= 2.3'
25
25
 
26
- spec.add_development_dependency "bundler", "~> 2.0"
27
- spec.add_development_dependency "rake", "~> 12.0"
28
- spec.add_development_dependency "rspec", "~> 3.0"
29
- spec.add_development_dependency "rubocop"
30
- spec.add_development_dependency "simplecov"
31
- spec.add_development_dependency "codeclimate-test-reporter", "~> 1.0.0"
26
+ spec.add_development_dependency 'bundler', '~> 2.0'
27
+ spec.add_development_dependency 'rake', '~> 12.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'rubocop'
30
+ spec.add_development_dependency 'simplecov'
32
31
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
- require "japanese_calendar/version"
3
- require "japanese_calendar/core_ext/date_time"
4
- require "japanese_calendar/core_ext/date"
5
- require "japanese_calendar/core_ext/time"
2
+
3
+ require 'japanese_calendar/version'
4
+ require 'japanese_calendar/core_ext/date_time'
5
+ require 'japanese_calendar/core_ext/date'
6
+ require 'japanese_calendar/core_ext/time'
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
- require "date"
3
- require "japanese_calendar/era"
4
- require "japanese_calendar/weekday"
2
+
3
+ require 'date'
4
+ require 'japanese_calendar/era'
5
+ require 'japanese_calendar/weekday'
5
6
 
6
7
  class Date
7
8
  prepend JapaneseCalendar::Era
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
- require "date"
3
- require "japanese_calendar/era"
4
- require "japanese_calendar/weekday"
2
+
3
+ require 'date'
4
+ require 'japanese_calendar/era'
5
+ require 'japanese_calendar/weekday'
5
6
 
6
7
  class DateTime
7
8
  prepend JapaneseCalendar::Era
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
- require "japanese_calendar/era"
3
- require "japanese_calendar/weekday"
2
+
3
+ require 'japanese_calendar/era'
4
+ require 'japanese_calendar/weekday'
4
5
 
5
6
  class Time
6
7
  prepend JapaneseCalendar::Era
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JapaneseCalendar
4
+ module Deprecator
5
+ private
6
+
7
+ def deprecate(directive, message)
8
+ warn "#{directive} is deprecated. #{message}"
9
+ end
10
+ end
11
+ end
@@ -1,8 +1,51 @@
1
1
  # frozen_string_literal: true
2
+
3
+ require 'japanese_calendar/era/deprecator'
4
+
2
5
  module JapaneseCalendar
3
6
  module Era
7
+ prepend Era::Deprecator
8
+
9
+ Period = Struct.new(:beginning_of_period, :kanji_name, :romaji_name)
10
+
11
+ MEIJI_6 = Date.new(1873, 1, 1)
12
+
13
+ PERIODS = [
14
+ Period.new(Date.new(2019, 5, 1), '令和', 'Reiwa').freeze,
15
+ Period.new(Date.new(1989, 1, 8), '平成', 'Heisei').freeze,
16
+ Period.new(Date.new(1926, 12, 25), '昭和', 'Showa').freeze,
17
+ Period.new(Date.new(1912, 7, 30), '大正', 'Taisho').freeze,
18
+ Period.new(Date.new(1868, 1, 25), '明治', 'Meiji').freeze
19
+ ].freeze
20
+
21
+ private_constant :Period, :MEIJI_6, :PERIODS
22
+
23
+ # Formats time according to the directives in the given format string.
24
+ #
25
+ # date_of_birth = Time.new(1978, 7, 19)
26
+ #
27
+ # date_of_birth.strftime("%JN") # => "昭和"
28
+ # date_of_birth.strftime("%JR") # => "Showa"
29
+ # date_of_birth.strftime("%^JR") # => "SHOWA"
30
+ # date_of_birth.strftime("%Jr") # => "S"
31
+ # date_of_birth.strftime("%Jy") # => "53"
32
+ #
33
+ # date_of_birth.strftime("%JN%-Jy年") # => "昭和53年"
34
+ #
35
+ # Raises an error when the Japanese year cannot be found.
36
+ #
37
+ # Time.new(1872, 12, 31).strftime("%JN%-Jy年") # => RuntimeError
38
+ def strftime(format)
39
+ string = format.gsub(era_pattern, era_conversion)
40
+ super(string)
41
+ end
42
+
4
43
  # Returns the Japanese era name (nengo) since 1 January 1873 (Meiji 6).
5
44
  #
45
+ # reiwa = Time.new(2019, 5, 1) # => 2019-05-01 00:00:00 +0900
46
+ # reiwa.era_name # => "令和"
47
+ # reiwa.era_name(:romaji) # => "Reiwa"
48
+ #
6
49
  # heisei = Time.new(1989, 1, 8) # => 1989-01-08 00:00:00 +0900
7
50
  # heisei.era_name # => "平成"
8
51
  # heisei.era_name(:romaji) # => "Heisei"
@@ -23,8 +66,8 @@ module JapaneseCalendar
23
66
  #
24
67
  # Time.new(1872, 12, 31).era_name # => RuntimeError
25
68
  def era_name(character = :kanji)
26
- unless %i(kanji romaji).include?(character)
27
- raise ArgumentError, "invalid character"
69
+ unless %i[kanji romaji].include?(character)
70
+ raise ArgumentError, 'invalid character'
28
71
  end
29
72
 
30
73
  current_era.send("#{character}_name")
@@ -32,7 +75,8 @@ module JapaneseCalendar
32
75
 
33
76
  # Returns the Japanese year since 1 January 1873 (Meiji 6).
34
77
  #
35
- # Time.new(2016, 12, 11).era_year # => 28
78
+ # Time.new(2019, 5, 1).era_year # => 1
79
+ # Time.new(2019, 4, 30).era_year # => 31
36
80
  # Time.new(1989, 1, 7).era_year # => 64
37
81
  # Time.new(1926, 12, 24).era_year # => 15
38
82
  # Time.new(1912, 7, 29).era_year # => 45
@@ -44,89 +88,65 @@ module JapaneseCalendar
44
88
  year - current_era.beginning_of_period.year + 1
45
89
  end
46
90
 
47
- # Formats time according to the directives in the given format string.
48
- #
49
- # date_of_birth = Time.new(1978, 7, 19)
50
- #
51
- # date_of_birth.strftime("%JN") # => "昭和"
52
- # date_of_birth.strftime("%JR") # => "Showa"
53
- # date_of_birth.strftime("%^JR") # => "SHOWA"
54
- # date_of_birth.strftime("%Jr") # => "S"
55
- # date_of_birth.strftime("%Jy") # => "53"
56
- #
57
- # date_of_birth.strftime("%JN%-Jy年") # => "昭和53年"
58
- #
59
- # Raises an error when the Japanese year cannot be found.
60
- #
61
- # Time.new(1872, 12, 31).strftime("%JN%-Jy年") # => RuntimeError
62
- def strftime(format)
63
- string = format.dup
64
- string.gsub!(/%JN/, era_name)
65
- string.gsub!(/%JR/, era_name(:romaji))
66
- string.gsub!(/%\^JR/, era_name(:romaji).upcase)
67
- string.gsub!(/%Jr/, era_name(:romaji)[0])
68
- string.gsub!(/%Jy/, "%02d" % era_year)
69
- string.gsub!(/%-Jy/, "%d" % era_year)
70
- string.gsub!(/%_Jy/, "%2d" % era_year)
71
- deprecated_japanese_calendar_era_name_strftime(string)
72
- deprecated_japanese_calendar_era_year_strftime(string)
73
- super(string)
91
+ private
92
+
93
+ def current_era
94
+ error_proc = proc { raise "#{self.class.name.downcase} out of range" }
95
+ error_proc.call if self.to_date < MEIJI_6
96
+ PERIODS.find(error_proc) do |period|
97
+ period.beginning_of_period <= self.to_date
98
+ end
74
99
  end
75
100
 
76
- private
101
+ def era_kanji_name
102
+ era_name(:kanji)
103
+ end
77
104
 
78
- Period = Struct.new(:beginning_of_period, :kanji_name, :romaji_name)
105
+ def era_romaji_name
106
+ era_name(:romaji)
107
+ end
79
108
 
80
- PERIODS = [
81
- Period.new(Date.new(2019, 5, 1), "令和", "Reiwa").freeze,
82
- Period.new(Date.new(1989, 1, 8), "平成", "Heisei").freeze,
83
- Period.new(Date.new(1926, 12, 25), "昭和", "Showa" ).freeze,
84
- Period.new(Date.new(1912, 7, 30), "大正", "Taisho").freeze,
85
- Period.new(Date.new(1868, 1, 25), "明治", "Meiji" ).freeze
86
- ].freeze
109
+ def era_romaji_upcase_name
110
+ era_romaji_name.upcase
111
+ end
87
112
 
88
- MEIJI_6 = Date.new(1873, 1, 1)
113
+ def era_romaji_abbreviation
114
+ era_romaji_name[0]
115
+ end
89
116
 
90
- def current_era
91
- error_proc = proc { raise "#{self.class.name.downcase} out of range" }
92
- error_proc.call if self.to_date < MEIJI_6
93
- PERIODS.find(error_proc) do |period|
94
- period.beginning_of_period <= self.to_date
95
- end
96
- end
117
+ def era_year_string
118
+ '%-d' % era_year
119
+ end
97
120
 
98
- def deprecated_japanese_calendar_era_name_strftime(string)
99
- if string =~ /%K/
100
- warn "%K is deprecated. Please use %JN instead."
101
- string.gsub!(/%K/, era_name)
102
- end
103
- if string =~ /%O/
104
- warn "%O is deprecated. Please use %JR instead."
105
- string.gsub!(/%O/, era_name(:romaji))
106
- end
107
- if string =~ /%\^O/
108
- warn "%^O is deprecated. Please use %^JR instead."
109
- string.gsub!(/%\^O/, era_name(:romaji).upcase)
110
- end
111
- if string =~ /%o/
112
- warn "%o is deprecated. Please use %Jr instead."
113
- string.gsub!(/%o/, era_name(:romaji)[0])
114
- end
115
- end
121
+ def era_year_zero_padded_string
122
+ '%02d' % era_year
123
+ end
116
124
 
117
- def deprecated_japanese_calendar_era_year_strftime(string)
118
- if string =~ /%J/
119
- warn "%J is deprecated. Please use %Jy instead."
120
- string.gsub!(/%J/, "%02d" % era_year)
121
- end
122
- if string =~ /%-J/
123
- warn "%-J is deprecated. Please use %-Jy instead."
124
- string.gsub!(/%-J/, "%d" % era_year)
125
- end
126
- if string =~ /%_J/
127
- warn "%_J is deprecated. Please use %_Jy instead."
128
- string.gsub!(/%_J/, "%2d" % era_year)
129
- end
130
- end
125
+ def era_year_blank_padded_string
126
+ '%2d' % era_year
127
+ end
128
+
129
+ def era_conversion
130
+ {
131
+ '%JN' => era_kanji_name,
132
+ '%JR' => era_romaji_name,
133
+ '%^JR' => era_romaji_upcase_name,
134
+ '%Jr' => era_romaji_abbreviation,
135
+ '%Jy' => era_year_zero_padded_string,
136
+ '%-Jy' => era_year_string,
137
+ '%_Jy' => era_year_blank_padded_string,
138
+ '%K' => era_kanji_name,
139
+ '%O' => era_romaji_name,
140
+ '%^O' => era_romaji_upcase_name,
141
+ '%o' => era_romaji_abbreviation,
142
+ '%J' => era_year_zero_padded_string,
143
+ '%-J' => era_year_string,
144
+ '%_J' => era_year_blank_padded_string
145
+ }
146
+ end
147
+
148
+ def era_pattern
149
+ Regexp.union(era_conversion.keys)
150
+ end
131
151
  end
132
152
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'japanese_calendar/deprecator'
4
+
5
+ module JapaneseCalendar
6
+ module Era
7
+ module Deprecator
8
+ include JapaneseCalendar::Deprecator
9
+
10
+ DEPRECATIONS = {
11
+ '%K' => 'Please use %JN instead.',
12
+ '%O' => 'Please use %JR instead.',
13
+ '%^O' => 'Please use %^JR instead.',
14
+ '%o' => 'Please use %Jr instead.',
15
+ '%J' => 'Please use %Jy instead.',
16
+ '%-J' => 'Please use %-Jy instead.',
17
+ '%_J' => 'Please use %_Jy instead.'
18
+ }.freeze
19
+
20
+ private_constant :DEPRECATIONS
21
+
22
+ def strftime(format)
23
+ deprecations = collect_era_deprecations(format)
24
+ deprecations.each { |deprecation| deprecate(*deprecation) }
25
+ super(format)
26
+ end
27
+
28
+ private
29
+
30
+ def collect_era_deprecations(format)
31
+ deprecation_pattern = Regexp.union(DEPRECATIONS.keys)
32
+ deprecated_directives = format.scan(deprecation_pattern).uniq
33
+ DEPRECATIONS.select do |directive, _|
34
+ deprecated_directives.include?(directive)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module JapaneseCalendar
3
- VERSION = "0.4.0"
4
+ VERSION = '0.4.1'
4
5
  end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
+
3
+ require 'japanese_calendar/weekday/deprecator'
4
+
2
5
  module JapaneseCalendar
3
6
  module Weekday
4
- NAMES = %w(日曜日 月曜日 火曜日 水曜日 木曜日 金曜日 土曜日).freeze
7
+ prepend Weekday::Deprecator
5
8
 
6
9
  # Formats time according to the directives in the given format string.
7
10
  #
@@ -10,26 +13,33 @@ module JapaneseCalendar
10
13
  # date_of_birth.strftime("%JA") # => "水曜日"
11
14
  # date_of_birth.strftime("%Ja") # => "水"
12
15
  #
13
- # date_of_birth.strftime("%-Y年%-m月%-d日(%q)") # => "1978年7月19日(水)"
16
+ # date_of_birth.strftime("%-Y年%-m月%-d日(%Ja)") # => "1978年7月19日(水)"
14
17
  def strftime(format)
15
- string = format.dup
16
- string.gsub!(/%JA/, NAMES[wday])
17
- string.gsub!(/%Ja/, NAMES[wday][0])
18
- deprecated_japanese_calendar_weekday_strftime(string)
18
+ string = format.gsub(weekday_pattern, weekday_conversion)
19
19
  super(string)
20
20
  end
21
21
 
22
22
  private
23
23
 
24
- def deprecated_japanese_calendar_weekday_strftime(string)
25
- if string =~ /%Q/
26
- warn "%Q is deprecated. Please use %JA instead."
27
- string.gsub!(/%Q/, NAMES[wday])
28
- end
29
- if string =~ /%q/
30
- warn "%q is deprecated. Please use %Ja instead."
31
- string.gsub!(/%q/, NAMES[wday][0])
32
- end
33
- end
24
+ def weekday_name
25
+ %w[日曜日 月曜日 火曜日 水曜日 木曜日 金曜日 土曜日][wday]
26
+ end
27
+
28
+ def weekday_abbreviation
29
+ weekday_name[0]
30
+ end
31
+
32
+ def weekday_conversion
33
+ {
34
+ '%JA' => weekday_name,
35
+ '%Ja' => weekday_abbreviation,
36
+ '%Q' => weekday_name,
37
+ '%q' => weekday_abbreviation
38
+ }
39
+ end
40
+
41
+ def weekday_pattern
42
+ Regexp.union(weekday_conversion.keys)
43
+ end
34
44
  end
35
45
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'japanese_calendar/deprecator'
4
+
5
+ module JapaneseCalendar
6
+ module Weekday
7
+ module Deprecator
8
+ include JapaneseCalendar::Deprecator
9
+
10
+ DEPRECATIONS = {
11
+ '%Q' => 'Please use %JA instead.',
12
+ '%q' => 'Please use %Ja instead.'
13
+ }.freeze
14
+
15
+ private_constant :DEPRECATIONS
16
+
17
+ def strftime(format)
18
+ deprecations = collect_weekday_deprecations(format)
19
+ deprecations.each { |deprecation| deprecate(*deprecation) }
20
+ super(format)
21
+ end
22
+
23
+ private
24
+
25
+ def collect_weekday_deprecations(format)
26
+ deprecation_pattern = Regexp.union(DEPRECATIONS.keys)
27
+ deprecated_directives = format.scan(deprecation_pattern).uniq
28
+ DEPRECATIONS.select do |directive, _|
29
+ deprecated_directives.include?(directive)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: japanese_calendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Yamamoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-01 00:00:00.000000000 Z
11
+ date: 2019-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: codeclimate-test-reporter
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: 1.0.0
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: 1.0.0
97
83
  description:
98
84
  email:
99
85
  - email@ryoyamamoto.jp
@@ -120,9 +106,12 @@ files:
120
106
  - lib/japanese_calendar/core_ext/date.rb
121
107
  - lib/japanese_calendar/core_ext/date_time.rb
122
108
  - lib/japanese_calendar/core_ext/time.rb
109
+ - lib/japanese_calendar/deprecator.rb
123
110
  - lib/japanese_calendar/era.rb
111
+ - lib/japanese_calendar/era/deprecator.rb
124
112
  - lib/japanese_calendar/version.rb
125
113
  - lib/japanese_calendar/weekday.rb
114
+ - lib/japanese_calendar/weekday/deprecator.rb
126
115
  homepage: https://github.com/RyoYamamotoJP/japanese_calendar
127
116
  licenses:
128
117
  - MIT