date_values 0.1.1 → 0.1.3
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 +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +53 -1
- data/lib/date_values/month_day.rb +5 -0
- data/lib/date_values/rails/i18n_backend.rb +29 -0
- data/lib/date_values/rails.rb +9 -0
- data/lib/date_values/time_of_day.rb +4 -0
- data/lib/date_values/version.rb +1 -1
- data/lib/date_values/year_month.rb +4 -0
- data/sig/date_values.rbs +6 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 18c625b8766c99ee4e063b2d2b756f68702d7468301cf0e14cb61c29647bc97e
|
|
4
|
+
data.tar.gz: cc21e80e920a5590109a62894d362b541f5f7362835f7d5113980b20bc28842f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3ea359a67138a64040d1fc62a9288c3862665f636db9e1651697ec6527390e1e53cb002f87745df2189b8eba8944f104e127b620af032be0e92d5a5d707f77a2
|
|
7
|
+
data.tar.gz: f5a8b524c53566ee986a3eea6bf28d08e889a428d15b7ae48f04443d46108ee7b882391adc82bc597af10338ef071ec9ecb83dc6812718d67ef332c5065432eb
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.1.3] - 2026-03-20
|
|
4
|
+
|
|
5
|
+
- Fix ActiveRecord type registration — use `ActiveSupport.on_load(:active_record)` to register types regardless of load order
|
|
6
|
+
- Values now work directly in ActiveRecord queries (`Shop.where(billing_month: YearMonth.new(2026, 3))`)
|
|
7
|
+
|
|
8
|
+
## [0.1.2] - 2026-03-19
|
|
9
|
+
|
|
10
|
+
- Add `#strftime` to `YearMonth`, `MonthDay`, and `TimeOfDay`
|
|
11
|
+
- Add I18n backend extension for Rails `l` helper — looks up `year_month.formats`, `month_day.formats`, and `time_of_day.formats` in locale files
|
|
12
|
+
|
|
3
13
|
## [0.1.1] - 2026-03-19
|
|
4
14
|
|
|
5
15
|
- Add `.from` class methods for converting from `Date` / `Time` (`YearMonth.from(date)`, `MonthDay.from(date)`, `TimeOfDay.from(time)`)
|
data/README.md
CHANGED
|
@@ -43,8 +43,12 @@ md = MonthDay.new(3, 19)
|
|
|
43
43
|
md.to_s # => "--03-19"
|
|
44
44
|
md.to_date(2026) # => #<Date: 2026-03-19>
|
|
45
45
|
|
|
46
|
-
MonthDay.from(Date.today) # => MonthDay[--03-
|
|
46
|
+
MonthDay.from(Date.today) # => MonthDay[--03-20]
|
|
47
47
|
MonthDay.parse('--03-19') # => MonthDay[--03-19]
|
|
48
|
+
|
|
49
|
+
# Range membership
|
|
50
|
+
summer = MonthDay.new(6, 1)..MonthDay.new(8, 31)
|
|
51
|
+
summer.cover?(MonthDay.new(7, 15)) # => true
|
|
48
52
|
```
|
|
49
53
|
|
|
50
54
|
### TimeOfDay
|
|
@@ -57,6 +61,10 @@ TimeOfDay.new(14, 30, 45).to_s # => "14:30:45"
|
|
|
57
61
|
|
|
58
62
|
TimeOfDay.from(Time.now) # => TimeOfDay[14:30]
|
|
59
63
|
TimeOfDay.parse('14:30') # => TimeOfDay[14:30]
|
|
64
|
+
|
|
65
|
+
# Range membership
|
|
66
|
+
business_hours = TimeOfDay.new(9, 0)..TimeOfDay.new(17, 0)
|
|
67
|
+
business_hours.cover?(TimeOfDay.new(12, 30)) # => true
|
|
60
68
|
```
|
|
61
69
|
|
|
62
70
|
### Pattern Matching
|
|
@@ -94,6 +102,50 @@ class Shop < ApplicationRecord
|
|
|
94
102
|
end
|
|
95
103
|
```
|
|
96
104
|
|
|
105
|
+
Values are automatically serialized in queries:
|
|
106
|
+
|
|
107
|
+
```ruby
|
|
108
|
+
Shop.where(billing_month: YearMonth.new(2026, 3))
|
|
109
|
+
# SELECT * FROM shops WHERE billing_month = '2026-03'
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### I18n / `l` Helper
|
|
113
|
+
|
|
114
|
+
All classes implement `#strftime`, and the Rails integration extends `I18n.l` to support them. Define formats in your locale files:
|
|
115
|
+
|
|
116
|
+
```yaml
|
|
117
|
+
# config/locales/en.yml
|
|
118
|
+
en:
|
|
119
|
+
year_month:
|
|
120
|
+
formats:
|
|
121
|
+
default: '%B %Y'
|
|
122
|
+
month_day:
|
|
123
|
+
formats:
|
|
124
|
+
default: '%B %-d'
|
|
125
|
+
time_of_day:
|
|
126
|
+
formats:
|
|
127
|
+
default: '%-I:%M %p'
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```yaml
|
|
131
|
+
# config/locales/ja.yml
|
|
132
|
+
ja:
|
|
133
|
+
year_month:
|
|
134
|
+
formats:
|
|
135
|
+
default: '%Y年%-m月'
|
|
136
|
+
month_day:
|
|
137
|
+
formats:
|
|
138
|
+
default: '%-m月%-d日'
|
|
139
|
+
time_of_day:
|
|
140
|
+
formats:
|
|
141
|
+
default: '%-H時%M分'
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
I18n.l YearMonth.new(2026, 3), locale: :en # => "March 2026"
|
|
146
|
+
I18n.l YearMonth.new(2026, 3), locale: :ja # => "2026年3月"
|
|
147
|
+
```
|
|
148
|
+
|
|
97
149
|
## License
|
|
98
150
|
|
|
99
151
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -30,6 +30,11 @@ module DateValues
|
|
|
30
30
|
[month, day] <=> [other.month, other.day]
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
def strftime(format)
|
|
34
|
+
# 2000 is a leap year, so Feb 29 works
|
|
35
|
+
Date.new(2000, month, day).strftime(format)
|
|
36
|
+
end
|
|
37
|
+
|
|
33
38
|
def to_date(year)
|
|
34
39
|
Date.new(year, month, day)
|
|
35
40
|
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DateValues
|
|
4
|
+
module Rails
|
|
5
|
+
module I18nBackend
|
|
6
|
+
TYPES = {
|
|
7
|
+
DateValues::YearMonth => :year_month,
|
|
8
|
+
DateValues::MonthDay => :month_day,
|
|
9
|
+
DateValues::TimeOfDay => :time_of_day
|
|
10
|
+
}.freeze
|
|
11
|
+
|
|
12
|
+
def localize(locale, object, format = :default, options = EMPTY_HASH)
|
|
13
|
+
type = TYPES[object.class]
|
|
14
|
+
return super unless type
|
|
15
|
+
|
|
16
|
+
format_key = format.is_a?(Symbol) ? format : nil
|
|
17
|
+
|
|
18
|
+
if format_key
|
|
19
|
+
entry = I18n.t("#{type}.formats.#{format_key}", locale: locale, default: nil)
|
|
20
|
+
raise I18n::MissingTranslationData.new(locale, "#{type}.formats.#{format_key}") unless entry
|
|
21
|
+
|
|
22
|
+
format = entry
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
object.strftime(format)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/date_values/rails.rb
CHANGED
|
@@ -6,7 +6,16 @@ require 'active_model/type'
|
|
|
6
6
|
require_relative 'rails/year_month_type'
|
|
7
7
|
require_relative 'rails/month_day_type'
|
|
8
8
|
require_relative 'rails/time_of_day_type'
|
|
9
|
+
require_relative 'rails/i18n_backend'
|
|
9
10
|
|
|
10
11
|
ActiveModel::Type.register(:year_month, DateValues::Rails::YearMonthType)
|
|
11
12
|
ActiveModel::Type.register(:month_day, DateValues::Rails::MonthDayType)
|
|
12
13
|
ActiveModel::Type.register(:time_of_day, DateValues::Rails::TimeOfDayType)
|
|
14
|
+
|
|
15
|
+
ActiveSupport.on_load(:active_record) do
|
|
16
|
+
ActiveRecord::Type.register(:year_month, DateValues::Rails::YearMonthType)
|
|
17
|
+
ActiveRecord::Type.register(:month_day, DateValues::Rails::MonthDayType)
|
|
18
|
+
ActiveRecord::Type.register(:time_of_day, DateValues::Rails::TimeOfDayType)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
I18n::Backend::Base.prepend(DateValues::Rails::I18nBackend)
|
|
@@ -29,6 +29,10 @@ module DateValues
|
|
|
29
29
|
[hour, minute, second] <=> [other.hour, other.minute, other.second]
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
def strftime(format)
|
|
33
|
+
Time.new(2000, 1, 1, hour, minute, second).strftime(format)
|
|
34
|
+
end
|
|
35
|
+
|
|
32
36
|
def to_s
|
|
33
37
|
if second.zero?
|
|
34
38
|
format('%02d:%02d', hour, minute)
|
data/lib/date_values/version.rb
CHANGED
data/sig/date_values.rbs
CHANGED
|
@@ -23,6 +23,8 @@ module DateValues
|
|
|
23
23
|
|
|
24
24
|
def succ: () -> YearMonth
|
|
25
25
|
|
|
26
|
+
def strftime: (String format) -> String
|
|
27
|
+
|
|
26
28
|
def to_date: () -> Date
|
|
27
29
|
|
|
28
30
|
def to_s: () -> String
|
|
@@ -45,6 +47,8 @@ module DateValues
|
|
|
45
47
|
def <=>: (MonthDay other) -> Integer
|
|
46
48
|
| (untyped other) -> nil
|
|
47
49
|
|
|
50
|
+
def strftime: (String format) -> String
|
|
51
|
+
|
|
48
52
|
def to_date: (Integer year) -> Date
|
|
49
53
|
|
|
50
54
|
def to_s: () -> String
|
|
@@ -72,6 +76,8 @@ module DateValues
|
|
|
72
76
|
def <=>: (TimeOfDay other) -> Integer
|
|
73
77
|
| (untyped other) -> nil
|
|
74
78
|
|
|
79
|
+
def strftime: (String format) -> String
|
|
80
|
+
|
|
75
81
|
def to_s: () -> String
|
|
76
82
|
|
|
77
83
|
def inspect: () -> String
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: date_values
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Keita Urashima
|
|
@@ -22,6 +22,7 @@ files:
|
|
|
22
22
|
- lib/date_values.rb
|
|
23
23
|
- lib/date_values/month_day.rb
|
|
24
24
|
- lib/date_values/rails.rb
|
|
25
|
+
- lib/date_values/rails/i18n_backend.rb
|
|
25
26
|
- lib/date_values/rails/month_day_type.rb
|
|
26
27
|
- lib/date_values/rails/time_of_day_type.rb
|
|
27
28
|
- lib/date_values/rails/year_month_type.rb
|