philiprehberger-struct_kit 0.4.0 → 0.5.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 +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +19 -0
- data/lib/philiprehberger/struct_kit/definition.rb +9 -0
- data/lib/philiprehberger/struct_kit/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 66d55207190faed7e2f44c1b5917c0c61740b9bbf4e285de11dd93c105d31cac
|
|
4
|
+
data.tar.gz: 5c263bcd332817762a19a4d01ae73209a8f98ae33f1fcbd9178881181af014e0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: eccb9ffe556b54ba950157125c6a7e6e7a4fdda5a121b4ffa193956d9ebd313e0810dde4d37b7f51d4508807903d9cd06ba22b464f1cfd4552cb2b0951b11ea8
|
|
7
|
+
data.tar.gz: 18b5d0674d7b1d7d87c395cc0f284e225d735852f826399902cde508ad2527c0bebc788f66f31a53e4155c99f5d82f8f50386d01be364aa988d9151d7f9733a4
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,11 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.5.0] - 2026-04-18
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- `.from_a(array)` class method on generated structs — constructs an instance from field values in declaration order; inverse of `#to_a`; raises `ArgumentError` on length mismatch
|
|
14
|
+
|
|
10
15
|
## [0.4.0] - 2026-04-16
|
|
11
16
|
|
|
12
17
|
### Added
|
|
@@ -117,7 +122,8 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|
|
117
122
|
- Value equality via `#==`
|
|
118
123
|
- Keyword-only constructor
|
|
119
124
|
|
|
120
|
-
[Unreleased]: https://github.com/philiprehberger/rb-struct-kit/compare/v0.
|
|
125
|
+
[Unreleased]: https://github.com/philiprehberger/rb-struct-kit/compare/v0.5.0...HEAD
|
|
126
|
+
[0.5.0]: https://github.com/philiprehberger/rb-struct-kit/compare/v0.4.0...v0.5.0
|
|
121
127
|
[0.4.0]: https://github.com/philiprehberger/rb-struct-kit/compare/v0.3.1...v0.4.0
|
|
122
128
|
[0.3.1]: https://github.com/philiprehberger/rb-struct-kit/compare/v0.3.0...v0.3.1
|
|
123
129
|
[0.3.0]: https://github.com/philiprehberger/rb-struct-kit/compare/v0.2.0...v0.3.0
|
data/README.md
CHANGED
|
@@ -177,6 +177,24 @@ Account.new(email: '', tags: ['a']) # ArgumentError: email must be present
|
|
|
177
177
|
Account.new(email: 'a@b', tags: []) # ArgumentError: tags must be present
|
|
178
178
|
```
|
|
179
179
|
|
|
180
|
+
### Positional constructor
|
|
181
|
+
|
|
182
|
+
```ruby
|
|
183
|
+
require 'philiprehberger/struct_kit'
|
|
184
|
+
|
|
185
|
+
Point = Philiprehberger::StructKit.define do
|
|
186
|
+
field :x, Integer
|
|
187
|
+
field :y, Integer
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
p = Point.from_a([1, 2])
|
|
191
|
+
p.x # => 1
|
|
192
|
+
p.y # => 2
|
|
193
|
+
|
|
194
|
+
# Roundtrip with #to_a
|
|
195
|
+
Point.from_a(p.to_a) == p # => true
|
|
196
|
+
```
|
|
197
|
+
|
|
180
198
|
### Introspection
|
|
181
199
|
|
|
182
200
|
```ruby
|
|
@@ -220,6 +238,7 @@ Define a new struct class. Evaluates the block in DSL context.
|
|
|
220
238
|
| Method | Description |
|
|
221
239
|
|--------|-------------|
|
|
222
240
|
| `.from_h(hash)` | Construct from hash (string or symbol keys) |
|
|
241
|
+
| `.from_a(array)` | Construct from an array of values in field-declaration order (inverse of `#to_a`) |
|
|
223
242
|
| `.field_names` | Return the declared field names in order |
|
|
224
243
|
|
|
225
244
|
## Development
|
|
@@ -105,6 +105,15 @@ module Philiprehberger
|
|
|
105
105
|
new(**sym_hash)
|
|
106
106
|
end
|
|
107
107
|
|
|
108
|
+
define_singleton_method(:from_a) do |array|
|
|
109
|
+
if array.length != _fields.size
|
|
110
|
+
raise ArgumentError,
|
|
111
|
+
"expected #{_fields.size} values for #{_fields.keys}, got #{array.length}"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
new(**_fields.keys.zip(array).to_h)
|
|
115
|
+
end
|
|
116
|
+
|
|
108
117
|
define_singleton_method(:field_names) do
|
|
109
118
|
_fields.keys
|
|
110
119
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: philiprehberger-struct_kit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Philip Rehberger
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-04-
|
|
11
|
+
date: 2026-04-19 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,
|