opt_struct 1.6.0 → 1.7.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/README.md +11 -14
- data/lib/opt_struct/instance_methods.rb +4 -0
- data/lib/opt_struct/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85c2fa0b44b80b18c83e3192538d13a93acbf9b4b33ca12ffd833daec925680d
|
4
|
+
data.tar.gz: 499734e72e8227e5998dfec858242e3229d332a4d885a3aeda0607418d930971
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26dd17e556704a4289070db192c387b33985e530d3bb03fee4fd16cbfbe45dbe0fe5051c462ee40a79e9c5c2140f07b4868677fa31b93e2c67025fd470a534f0
|
7
|
+
data.tar.gz: 8c582de7a5ac0bfbcc7d9b1d237cc74952803efe7864ecab39e0cd0f5cc4796bac7f23e0e742fe3c1e7c90ae216f3797e315385007986647ea85c7078f71e388
|
data/README.md
CHANGED
@@ -1,7 +1,4 @@
|
|
1
|
-
# The Opt Struct
|
2
|
-
|
3
|
-
[travis-image]: https://travis-ci.org/carlzulauf/opt_struct.svg?branch=master
|
4
|
-
[travis-link]: http://travis-ci.org/carlzulauf/opt_struct
|
1
|
+
# The Opt Struct
|
5
2
|
|
6
3
|
A struct around a hash. Great for encapsulating actions with complex configuration, like interactor/action classes.
|
7
4
|
|
@@ -17,7 +14,7 @@ gem "opt_struct"
|
|
17
14
|
class User < OptStruct.new
|
18
15
|
required :email, :name
|
19
16
|
option :role, default: "member"
|
20
|
-
|
17
|
+
|
21
18
|
def formatted_email
|
22
19
|
%{"#{name}" <#{email}>}
|
23
20
|
end
|
@@ -156,7 +153,7 @@ Passing a Hash to `.new` or `.build` is equivalent to passing the same hash to `
|
|
156
153
|
User = OptStruct.new(email: nil, role: "member")
|
157
154
|
```
|
158
155
|
|
159
|
-
Default blocks can also be used and are late evaluated
|
156
|
+
Default blocks can also be used and are late evaluated within the struct instance.
|
160
157
|
|
161
158
|
```ruby
|
162
159
|
class User < OptStruct.new
|
@@ -175,9 +172,9 @@ end
|
|
175
172
|
class User < OptStruct.new
|
176
173
|
option :email, nil
|
177
174
|
option :role, -> { default_role }
|
178
|
-
|
175
|
+
|
179
176
|
private
|
180
|
-
|
177
|
+
|
181
178
|
def default_role
|
182
179
|
"member"
|
183
180
|
end
|
@@ -189,7 +186,7 @@ Default symbols are treated as method calls if the struct `#respond_to?` the met
|
|
189
186
|
```ruby
|
190
187
|
class User < OptStruct.new
|
191
188
|
options :email, :role => :default_role
|
192
|
-
|
189
|
+
|
193
190
|
def default_role
|
194
191
|
"member"
|
195
192
|
end
|
@@ -268,7 +265,7 @@ end
|
|
268
265
|
|
269
266
|
Expected arguments are similar to required arguments, except they are in `.expected_arguments` collection, which is checked when an OptStruct is initialized.
|
270
267
|
|
271
|
-
Expected arguments can also be supplied using keywords. An `ArgumentError` is only raised if the expected argument is not in the list of arguments passed to `OptStruct#new` **and** the argument is not present in the
|
268
|
+
Expected arguments can also be supplied using keywords. An `ArgumentError` is only raised if the expected argument is not in the list of arguments passed to `OptStruct#new` **and** the argument is not present in the optional Hash passed to `OptStruct#new`.
|
272
269
|
|
273
270
|
The following examples will initialize any of the `User` class examples above without error.
|
274
271
|
|
@@ -294,7 +291,7 @@ Feel free to write your own accessor methods for things like dependent options o
|
|
294
291
|
class Person < OptStruct.new
|
295
292
|
option :given_name
|
296
293
|
option :family_name
|
297
|
-
|
294
|
+
|
298
295
|
def name
|
299
296
|
options.fetch(:name) { "#{given_name} #{family_name}" }
|
300
297
|
end
|
@@ -310,7 +307,7 @@ OptStruct classes are initialized in an `initialize` method (in `OptStruct::Inst
|
|
310
307
|
```ruby
|
311
308
|
class UserReportBuilder < OptStruct.new(:user)
|
312
309
|
attr_reader :report
|
313
|
-
|
310
|
+
|
314
311
|
def initialize(*)
|
315
312
|
super
|
316
313
|
@report = []
|
@@ -318,7 +315,7 @@ class UserReportBuilder < OptStruct.new(:user)
|
|
318
315
|
end
|
319
316
|
```
|
320
317
|
|
321
|
-
`OptStruct` also provides initialization callbacks to make hooking into and customizing the initialization of OptStruct classes easier and require less code.
|
318
|
+
`OptStruct` also provides initialization callbacks to make hooking into and customizing the initialization of OptStruct classes easier, less brittle, and require less code.
|
322
319
|
|
323
320
|
```ruby
|
324
321
|
class UserReportBuilder < OptStruct.new(:user)
|
@@ -330,7 +327,7 @@ end
|
|
330
327
|
```ruby
|
331
328
|
class UserReportBuilder < OptStruct.new(:user)
|
332
329
|
attr_reader :report
|
333
|
-
|
330
|
+
|
334
331
|
around_init do |instance|
|
335
332
|
instance.call
|
336
333
|
@report = []
|
data/lib/opt_struct/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opt_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carl Zulauf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-07-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Struct with support for keyword params and mixin support
|
14
14
|
email:
|
@@ -43,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
43
|
- !ruby/object:Gem::Version
|
44
44
|
version: '0'
|
45
45
|
requirements: []
|
46
|
-
rubygems_version: 3.
|
46
|
+
rubygems_version: 3.5.22
|
47
47
|
signing_key:
|
48
48
|
specification_version: 4
|
49
49
|
summary: The Option Struct
|