guide-rail 0.2.5 → 0.2.6

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: 68342bb92a1b43e2cfbbb015595332832d105e98fe86a05b57ffd084339d0940
4
- data.tar.gz: 37736378ff24a0bef3dfe79ffb0c63a7d7ed86272da7306a60eefc76c78db6a7
3
+ metadata.gz: eb2e0cd8efa0344e283e3595e8df20e6512b27d97f4eb5ed1922a67b7a7bf91a
4
+ data.tar.gz: 7e468276f145c64e970995513e322a62a52d4687881078db6a0bb0d83b23846c
5
5
  SHA512:
6
- metadata.gz: 35fe93962c60b50699ef50678d0b156ade74e7a3fbeb2aac7ef760828790a7ab1e509409f63a9ab9ce7da0707ac9d8772f5a6010f9530d45aadd7431fd763aac
7
- data.tar.gz: 7c3d653488dd0ed9f28ad689a71d65c55af3b9d902b0d5c0e11c2c69416516b280ff3ead4360146f49e85adc58cb4e35215a822bfd90013fa7a5cff891832e34
6
+ metadata.gz: aec2c1c7e698783a7bd188bea69cf9d893b96b71bd226e4672fcac4652164b180f726d00a030749ea8dc47bda427aae14c1f9bc39d23adc49f2bf0572c6c15eb
7
+ data.tar.gz: bdbd5f6517a2d46b5c62493a1f87a8e80c393b88796537559086c0115c3afb6dd2d3d30581a653255fe2a02dc751c92ce9bc3a033e49ba4675fc9c5462db2c78
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [0.2.6] - 2026-11-23
2
+
3
+ No change in the code from 0.2.5. Only README, CHANGELOG and the version differ in the package.
4
+
5
+ - README: add techniques of `computed`
6
+ - share a method among computed attributes, as a class method of the extending class
7
+ - add or replace values in a nested Hash, by returning a new Hash that replaces the whole attribute
8
+ - Restructure tests by schema and input patterns. No change in behavior
9
+
1
10
  ## [0.2.5] - 2026-11-22
2
11
 
3
12
  - Fix `renderable` with `computed` overwriting a nested schema attr. When the computed returns `nil`, the default of the schema shape ( e.g. `{lat: "", lon: ""}` ) is set instead of `""`
data/README.md CHANGED
@@ -134,6 +134,73 @@ UserView.from(first_name: "Jane", last_name: "Doe", role: "admin", status: "memb
134
134
  # => #<data User first_name="Jane", last_name="Doe", role="admin", status="owner", display_name="Jane Doe">
135
135
  ```
136
136
 
137
+ #### Sharing a method among computed attributes
138
+
139
+ There is no dedicated DSL for sharing logic, but the block of `computed` is a closure evaluated with `self` as the class that extends GuideRail, so it can call class methods of the class. A class method can be used from multiple computed attributes.
140
+
141
+ ```ruby
142
+ GreetingSchema = Dry::Schema.define do
143
+ required(:first_name).filled(:string)
144
+ required(:last_name).filled(:string)
145
+ end
146
+
147
+ class GreetingView
148
+ extend GuideRail
149
+
150
+ schema GreetingSchema
151
+ class_name :Greeting
152
+
153
+ def self.display_name_of(acc) = "#{acc.first_name} #{acc.last_name}"
154
+
155
+ computed :display_name do |acc|
156
+ display_name_of(acc)
157
+ end
158
+
159
+ computed :message do |acc|
160
+ "Hello, #{display_name_of(acc)}"
161
+ end
162
+ end
163
+
164
+ GreetingView.from(first_name: "Jane", last_name: "Doe")
165
+ # => #<data Greeting first_name="Jane", last_name="Doe", display_name="Jane Doe", message="Hello, Jane Doe">
166
+ ```
167
+
168
+ Both are computed attributes, so `renderable`, `to_h`, pattern matching and freezing apply to them. The method runs once per computed that calls it.
169
+
170
+ If the value does not need to be an attribute, a method in [`yield_block`](#extending-the-data-class) can read other attributes as well. Note that it is not a member ( not in `to_h` or pattern matching ), and with `renderable` it reads the output values ( `""` instead of `nil` ) and its own `nil` is not converted.
171
+
172
+ #### Adding or replacing values in a nested Hash
173
+
174
+ Overwriting works for a nested Hash attribute as well. Return a new Hash ( e.g. by `Hash#merge` ) to add a key or replace only some of its values. `acc` itself is not changed.
175
+
176
+ ```ruby
177
+ LocationSchema = Dry::Schema.Params do
178
+ required(:addr).filled(:string)
179
+ required(:location).maybe do
180
+ hash do
181
+ required(:lat).maybe(:float)
182
+ required(:lon).maybe(:float)
183
+ end
184
+ end
185
+ end
186
+
187
+ class PlaceView
188
+ extend GuideRail
189
+
190
+ schema LocationSchema
191
+ class_name :Place
192
+
193
+ computed :location do |acc|
194
+ acc.location&.merge(label: acc.addr, lat: acc.location[:lat]&.round(1))
195
+ end
196
+ end
197
+
198
+ PlaceView.from(addr: "Tokyo", location: {lat: "35.6812", lon: "139.7671"})
199
+ # => #<data Place addr="Tokyo", location={lat: 35.7, lon: 139.7671, label: "Tokyo"}>
200
+ ```
201
+
202
+ With `renderable`, use 0.2.5 or later. When the computed returns `nil`, the default of the schema shape ( `{lat: "", lon: ""}` ) is set. Before 0.2.5, it was `""`.
203
+
137
204
  ### Collection input
138
205
 
139
206
  `from_collection` applies `from` to each element of a collection ( anything that responds to `to_ary`, e.g. Array ) and returns the results in the same order. Each element succeeds or fails on its own. With `monadic true` ( see [Monadic mode](#monadic-mode) ), each element is a `Dry::Monads::Result`.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GuideRail
4
- VERSION = "0.2.5"
4
+ VERSION = "0.2.6"
5
5
  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.5
4
+ version: 0.2.6
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-22 00:00:00.000000000 Z
11
+ date: 2026-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-schema