guide-rail 0.2.1 → 0.2.2
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 +4 -0
- data/README.md +58 -31
- data/lib/guide_rail/factory.rb +64 -17
- data/lib/guide_rail/version.rb +1 -1
- data/lib/guide_rail.rb +17 -1
- data/sig/guide-rail.rbs +2 -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: 2ce10b508a5ce01fbf26be3e766badf62f70a095c2ce7e4d091eeb264037c098
|
|
4
|
+
data.tar.gz: ef7d1b914421af3bb3cc213fec02dd73c95253e7cd1bc3c7b02836b83d543501
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c027c09ce16460cbc92203995b62a1dd2a308c3efd791639843d9f1ae65cea239c26f7017e3693c25e1368b042372ac10526adc467f606ec1436dacbd11c692c
|
|
7
|
+
data.tar.gz: 0506401c27d731f7a1ca1334a80be040348423b05809ee1de1396ae7ba1e7e9f22e8dc9873f6f5258268d5dc1f07b7f753d157a0741810b51545f39056511593
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -59,11 +59,31 @@ SimpleNonNullableCreator.from(name: "John") # => #<data SimpleNonNullable name="
|
|
|
59
59
|
SimpleNonNullableCreator.from({}) # => #<Dry::Schema::Result{} errors={name: ["is missing"]} path=[]>
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
+
Values of the generated Data object come from the schema result, so nested Hashes have only the keys in the schema. With `Dry::Schema.Params`, String keys are accepted and values are coerced:
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
AgeSchema = Dry::Schema.Params do
|
|
66
|
+
required(:age).filled(:integer)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
class AgeCreator
|
|
70
|
+
extend GuideRail
|
|
71
|
+
|
|
72
|
+
schema AgeSchema
|
|
73
|
+
class_name :Age
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
AgeCreator.from("age" => "20") # => #<data Age age=20>
|
|
77
|
+
```
|
|
78
|
+
|
|
62
79
|
### Nullable Object with `renderable` option
|
|
63
80
|
|
|
81
|
+
With `renderable`, `nil` and missing optional keys become `""`.
|
|
82
|
+
|
|
64
83
|
```ruby
|
|
65
84
|
SimpleNullableSchema = Dry::Schema.define do
|
|
66
85
|
required(:name).maybe(:string)
|
|
86
|
+
optional(:nickname).filled(:string)
|
|
67
87
|
end
|
|
68
88
|
|
|
69
89
|
class SimpleRenderableCreator
|
|
@@ -74,65 +94,72 @@ class SimpleRenderableCreator
|
|
|
74
94
|
renderable true
|
|
75
95
|
end
|
|
76
96
|
|
|
77
|
-
SimpleRenderableCreator.from(name: nil) # => #<data SimpleRenderable name="">
|
|
78
|
-
SimpleRenderableCreator.from(name: "John") # => #<data SimpleRenderable name="John">
|
|
97
|
+
SimpleRenderableCreator.from(name: nil) # => #<data SimpleRenderable name="", nickname="">
|
|
98
|
+
SimpleRenderableCreator.from(name: "John", nickname: "Jack") # => #<data SimpleRenderable name="John", nickname="Jack">
|
|
79
99
|
SimpleRenderableCreator.from({}) # => #<Dry::Schema::Result{} errors={name: ["is missing"]} path=[]>
|
|
80
100
|
```
|
|
81
101
|
|
|
82
|
-
###
|
|
102
|
+
### Computed attributes
|
|
83
103
|
|
|
84
|
-
|
|
104
|
+
`computed` defines an attribute derived from validated input. The block receives `acc`, which has readers of validated input ( e.g. `acc.first_name` ), and returns the value. A new name adds a member to the generated Data class, and a name that exists in the schema overwrites the value.
|
|
85
105
|
|
|
86
|
-
|
|
106
|
+
`acc` does not contain results of other computed attributes.
|
|
87
107
|
|
|
88
108
|
```ruby
|
|
89
|
-
|
|
90
|
-
|
|
109
|
+
UserSchema = Dry::Schema.define do
|
|
110
|
+
required(:first_name).filled(:string)
|
|
111
|
+
required(:last_name).filled(:string)
|
|
112
|
+
required(:role).filled(:string)
|
|
113
|
+
required(:status).filled(:string)
|
|
91
114
|
end
|
|
92
115
|
|
|
93
|
-
class
|
|
116
|
+
class UserView
|
|
94
117
|
extend GuideRail
|
|
95
118
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
renderable true
|
|
119
|
+
schema UserSchema
|
|
120
|
+
class_name :User
|
|
99
121
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
122
|
+
computed :display_name do |acc|
|
|
123
|
+
"#{acc.first_name} #{acc.last_name}"
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
computed :status do |acc|
|
|
127
|
+
(acc.role == "admin") ? "owner" : acc.status
|
|
105
128
|
end
|
|
106
129
|
end
|
|
107
130
|
|
|
108
|
-
|
|
131
|
+
UserView.from(first_name: "Jane", last_name: "Doe", role: "admin", status: "member")
|
|
132
|
+
# => #<data User first_name="Jane", last_name="Doe", role="admin", status="owner", display_name="Jane Doe">
|
|
109
133
|
```
|
|
110
134
|
|
|
111
|
-
###
|
|
112
|
-
|
|
113
|
-
Values of the generated Data object come from the schema result, not from the input as is. So the Data object has the values the schema validated.
|
|
135
|
+
### Extending the Data class
|
|
114
136
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
Use `Dry::Schema.Params` for HTTP params. String keys are accepted, and values are coerced before validation.
|
|
137
|
+
`yield_block` gives a block to `Data.define` of the generated class. Use it for what cannot be an attribute, such as methods with arguments.
|
|
118
138
|
|
|
119
139
|
```ruby
|
|
120
|
-
|
|
121
|
-
required(:
|
|
140
|
+
NameSchema = Dry::Schema.define do
|
|
141
|
+
required(:first_name).filled(:string)
|
|
142
|
+
required(:last_name).filled(:string)
|
|
122
143
|
end
|
|
123
144
|
|
|
124
|
-
class
|
|
145
|
+
class NameView
|
|
125
146
|
extend GuideRail
|
|
126
147
|
|
|
127
|
-
schema
|
|
128
|
-
class_name :
|
|
148
|
+
schema NameSchema
|
|
149
|
+
class_name :Name
|
|
150
|
+
|
|
151
|
+
yield_block do
|
|
152
|
+
def full_name(order: :western)
|
|
153
|
+
(order == :eastern) ? "#{last_name} #{first_name}" : "#{first_name} #{last_name}"
|
|
154
|
+
end
|
|
155
|
+
end
|
|
129
156
|
end
|
|
130
157
|
|
|
131
|
-
|
|
158
|
+
name = NameView.from(first_name: "Taro", last_name: "Yamada")
|
|
159
|
+
name.full_name # => "Taro Yamada"
|
|
160
|
+
name.full_name(order: :eastern) # => "Yamada Taro"
|
|
132
161
|
```
|
|
133
162
|
|
|
134
|
-
In both cases, keys not in the schema are dropped, including those in nested Hashes.
|
|
135
|
-
|
|
136
163
|
### Monadic mode
|
|
137
164
|
|
|
138
165
|
```ruby
|
data/lib/guide_rail/factory.rb
CHANGED
|
@@ -7,11 +7,11 @@ class GuideRail::Factory
|
|
|
7
7
|
# @param [String|Symbol] klass
|
|
8
8
|
#
|
|
9
9
|
def class_name(klass)
|
|
10
|
-
@
|
|
10
|
+
@_class_name = klass
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
#
|
|
14
|
-
# @param [Dry::Schema::Params] schema
|
|
14
|
+
# @param [Dry::Schema::Params|Dry::Schema::Processor] schema
|
|
15
15
|
#
|
|
16
16
|
def schema(schema)
|
|
17
17
|
@_schema = schema
|
|
@@ -38,15 +38,26 @@ class GuideRail::Factory
|
|
|
38
38
|
@_block = block
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
#
|
|
42
|
+
# @param [Symbol] name
|
|
43
|
+
# @param [Proc] block
|
|
44
|
+
#
|
|
45
|
+
def computed(name, &block)
|
|
46
|
+
raise ArgumentError, "computed :#{name} requires a block" unless block
|
|
47
|
+
|
|
48
|
+
(@_computeds ||= {})[name.to_sym] = block
|
|
49
|
+
end
|
|
50
|
+
|
|
41
51
|
#
|
|
42
52
|
# @return [Data]
|
|
43
53
|
#
|
|
44
54
|
def create_class!
|
|
45
|
-
Object.const_get @
|
|
55
|
+
Object.const_get @_class_name
|
|
46
56
|
rescue NameError
|
|
47
|
-
@
|
|
48
|
-
|
|
49
|
-
|
|
57
|
+
@_schema_attrs = @_schema.key_map.to_a.map { |k| k.name.to_sym }
|
|
58
|
+
@_acc_class = Data.define(*@_schema_attrs)
|
|
59
|
+
klass = Data.define(*all_member_names, &@_block)
|
|
60
|
+
Object.const_set(@_class_name, klass)
|
|
50
61
|
klass
|
|
51
62
|
end
|
|
52
63
|
|
|
@@ -58,19 +69,14 @@ class GuideRail::Factory
|
|
|
58
69
|
acceptance = accept(data)
|
|
59
70
|
create_class!
|
|
60
71
|
result = @_schema.call(acceptance)
|
|
61
|
-
Object.const_get @
|
|
72
|
+
Object.const_get @_class_name
|
|
62
73
|
|
|
63
74
|
if result.success?
|
|
64
|
-
values = result
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
else
|
|
70
|
-
values
|
|
71
|
-
end
|
|
72
|
-
).values_at(*@_attrs).map(&:freeze)
|
|
73
|
-
)
|
|
75
|
+
values = schema_values(result)
|
|
76
|
+
values.merge!(compute(values))
|
|
77
|
+
values = to_renderable(values, renderable_default.merge(computed_shallow_renderable_default)) if @_renderable
|
|
78
|
+
|
|
79
|
+
new_value = Object.const_get(@_class_name).new(*values.values_at(*all_member_names).map(&:freeze))
|
|
74
80
|
|
|
75
81
|
@_monadic ? Success(new_value) : new_value
|
|
76
82
|
else
|
|
@@ -78,6 +84,47 @@ class GuideRail::Factory
|
|
|
78
84
|
end
|
|
79
85
|
end
|
|
80
86
|
|
|
87
|
+
#
|
|
88
|
+
# @return [Array<Symbol>]
|
|
89
|
+
#
|
|
90
|
+
def all_member_names
|
|
91
|
+
(@_schema_attrs + (@_computeds || {}).keys).uniq
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
#
|
|
95
|
+
# Values come from the schema result, so they are coerced by dry-schema
|
|
96
|
+
# ( e.g. "20" -> 20 with Params ). Keys are aligned to the schema attrs
|
|
97
|
+
# ( missing optional keys become nil ) and values are frozen.
|
|
98
|
+
#
|
|
99
|
+
# @param [Dry::Schema::Result] result
|
|
100
|
+
# @return [Hash]
|
|
101
|
+
#
|
|
102
|
+
def schema_values(result)
|
|
103
|
+
values = result.to_h
|
|
104
|
+
@_schema_attrs.to_h { |k| [k, values[k].freeze] }
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
#
|
|
108
|
+
# Evaluate each computed block with acc made of validated input and
|
|
109
|
+
# return the results by name.
|
|
110
|
+
#
|
|
111
|
+
# @param [Hash] values
|
|
112
|
+
# @return [Hash]
|
|
113
|
+
#
|
|
114
|
+
def compute(values)
|
|
115
|
+
return {} unless @_computeds
|
|
116
|
+
|
|
117
|
+
acc = @_acc_class.new(**values)
|
|
118
|
+
@_computeds.transform_values { |block| block.call(acc).freeze }
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
#
|
|
122
|
+
# @return [Hash]
|
|
123
|
+
#
|
|
124
|
+
def computed_shallow_renderable_default
|
|
125
|
+
(@_computeds || {}).keys.to_h { |k| [k, ""] }
|
|
126
|
+
end
|
|
127
|
+
|
|
81
128
|
#
|
|
82
129
|
# @param [untyped] data
|
|
83
130
|
# @return [Hash]
|
data/lib/guide_rail/version.rb
CHANGED
data/lib/guide_rail.rb
CHANGED
|
@@ -24,7 +24,7 @@ module GuideRail
|
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
#
|
|
27
|
-
# @param [Dry::Schema::Params] schema
|
|
27
|
+
# @param [Dry::Schema::Params|Dry::Schema::Processor] schema
|
|
28
28
|
#
|
|
29
29
|
def schema(schema)
|
|
30
30
|
@_guide_rail_factory.schema(schema)
|
|
@@ -51,6 +51,22 @@ module GuideRail
|
|
|
51
51
|
@_guide_rail_factory.yield_block(&block)
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
#
|
|
55
|
+
# Define a computed attribute derived from validated input.
|
|
56
|
+
# The block receives acc ( read-only accessor of validated input ) and
|
|
57
|
+
# returns the value. Same name as a schema attr overwrites it.
|
|
58
|
+
#
|
|
59
|
+
# computed :display_name do |acc|
|
|
60
|
+
# "#{acc.first_name} #{acc.last_name}"
|
|
61
|
+
# end
|
|
62
|
+
#
|
|
63
|
+
# @param [Symbol] name
|
|
64
|
+
# @param [Proc] block
|
|
65
|
+
#
|
|
66
|
+
def computed(name, &block)
|
|
67
|
+
@_guide_rail_factory.computed(name, &block)
|
|
68
|
+
end
|
|
69
|
+
|
|
54
70
|
#
|
|
55
71
|
# @return [Data]
|
|
56
72
|
#
|
data/sig/guide-rail.rbs
CHANGED
|
@@ -3,9 +3,10 @@ module GuideRail
|
|
|
3
3
|
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
|
|
4
4
|
|
|
5
5
|
def class_name: (klass: String | Symbol) -> String | Symbol
|
|
6
|
-
def schema: (schema: Dry::Schema::Params) -> Dry::Schema::Params
|
|
6
|
+
def schema: (schema: Dry::Schema::Params | Dry::Schema::Processor) -> (Dry::Schema::Params | Dry::Schema::Processor)
|
|
7
7
|
def renderable: (bool: bool) -> bool
|
|
8
8
|
def yield_block: { } -> void
|
|
9
|
+
def computed: (Symbol name) { (untyped acc) -> untyped } -> Proc
|
|
9
10
|
def create_class!: () -> Data
|
|
10
11
|
def from: (untyped data) -> Data | Dry::Schema::Result | Dry::Monads::Result<Data | Dry::Schema::Result>
|
|
11
12
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: guide-rail
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- wtnabe
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-22 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: dry-schema
|