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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d861d6057e043f72f337d80ccd5707072bd18457f24f1ab21b4acb04dd0c55f6
4
- data.tar.gz: 1191bbf29b5f510c775a248e4a44869d915a89e8c17b4e3b45c925caf478417a
3
+ metadata.gz: 2ce10b508a5ce01fbf26be3e766badf62f70a095c2ce7e4d091eeb264037c098
4
+ data.tar.gz: ef7d1b914421af3bb3cc213fec02dd73c95253e7cd1bc3c7b02836b83d543501
5
5
  SHA512:
6
- metadata.gz: d9e4d41b32c7794239e8ae34a2ce0431113a4653bb13477a2abef287dcee647d8583919a80de0f47afdac971c775c3d43ef0ec6a0e817e9db6089ecd1d0c2ae9
7
- data.tar.gz: 316e43fea80969de8ec9e4b6f330a57901bb0433ff5b7db81839992d2d2d317584a7108cc5b251a11a73d3751b90cfd2b7ab88028ce2dc6ccca685ec40019722
6
+ metadata.gz: c027c09ce16460cbc92203995b62a1dd2a308c3efd791639843d9f1ae65cea239c26f7017e3693c25e1368b042372ac10526adc467f606ec1436dacbd11c692c
7
+ data.tar.gz: 0506401c27d731f7a1ca1334a80be040348423b05809ee1de1396ae7ba1e7e9f22e8dc9873f6f5258268d5dc1f07b7f753d157a0741810b51545f39056511593
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [0.2.2] - 2026-11-22
2
+
3
+ - Add `computed` to define attributes derived from validated input
4
+
1
5
  ## [0.2.1] - 2026-11-22
2
6
 
3
7
  - Move internals to `GuideRail::Factory`. The extending class has only the DSL, `create_class!` and `from` as class methods
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
- ### extendable Data class
102
+ ### Computed attributes
83
103
 
84
- You can give a block to Creator class, the generated Data class can be extended by the block ( applying to `define` class method ).
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
- This can be used to define decorator methods and conversion processes, so the generated Data class can also be used as a so-called ViewModel.
106
+ `acc` does not contain results of other computed attributes.
87
107
 
88
108
  ```ruby
89
- OptionalSchema = Dry::Schema.define do
90
- optional(:name).filled(:string)
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 OptionalAndYieldAccepter
116
+ class UserView
94
117
  extend GuideRail
95
118
 
96
- class_name :ExtendedData
97
- schema OptionalSchema
98
- renderable true
119
+ schema UserSchema
120
+ class_name :User
99
121
 
100
- yield_block do
101
- alias_method :name_orig, :name
102
- define_method :name do
103
- "decorated #{name_orig}"
104
- end
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
- OptionalAndYieldAccepter.from({}).name.start_with? "decorated" # => true
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
- ### Choosing a schema
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
- Use `Dry::Schema.define` for typed data such as hand-made Hashes or ActiveModel. Values are not coerced, and the Data object has the input values as is.
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
- AgeSchema = Dry::Schema.Params do
121
- required(:age).filled(:integer)
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 AgeCreator
145
+ class NameView
125
146
  extend GuideRail
126
147
 
127
- schema AgeSchema
128
- class_name :Age
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
- AgeCreator.from("age" => "20") # => #<data Age age=20>
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
@@ -7,11 +7,11 @@ class GuideRail::Factory
7
7
  # @param [String|Symbol] klass
8
8
  #
9
9
  def class_name(klass)
10
- @_class = klass
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 @_class
55
+ Object.const_get @_class_name
46
56
  rescue NameError
47
- @_attrs = @_schema.key_map.to_a.map { |k| k.name.to_sym }
48
- klass = Data.define(*@_attrs, &@_block)
49
- Object.const_set(@_class, klass)
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 @_class
72
+ Object.const_get @_class_name
62
73
 
63
74
  if result.success?
64
- values = result.to_h
65
- new_value = Object.const_get(@_class).new(
66
- *(
67
- if @_renderable
68
- to_renderable(values)
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]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GuideRail
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
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.1
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-21 00:00:00.000000000 Z
11
+ date: 2026-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-schema