date_values 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9128f65130329b58d9b750e411178c545086d0b46121ebe8a502fd3bb2774d9f
4
- data.tar.gz: 39c349f3f8edb108890f1c3b267a75e3e16ef59f846fc5bebdc5198f38a89f31
3
+ metadata.gz: 8ddef0176920084031675a2e93da2007646c642f23d0238ff4c54848f4ae0691
4
+ data.tar.gz: ecfbf84c37563d8b7ec9c592a625313aa95e7c0de0bb13d69b05ca8c96349389
5
5
  SHA512:
6
- metadata.gz: 30ceca8989f62e0fa958b02858ccefef45cff6d637e77f0fa7f4f3e87c6be562fb5d7c444873ebedb0f0ccf665f2662806ff7a5a0103459e47cce86e7753988e
7
- data.tar.gz: d0c2a75cb71ae66715f1cf357602f948910d518d3d9901a57672f7457626e500fe6a91ebd18cb0d56738aeb6c2c66c74797043b92576f154d656fe4c138b9eae
6
+ metadata.gz: e1f49621fe9037b5a5dddaea83c267bdcf7493dbf15b87a6ddb6e93b131843799836aa0608cb9f83b8759453265cd1ec747cbf01e3c6f518b7cf9517c3e832bd
7
+ data.tar.gz: 25ce52fd8bfd18544c62704562704eeba340cf44cfbb57d408c38d3d19b3771034026803a80f1d2660894541cca4d8c213a6bd462bbf62db2184ed4bde6106be
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.1] - 2026-03-19
4
+
5
+ - Add `.from` class methods for converting from `Date` / `Time` (`YearMonth.from(date)`, `MonthDay.from(date)`, `TimeOfDay.from(time)`)
6
+
3
7
  ## [0.1.0] - 2026-03-19
4
8
 
5
9
  - `DateValues::YearMonth` — year-month value object with arithmetic (`+`, `-`), `Range` support, and `Date` conversion
data/README.md CHANGED
@@ -24,6 +24,7 @@ ym = YearMonth.new(2026, 3)
24
24
  ym.to_s # => "2026-03"
25
25
  ym.to_date # => #<Date: 2026-03-01>
26
26
 
27
+ YearMonth.from(Date.today) # => YearMonth[2026-03]
27
28
  YearMonth.parse('2026-03') # => YearMonth[2026-03]
28
29
 
29
30
  ym + 1 # => YearMonth[2026-04]
@@ -42,6 +43,7 @@ md = MonthDay.new(3, 19)
42
43
  md.to_s # => "--03-19"
43
44
  md.to_date(2026) # => #<Date: 2026-03-19>
44
45
 
46
+ MonthDay.from(Date.today) # => MonthDay[--03-19]
45
47
  MonthDay.parse('--03-19') # => MonthDay[--03-19]
46
48
  ```
47
49
 
@@ -53,9 +55,31 @@ tod.to_s # => "14:30"
53
55
 
54
56
  TimeOfDay.new(14, 30, 45).to_s # => "14:30:45"
55
57
 
58
+ TimeOfDay.from(Time.now) # => TimeOfDay[14:30]
56
59
  TimeOfDay.parse('14:30') # => TimeOfDay[14:30]
57
60
  ```
58
61
 
62
+ ### Pattern Matching
63
+
64
+ Built on `Data.define`, so pattern matching works out of the box:
65
+
66
+ ```ruby
67
+ case YearMonth.new(2026, 3)
68
+ in { year: 2026, month: (1..3) }
69
+ puts 'Q1 2026'
70
+ end
71
+
72
+ case MonthDay.new(12, 25)
73
+ in { month: 12, day: 25 }
74
+ puts 'Christmas'
75
+ end
76
+
77
+ case TimeOfDay.new(14, 30)
78
+ in { hour: (9..17) }
79
+ puts 'Business hours'
80
+ end
81
+ ```
82
+
59
83
  ## Rails Integration
60
84
 
61
85
  Opt-in ActiveModel type casting for ActiveRecord attributes:
@@ -13,6 +13,10 @@ module DateValues
13
13
  super
14
14
  end
15
15
 
16
+ def self.from(date)
17
+ new(date.month, date.day)
18
+ end
19
+
16
20
  def self.parse(str)
17
21
  raise ArgumentError, "invalid MonthDay: #{str}" unless str.match?(/\A--\d{2}-\d{2}\z/)
18
22
 
@@ -12,6 +12,10 @@ module DateValues
12
12
  super
13
13
  end
14
14
 
15
+ def self.from(time)
16
+ new(time.hour, time.min, time.sec)
17
+ end
18
+
15
19
  def self.parse(str)
16
20
  parts = str.split(':')
17
21
  raise ArgumentError, "invalid TimeOfDay: #{str}" unless [2, 3].include?(parts.size)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DateValues
4
- VERSION = "0.1.0"
4
+ VERSION = '0.1.1'
5
5
  end
@@ -12,6 +12,10 @@ module DateValues
12
12
  super
13
13
  end
14
14
 
15
+ def self.from(date)
16
+ new(date.year, date.month)
17
+ end
18
+
15
19
  def self.parse(str)
16
20
  raise ArgumentError, "invalid YearMonth: #{str}" unless str.match?(/\A\d{4}-\d{2}\z/)
17
21
 
data/sig/date_values.rbs CHANGED
@@ -9,6 +9,8 @@ module DateValues
9
9
 
10
10
  def initialize: (Integer year, Integer month) -> void
11
11
 
12
+ def self.from: (Date date) -> YearMonth
13
+
12
14
  def self.parse: (String str) -> YearMonth
13
15
 
14
16
  def <=>: (YearMonth other) -> Integer
@@ -36,6 +38,8 @@ module DateValues
36
38
 
37
39
  def initialize: (Integer month, Integer day) -> void
38
40
 
41
+ def self.from: (Date date) -> MonthDay
42
+
39
43
  def self.parse: (String str) -> MonthDay
40
44
 
41
45
  def <=>: (MonthDay other) -> Integer
@@ -61,6 +65,8 @@ module DateValues
61
65
 
62
66
  def initialize: (Integer hour, Integer minute, ?Integer second) -> void
63
67
 
68
+ def self.from: (Time time) -> TimeOfDay
69
+
64
70
  def self.parse: (String str) -> TimeOfDay
65
71
 
66
72
  def <=>: (TimeOfDay other) -> Integer
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima