date_values-rails 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5c1bf3a457deec7273dfaa168ac05e53502775d7877d6c02159ec25639c7fdd8
4
+ data.tar.gz: 7c17fd631f2035b8e68899e4d1b9ae5393e578c77db2429ad69222e8532a525f
5
+ SHA512:
6
+ metadata.gz: 3fb2a359513982c11b69c290af20f23d1b651069474879525153387b96a97921165da07078e493f84634929ea31fec8c1c2b6eb796a13b865ffb6b4a0d52e6fb
7
+ data.tar.gz: 7bfdab0ce9089d8ab4fd7d121765255c2ccc9e7cfe2eabd4ab736714fbf6714d76dbce638f4e6eecbad05abd6b77ea3244b1de72aa2957dbc3b3271d9cf8c543
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-03-20
4
+
5
+ - Extracted from [date_values](https://github.com/ursm/date_values) gem
6
+ - ActiveModel types: `:year_month`, `:month_day`, `:time_of_day`
7
+ - ActiveRecord type registration via `ActiveSupport.on_load`
8
+ - I18n backend extension for Rails `l` helper
9
+ - `date_value` validator for invalid input detection
10
+ - Cast accepts Hash params from forms and lenient string formats
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Keita Urashima
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,117 @@
1
+ # DateValues::Rails
2
+
3
+ Rails integration for [date_values](https://github.com/ursm/date_values) — ActiveModel type casting, validation, and I18n support for `YearMonth`, `MonthDay`, and `TimeOfDay`.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bundle add date_values-rails
9
+ ```
10
+
11
+ This will also install `date_values` as a dependency.
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ require 'date_values/rails'
17
+
18
+ class Shop < ApplicationRecord
19
+ attribute :billing_month, :year_month # string column "2026-03"
20
+ attribute :anniversary, :month_day # string column "--03-19"
21
+ attribute :opens_at, :time_of_day # string or time column
22
+ end
23
+ ```
24
+
25
+ ### Form Input
26
+
27
+ Cast accepts Hash params from forms, so select-based inputs work naturally:
28
+
29
+ ```ruby
30
+ # params[:shop][:anniversary] => {"month" => "3", "day" => "19"}
31
+ shop.anniversary # => #<DateValues::MonthDay --03-19>
32
+ ```
33
+
34
+ String input is lenient — `"3/19"`, `"03-19"`, and `"--03-19"` are all accepted.
35
+
36
+ ### Queries
37
+
38
+ Values are automatically serialized in queries:
39
+
40
+ ```ruby
41
+ Shop.where(billing_month: YearMonth.new(2026, 3))
42
+ # SELECT * FROM shops WHERE billing_month = '2026-03'
43
+ ```
44
+
45
+ ### Validation
46
+
47
+ All classes are `Comparable` and value-equal, so standard Rails validators work as-is:
48
+
49
+ ```ruby
50
+ class Contract < ApplicationRecord
51
+ attribute :start_month, :year_month
52
+ attribute :opens_at, :time_of_day
53
+
54
+ validates :start_month, comparison: {greater_than: -> { YearMonth.from(Date.current) }}
55
+ validates :opens_at, comparison: {
56
+ greater_than_or_equal_to: TimeOfDay.new(9, 0),
57
+ less_than_or_equal_to: TimeOfDay.new(17, 0)
58
+ }
59
+ end
60
+ ```
61
+
62
+ Invalid input (e.g. `"25:00"`) is cast to `nil` rather than raising, following the same convention as Rails' built-in types. The `date_value` validator detects this and gives a meaningful error message:
63
+
64
+ ```ruby
65
+ class Shop < ApplicationRecord
66
+ attribute :opens_at, :time_of_day
67
+
68
+ validates :opens_at, presence: true, date_value: true
69
+ end
70
+
71
+ Shop.new(opens_at: '25:00').errors[:opens_at] # => ["is invalid"]
72
+ Shop.new(opens_at: '').errors[:opens_at] # => ["can't be blank"]
73
+ ```
74
+
75
+ ### I18n / `l` Helper
76
+
77
+ All classes implement `#strftime`, and the Rails integration extends `I18n.l` to support them. Define formats in your locale files:
78
+
79
+ ```yaml
80
+ # config/locales/en.yml
81
+ en:
82
+ year_month:
83
+ formats:
84
+ default: '%B %Y'
85
+ month_day:
86
+ formats:
87
+ default: '%B %-d'
88
+ time_of_day:
89
+ formats:
90
+ default: '%-I:%M %p'
91
+ long: '%-I:%M:%S %p'
92
+ ```
93
+
94
+ ```yaml
95
+ # config/locales/ja.yml
96
+ ja:
97
+ year_month:
98
+ formats:
99
+ default: '%Y年%-m月'
100
+ month_day:
101
+ formats:
102
+ default: '%-m月%-d日'
103
+ time_of_day:
104
+ formats:
105
+ default: '%-H時%-M分'
106
+ long: '%-H時%-M分%-S秒'
107
+ ```
108
+
109
+ ```ruby
110
+ I18n.l YearMonth.new(2026, 3), locale: :en # => "March 2026"
111
+ I18n.l YearMonth.new(2026, 3), locale: :ja # => "2026年3月"
112
+ I18n.l TimeOfDay.new(14, 30), format: :long # => "2:30:00 PM"
113
+ ```
114
+
115
+ ## License
116
+
117
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'minitest/test_task'
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model/validator'
4
+
5
+ class DateValueValidator < ActiveModel::EachValidator
6
+ def validate_each(record, attribute, value)
7
+ raw = record.read_attribute_before_type_cast(attribute)
8
+ return if raw.blank?
9
+ return unless value.nil?
10
+
11
+ record.errors.add(attribute, :invalid, **options)
12
+ end
13
+ 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
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DateValues
4
+ module Rails
5
+ class MonthDayType < ActiveModel::Type::Value
6
+ def type
7
+ :month_day
8
+ end
9
+
10
+ def cast(value)
11
+ case value
12
+ when MonthDay then value
13
+ when Hash then cast_hash(value)
14
+ when String then MonthDay.parse(value)
15
+ when nil then nil
16
+ end
17
+ rescue ArgumentError
18
+ nil
19
+ end
20
+
21
+ def serialize(value)
22
+ value&.to_s
23
+ end
24
+
25
+ def deserialize(value)
26
+ return nil if value.nil?
27
+
28
+ MonthDay.parse(value)
29
+ end
30
+
31
+ private
32
+
33
+ def cast_hash(hash)
34
+ hash = hash.transform_keys(&:to_sym)
35
+ month = hash[:month]&.to_i
36
+ day = hash[:day]&.to_i
37
+ return nil unless month && day
38
+
39
+ MonthDay.new(month, day)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DateValues
4
+ module Rails
5
+ class TimeOfDayType < ActiveModel::Type::Value
6
+ def type
7
+ :time_of_day
8
+ end
9
+
10
+ def cast(value)
11
+ case value
12
+ when TimeOfDay then value
13
+ when Hash then cast_hash(value)
14
+ when Time then TimeOfDay.new(value.hour, value.min, value.sec)
15
+ when String then TimeOfDay.parse(value)
16
+ when nil then nil
17
+ end
18
+ rescue ArgumentError
19
+ nil
20
+ end
21
+
22
+ def serialize(value)
23
+ value&.to_s
24
+ end
25
+
26
+ def deserialize(value)
27
+ case value
28
+ when nil then nil
29
+ when Time then TimeOfDay.new(value.hour, value.min, value.sec)
30
+ when String then TimeOfDay.parse(value)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def cast_hash(hash)
37
+ hash = hash.transform_keys(&:to_sym)
38
+ hour = hash[:hour]&.to_i
39
+ minute = hash[:minute]&.to_i
40
+ second = hash[:second]&.to_i
41
+ return nil unless hour && minute
42
+
43
+ TimeOfDay.new(hour, minute, second || 0)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DateValues
4
+ module Rails
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DateValues
4
+ module Rails
5
+ class YearMonthType < ActiveModel::Type::Value
6
+ def type
7
+ :year_month
8
+ end
9
+
10
+ def cast(value)
11
+ case value
12
+ when YearMonth then value
13
+ when Hash then cast_hash(value)
14
+ when String then YearMonth.parse(value)
15
+ when nil then nil
16
+ end
17
+ rescue ArgumentError
18
+ nil
19
+ end
20
+
21
+ def serialize(value)
22
+ value&.to_s
23
+ end
24
+
25
+ def deserialize(value)
26
+ return nil if value.nil?
27
+
28
+ YearMonth.parse(value)
29
+ end
30
+
31
+ private
32
+
33
+ def cast_hash(hash)
34
+ hash = hash.transform_keys(&:to_sym)
35
+ year = hash[:year]&.to_i
36
+ month = hash[:month]&.to_i
37
+ return nil unless year && month
38
+
39
+ YearMonth.new(year, month)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date_values'
4
+ require_relative 'rails/version'
5
+ require 'active_support'
6
+ require 'active_model/type'
7
+ require_relative 'rails/year_month_type'
8
+ require_relative 'rails/month_day_type'
9
+ require_relative 'rails/time_of_day_type'
10
+ require_relative 'rails/i18n_backend'
11
+ require_relative 'rails/date_value_validator'
12
+
13
+ ActiveModel::Type.register(:year_month, DateValues::Rails::YearMonthType)
14
+ ActiveModel::Type.register(:month_day, DateValues::Rails::MonthDayType)
15
+ ActiveModel::Type.register(:time_of_day, DateValues::Rails::TimeOfDayType)
16
+
17
+ ActiveSupport.on_load(:active_record) do
18
+ ActiveRecord::Type.register(:year_month, DateValues::Rails::YearMonthType)
19
+ ActiveRecord::Type.register(:month_day, DateValues::Rails::MonthDayType)
20
+ ActiveRecord::Type.register(:time_of_day, DateValues::Rails::TimeOfDayType)
21
+ end
22
+
23
+ I18n::Backend::Base.prepend(DateValues::Rails::I18nBackend)
@@ -0,0 +1,44 @@
1
+ module DateValues
2
+ module Rails
3
+ class YearMonthType < ActiveModel::Type::Value
4
+ def type: () -> :year_month
5
+
6
+ def cast: (YearMonth value) -> YearMonth
7
+ | (String value) -> YearMonth
8
+ | (nil value) -> nil
9
+
10
+ def serialize: (YearMonth? value) -> String?
11
+
12
+ def deserialize: (String value) -> YearMonth
13
+ | (nil value) -> nil
14
+ end
15
+
16
+ class MonthDayType < ActiveModel::Type::Value
17
+ def type: () -> :month_day
18
+
19
+ def cast: (MonthDay value) -> MonthDay
20
+ | (String value) -> MonthDay
21
+ | (nil value) -> nil
22
+
23
+ def serialize: (MonthDay? value) -> String?
24
+
25
+ def deserialize: (String value) -> MonthDay
26
+ | (nil value) -> nil
27
+ end
28
+
29
+ class TimeOfDayType < ActiveModel::Type::Value
30
+ def type: () -> :time_of_day
31
+
32
+ def cast: (TimeOfDay value) -> TimeOfDay
33
+ | (Time value) -> TimeOfDay
34
+ | (String value) -> TimeOfDay
35
+ | (nil value) -> nil
36
+
37
+ def serialize: (TimeOfDay? value) -> String?
38
+
39
+ def deserialize: (Time value) -> TimeOfDay
40
+ | (String value) -> TimeOfDay
41
+ | (nil value) -> nil
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: date_values-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Keita Urashima
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: date_values
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 0.2.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.2.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: activemodel
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '7.2'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '7.2'
40
+ email:
41
+ - ursm@ursm.jp
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - CHANGELOG.md
47
+ - LICENSE.txt
48
+ - README.md
49
+ - Rakefile
50
+ - lib/date_values/rails.rb
51
+ - lib/date_values/rails/date_value_validator.rb
52
+ - lib/date_values/rails/i18n_backend.rb
53
+ - lib/date_values/rails/month_day_type.rb
54
+ - lib/date_values/rails/time_of_day_type.rb
55
+ - lib/date_values/rails/version.rb
56
+ - lib/date_values/rails/year_month_type.rb
57
+ - sig/date_values/rails.rbs
58
+ homepage: https://github.com/ursm/date_values-rails
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ homepage_uri: https://github.com/ursm/date_values-rails
63
+ source_code_uri: https://github.com/ursm/date_values-rails
64
+ changelog_uri: https://github.com/ursm/date_values-rails/blob/main/CHANGELOG.md
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.3.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 4.0.6
80
+ specification_version: 4
81
+ summary: Rails integration for date_values
82
+ test_files: []