philiprehberger-struct_kit 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 +7 -0
- data/CHANGELOG.md +22 -0
- data/LICENSE +21 -0
- data/README.md +151 -0
- data/lib/philiprehberger/struct_kit/definition.rb +110 -0
- data/lib/philiprehberger/struct_kit/field.rb +69 -0
- data/lib/philiprehberger/struct_kit/validation.rb +20 -0
- data/lib/philiprehberger/struct_kit/version.rb +7 -0
- data/lib/philiprehberger/struct_kit.rb +16 -0
- metadata +59 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8b55b47cb37e7ebf99690da52df447bbd35570f3d025047fd37bb5be1f15cbb0
|
|
4
|
+
data.tar.gz: 03a1b60f04e75e455389020bbdf34d2ec028651fe7c14abeb0038a6c33937068
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8efe14b43443a8ce8fca842f2ae3ae70a8cb511c753319883fba1454c0fd45f8839945ebfebd282242291b51ea28148ef213756c8ca6cbb55e142c51d3ef7851
|
|
7
|
+
data.tar.gz: 668b1b75f73a814b437c6647019afbbbc78e566817de8f11a650a7cb8fa6ef7bab695e0417f0b0255dc7175cb2b1b118d6fdef9f507b971e67ea7f157408966d
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this gem will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2026-03-21
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- Initial release
|
|
14
|
+
- DSL-based struct definition with `StructKit.define`
|
|
15
|
+
- Typed fields with runtime type checking
|
|
16
|
+
- Default values (static and lambda)
|
|
17
|
+
- Value coercion via `coerce:` option
|
|
18
|
+
- Validation rules (range, format, custom blocks)
|
|
19
|
+
- Immutable instances (frozen by default)
|
|
20
|
+
- `#to_h` and `.from_h` for hash serialization
|
|
21
|
+
- `#merge` for creating modified copies
|
|
22
|
+
- Pattern matching support via `deconstruct_keys`
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 philiprehberger
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# philiprehberger-struct_kit
|
|
2
|
+
|
|
3
|
+
[](https://github.com/philiprehberger/rb-struct-kit/actions/workflows/ci.yml)
|
|
4
|
+
[](https://rubygems.org/gems/philiprehberger-struct_kit)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
Enhanced struct builder with typed fields, defaults, validation, and pattern matching
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
|
|
11
|
+
- Ruby >= 3.1
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Add to your Gemfile:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
gem 'philiprehberger-struct_kit'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or install directly:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
gem install philiprehberger-struct_kit
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
require 'philiprehberger/struct_kit'
|
|
31
|
+
|
|
32
|
+
User = Philiprehberger::StructKit.define do
|
|
33
|
+
field :name, String
|
|
34
|
+
field :age, Integer, default: 0
|
|
35
|
+
field :role, Symbol, default: :user
|
|
36
|
+
validate :age, range: 0..150
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
user = User.new(name: 'Alice', age: 30)
|
|
40
|
+
user.name # => "Alice"
|
|
41
|
+
user.age # => 30
|
|
42
|
+
user.role # => :user
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Defaults
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
Config = Philiprehberger::StructKit.define do
|
|
49
|
+
field :timeout, Integer, default: 30
|
|
50
|
+
field :tags, Array, default: -> { [] }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
config = Config.new
|
|
54
|
+
config.timeout # => 30
|
|
55
|
+
config.tags # => []
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Type Coercion
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
Record = Philiprehberger::StructKit.define do
|
|
62
|
+
field :count, Integer, default: 0, coerce: ->(v) { v.to_i }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
record = Record.new(count: '42')
|
|
66
|
+
record.count # => 42
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Validation
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
user = User.new(name: 'Alice', age: 200)
|
|
73
|
+
user.valid? # => false
|
|
74
|
+
user.errors # => ["age must be in range 0..150"]
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Immutability
|
|
78
|
+
|
|
79
|
+
```ruby
|
|
80
|
+
user = User.new(name: 'Alice')
|
|
81
|
+
user.frozen? # => true
|
|
82
|
+
|
|
83
|
+
updated = user.merge(age: 31) # returns new instance
|
|
84
|
+
updated.age # => 31
|
|
85
|
+
user.age # => 0 (unchanged)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Hash Serialization
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
user = User.new(name: 'Alice', age: 30)
|
|
92
|
+
user.to_h # => { name: "Alice", age: 30, role: :user }
|
|
93
|
+
|
|
94
|
+
User.from_h({ name: 'Bob', age: 25 })
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Pattern Matching
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
user = User.new(name: 'Alice', role: :admin)
|
|
101
|
+
|
|
102
|
+
case user
|
|
103
|
+
in { role: :admin }
|
|
104
|
+
puts 'Admin user'
|
|
105
|
+
in { role: :user }
|
|
106
|
+
puts 'Regular user'
|
|
107
|
+
end
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## API
|
|
111
|
+
|
|
112
|
+
### `Philiprehberger::StructKit`
|
|
113
|
+
|
|
114
|
+
| Method | Description |
|
|
115
|
+
|--------|-------------|
|
|
116
|
+
| `.define { block }` | Define a new struct class with the DSL |
|
|
117
|
+
|
|
118
|
+
### DSL (inside `define` block)
|
|
119
|
+
|
|
120
|
+
| Method | Description |
|
|
121
|
+
|--------|-------------|
|
|
122
|
+
| `field :name, Type, default:, coerce:` | Define a typed field with optional default and coercion |
|
|
123
|
+
| `validate :name, range:, format:` | Add validation rules to a field |
|
|
124
|
+
|
|
125
|
+
### Instance Methods
|
|
126
|
+
|
|
127
|
+
| Method | Description |
|
|
128
|
+
|--------|-------------|
|
|
129
|
+
| `#to_h` | Convert to a plain hash |
|
|
130
|
+
| `#merge(**attrs)` | Return a new instance with merged attributes |
|
|
131
|
+
| `#valid?` | Whether all validations pass |
|
|
132
|
+
| `#errors` | Array of validation error messages |
|
|
133
|
+
| `#deconstruct_keys(keys)` | Pattern matching support |
|
|
134
|
+
|
|
135
|
+
### Class Methods
|
|
136
|
+
|
|
137
|
+
| Method | Description |
|
|
138
|
+
|--------|-------------|
|
|
139
|
+
| `.from_h(hash)` | Construct from a hash (string or symbol keys) |
|
|
140
|
+
|
|
141
|
+
## Development
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
bundle install
|
|
145
|
+
bundle exec rspec # Run tests
|
|
146
|
+
bundle exec rubocop # Check code style
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
MIT
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'field'
|
|
4
|
+
require_relative 'validation'
|
|
5
|
+
|
|
6
|
+
module Philiprehberger
|
|
7
|
+
module StructKit
|
|
8
|
+
class Definition
|
|
9
|
+
def initialize
|
|
10
|
+
@fields = {}
|
|
11
|
+
@validations = {}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def field(name, type = nil, default: :__no_default__, coerce: nil)
|
|
15
|
+
@fields[name] = Field.new(name, type, default: default, coerce: coerce)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def validate(name, **rules, &block)
|
|
19
|
+
@validations[name] ||= []
|
|
20
|
+
@validations[name] << rules unless rules.empty?
|
|
21
|
+
@validations[name] << block if block
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def build
|
|
25
|
+
fields = @fields
|
|
26
|
+
validations = @validations
|
|
27
|
+
|
|
28
|
+
validations.each do |name, rules|
|
|
29
|
+
next unless fields[name]
|
|
30
|
+
|
|
31
|
+
rules.each { |rule| fields[name].add_validation(rule) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
klass = Class.new do
|
|
35
|
+
include Validation
|
|
36
|
+
|
|
37
|
+
define_method(:_fields_data) { fields }
|
|
38
|
+
|
|
39
|
+
class << self
|
|
40
|
+
attr_accessor :_fields
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
fields.each do |name, _field|
|
|
44
|
+
attr_reader name
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
define_method(:initialize) do |**kwargs|
|
|
48
|
+
self.class._fields.each do |name, f|
|
|
49
|
+
value = if kwargs.key?(name)
|
|
50
|
+
kwargs[name]
|
|
51
|
+
elsif f.has_default?
|
|
52
|
+
f.resolve_default
|
|
53
|
+
else
|
|
54
|
+
raise ArgumentError, "missing keyword: #{name}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
value = f.coerce_value(value)
|
|
58
|
+
|
|
59
|
+
unless f.validate_type(value)
|
|
60
|
+
raise TypeError, "#{name} must be a #{f.type}, got #{value.class}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
instance_variable_set(:"@#{name}", value)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
freeze
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
define_method(:to_h) do
|
|
70
|
+
self.class._fields.each_with_object({}) do |(name, _), hash|
|
|
71
|
+
hash[name] = instance_variable_get(:"@#{name}")
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
define_method(:merge) do |**attrs|
|
|
76
|
+
self.class.new(**to_h.merge(attrs))
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
define_method(:==) do |other|
|
|
80
|
+
other.is_a?(self.class) && to_h == other.to_h
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
define_method(:hash) do
|
|
84
|
+
to_h.hash
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
define_method(:deconstruct_keys) do |keys|
|
|
88
|
+
h = to_h
|
|
89
|
+
keys ? h.slice(*keys) : h
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
define_method(:to_s) do
|
|
93
|
+
fields_str = to_h.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')
|
|
94
|
+
"#<#{self.class.name || 'StructKit'} #{fields_str}>"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
define_method(:inspect) { to_s }
|
|
98
|
+
|
|
99
|
+
define_singleton_method(:from_h) do |hash|
|
|
100
|
+
sym_hash = hash.transform_keys(&:to_sym)
|
|
101
|
+
new(**sym_hash)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
klass._fields = fields
|
|
106
|
+
klass
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Philiprehberger
|
|
4
|
+
module StructKit
|
|
5
|
+
class Field
|
|
6
|
+
attr_reader :name, :type, :default, :coerce, :validations
|
|
7
|
+
|
|
8
|
+
def initialize(name, type = nil, default: :__no_default__, coerce: nil)
|
|
9
|
+
@name = name
|
|
10
|
+
@type = type
|
|
11
|
+
@default = default
|
|
12
|
+
@coerce = coerce
|
|
13
|
+
@validations = []
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def has_default?
|
|
17
|
+
@default != :__no_default__
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def resolve_default
|
|
21
|
+
return nil unless has_default?
|
|
22
|
+
|
|
23
|
+
@default.respond_to?(:call) ? @default.call : @default
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def coerce_value(value)
|
|
27
|
+
return value unless @coerce
|
|
28
|
+
|
|
29
|
+
@coerce.call(value)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def validate_type(value)
|
|
33
|
+
return true if @type.nil?
|
|
34
|
+
return true if value.nil? && has_default?
|
|
35
|
+
|
|
36
|
+
value.is_a?(@type)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def add_validation(rule)
|
|
40
|
+
@validations << rule
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def validate_value(value)
|
|
44
|
+
errors = []
|
|
45
|
+
|
|
46
|
+
unless validate_type(value)
|
|
47
|
+
errors << "#{@name} must be a #{@type}, got #{value.class}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
@validations.each do |rule|
|
|
51
|
+
case rule
|
|
52
|
+
when Hash
|
|
53
|
+
if rule[:range] && !rule[:range].include?(value)
|
|
54
|
+
errors << "#{@name} must be in range #{rule[:range]}"
|
|
55
|
+
end
|
|
56
|
+
if rule[:format] && !rule[:format].match?(value.to_s)
|
|
57
|
+
errors << "#{@name} does not match required format"
|
|
58
|
+
end
|
|
59
|
+
when Proc
|
|
60
|
+
msg = rule.call(value)
|
|
61
|
+
errors << msg if msg.is_a?(String)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
errors
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Philiprehberger
|
|
4
|
+
module StructKit
|
|
5
|
+
module Validation
|
|
6
|
+
def valid?
|
|
7
|
+
errors.empty?
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def errors
|
|
11
|
+
errs = []
|
|
12
|
+
self.class._fields.each do |name, field|
|
|
13
|
+
value = instance_variable_get(:"@#{name}")
|
|
14
|
+
errs.concat(field.validate_value(value))
|
|
15
|
+
end
|
|
16
|
+
errs
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'struct_kit/version'
|
|
4
|
+
require_relative 'struct_kit/field'
|
|
5
|
+
require_relative 'struct_kit/validation'
|
|
6
|
+
require_relative 'struct_kit/definition'
|
|
7
|
+
|
|
8
|
+
module Philiprehberger
|
|
9
|
+
module StructKit
|
|
10
|
+
def self.define(&block)
|
|
11
|
+
defn = Definition.new
|
|
12
|
+
defn.instance_eval(&block)
|
|
13
|
+
defn.build
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: philiprehberger-struct_kit
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Philip Rehberger
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-03-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Define data classes with typed fields, default values, validation rules,
|
|
14
|
+
and pattern matching support. Immutable by default with keyword-only construction,
|
|
15
|
+
JSON/Hash serialization, and runtime type checking.
|
|
16
|
+
email:
|
|
17
|
+
- me@philiprehberger.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- CHANGELOG.md
|
|
23
|
+
- LICENSE
|
|
24
|
+
- README.md
|
|
25
|
+
- lib/philiprehberger/struct_kit.rb
|
|
26
|
+
- lib/philiprehberger/struct_kit/definition.rb
|
|
27
|
+
- lib/philiprehberger/struct_kit/field.rb
|
|
28
|
+
- lib/philiprehberger/struct_kit/validation.rb
|
|
29
|
+
- lib/philiprehberger/struct_kit/version.rb
|
|
30
|
+
homepage: https://github.com/philiprehberger/rb-struct-kit
|
|
31
|
+
licenses:
|
|
32
|
+
- MIT
|
|
33
|
+
metadata:
|
|
34
|
+
homepage_uri: https://github.com/philiprehberger/rb-struct-kit
|
|
35
|
+
source_code_uri: https://github.com/philiprehberger/rb-struct-kit
|
|
36
|
+
changelog_uri: https://github.com/philiprehberger/rb-struct-kit/blob/main/CHANGELOG.md
|
|
37
|
+
bug_tracker_uri: https://github.com/philiprehberger/rb-struct-kit/issues
|
|
38
|
+
rubygems_mfa_required: 'true'
|
|
39
|
+
post_install_message:
|
|
40
|
+
rdoc_options: []
|
|
41
|
+
require_paths:
|
|
42
|
+
- lib
|
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 3.1.0
|
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
requirements: []
|
|
54
|
+
rubygems_version: 3.5.22
|
|
55
|
+
signing_key:
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: Enhanced struct builder with typed fields, defaults, validation, and pattern
|
|
58
|
+
matching
|
|
59
|
+
test_files: []
|