philiprehberger-struct_kit 0.1.10 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba562c9ffd1a02e0779d43522f7723255e3eefaa78392ec0976d2f4ca6f3b962
4
- data.tar.gz: f136ffe60be68b848f98fc9f339cb3377a84705045ca391c0a89e75b78aa9e41
3
+ metadata.gz: fcc66136d59c7eb3cb37b487a849582ec4f2cd9c322a60dc924402aa380f1d7e
4
+ data.tar.gz: ad8bc697727169695feb41cc1dc37efcab5a3401e1190944e831f9612f252c1b
5
5
  SHA512:
6
- metadata.gz: b1be2a6a3f9a6fc75cfac72f7be42090f8b596109b015391bd64c7923a786f22fa439922483c5435e5661940d62ed14773cec40cddea6ecfb4ed9a286b2a2102
7
- data.tar.gz: 362c167d9c443ba40d646a8968e24dfaaed0af600a9c461639dd3e579e5ff80f7e8943fa6c2aa387e27e042d502b13b9f1447f303cf4d73a550c7cac05e0ac60
6
+ metadata.gz: 747aeabf61174e81aff1f9792b8b3d4fd2044a201805716c0eac054ee33385bd86552c75c1d98177d61e13f94d55e6af581a84f6033cd6110cf235e7b09a7a0b
7
+ data.tar.gz: 6c60682cdcf912430453471cfd9ddba394a06110d9edcb8f44e40fd9e856fb5b0467cafa62792809f8f07035c44e66d13878a697a67f8d86db4bcfe4b80d69d7
data/CHANGELOG.md CHANGED
@@ -7,6 +7,21 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.0] - 2026-04-04
11
+
12
+ ### Added
13
+ - Type coercion support via `coerce:` option on `field` DSL
14
+ - GitHub issue template gem version field
15
+ - Feature request "Alternatives considered" field
16
+
17
+ ### Fixed
18
+ - Gemspec author and email to match standard template
19
+
20
+ ## [0.1.11] - 2026-03-31
21
+
22
+ ### Added
23
+ - Add GitHub issue templates, dependabot config, and PR template
24
+
10
25
  ## [0.1.10] - 2026-03-31
11
26
 
12
27
  ### Changed
data/README.md CHANGED
@@ -97,6 +97,22 @@ user.to_json # => '{"name":"Alice","age":30,"role":"user"}'
97
97
  User.from_h({ 'name' => 'Bob', 'age' => 25 }) # string keys OK
98
98
  ```
99
99
 
100
+ ### Coercion
101
+
102
+ ```ruby
103
+ require "philiprehberger/struct_kit"
104
+
105
+ User = Philiprehberger::StructKit.define do
106
+ field :age, Integer, coerce: ->(v) { Integer(v) }
107
+ field :status, Symbol, coerce: ->(v) { v.to_sym }
108
+ validate :age, range: 0..150
109
+ end
110
+
111
+ user = User.new(age: "25", status: "active")
112
+ user.age # => 25 (Integer)
113
+ user.status # => :active (Symbol)
114
+ ```
115
+
100
116
  ### Pattern Matching
101
117
 
102
118
  ```ruby
@@ -118,7 +134,7 @@ Define a new struct class. Evaluates the block in DSL context.
118
134
 
119
135
  | Method | Description |
120
136
  |--------|-------------|
121
- | `field(name, type = nil, default: UNSET)` | Declare a typed field with optional default |
137
+ | `field(name, type = nil, default: UNSET, coerce: nil)` | Declare a typed field with optional default and coercion |
122
138
  | `validate(name, range: nil, format: nil, &block)` | Add validation rule to a field |
123
139
 
124
140
  ### Instance Methods
@@ -11,8 +11,8 @@ module Philiprehberger
11
11
  @mutable = mutable
12
12
  end
13
13
 
14
- def field(name, type = nil, default: Field::UNSET)
15
- @fields[name] = Field.new(name, type, default: default)
14
+ def field(name, type = nil, default: Field::UNSET, coerce: nil)
15
+ @fields[name] = Field.new(name, type, default: default, coerce: coerce)
16
16
  end
17
17
 
18
18
  def validate(field_name, range: nil, format: nil, &block)
@@ -54,6 +54,8 @@ module Philiprehberger
54
54
  raise ArgumentError, "missing keyword: #{fname}"
55
55
  end
56
56
 
57
+ value = f.coerce_value(value)
58
+
57
59
  unless f.type_valid?(value)
58
60
  expected = f.type.is_a?(Array) ? f.type.map(&:name).join(' or ') : f.type.name
59
61
  raise TypeError, "#{fname} must be #{expected}, got #{value.class}"
@@ -5,15 +5,23 @@ module Philiprehberger
5
5
  class Field
6
6
  UNSET = Object.new.freeze
7
7
 
8
- attr_reader :name, :type, :validations
8
+ attr_reader :name, :type, :validations, :coerce
9
9
 
10
- def initialize(name, type = nil, default: UNSET)
10
+ def initialize(name, type = nil, default: UNSET, coerce: nil)
11
11
  @name = name
12
12
  @type = type
13
13
  @default = default
14
+ @coerce = coerce
14
15
  @validations = []
15
16
  end
16
17
 
18
+ def coerce_value(value)
19
+ return value if @coerce.nil?
20
+ return @coerce.call(value) if @coerce.is_a?(Proc)
21
+
22
+ value
23
+ end
24
+
17
25
  def has_default?
18
26
  @default != UNSET
19
27
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module StructKit
5
- VERSION = '0.1.10'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,20 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-struct_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - philiprehberger
7
+ - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-03-31 00:00:00.000000000 Z
11
+ date: 2026-04-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Define data classes with typed fields, default values, validation rules,
14
14
  and pattern matching support. Immutable by default with keyword-only construction,
15
15
  JSON/Hash serialization, and runtime type checking.
16
16
  email:
17
- - philiprehberger@users.noreply.github.com
17
+ - me@philiprehberger.com
18
18
  executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
@@ -26,11 +26,11 @@ files:
26
26
  - lib/philiprehberger/struct_kit/definition.rb
27
27
  - lib/philiprehberger/struct_kit/field.rb
28
28
  - lib/philiprehberger/struct_kit/version.rb
29
- homepage: https://github.com/philiprehberger/rb-struct-kit
29
+ homepage: https://philiprehberger.com/open-source-packages/ruby/philiprehberger-struct_kit
30
30
  licenses:
31
31
  - MIT
32
32
  metadata:
33
- homepage_uri: https://github.com/philiprehberger/rb-struct-kit
33
+ homepage_uri: https://philiprehberger.com/open-source-packages/ruby/philiprehberger-struct_kit
34
34
  source_code_uri: https://github.com/philiprehberger/rb-struct-kit
35
35
  changelog_uri: https://github.com/philiprehberger/rb-struct-kit/blob/main/CHANGELOG.md
36
36
  bug_tracker_uri: https://github.com/philiprehberger/rb-struct-kit/issues