opt_struct 1.6.0 → 1.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e02d02b33fd0c94b79f6e08079fdd1df45deb62fae647d764fd72d6703b6eb00
4
- data.tar.gz: '06682687240b5cd22eddb32f0ca2b5682eee3c701f96b769b77dc9343df41463'
3
+ metadata.gz: 4c47c60bff407dea977e1408716b9d47e0f4b05a16128734601ce2f6e4fbbe50
4
+ data.tar.gz: 10a2da04669533bafa82384ce79a43083202e0692a93956ef731efc0c5932963
5
5
  SHA512:
6
- metadata.gz: 43728c868888b3f3506ae75c49a25c01b67bb8e2ff49f58f0944fdda7d37549f9f3f33bf88a6b3fde6ff8ed1add1ea846fbd40439e19c0c4e46384379bc44d9a
7
- data.tar.gz: b6546f89c3d8b82167afb694eb2589db67c431d77af45cf6874b4d87d9907a5944fb87088ff7e57e3a899a9cc5594bda50e4fc71ed91181a7d40e883a2b43324
6
+ metadata.gz: 344f1d25a46748a08c60650f706f4f50697324e6985c6d0c6d57aeb1d0de3cb1ec36f82313aac2d5eaf1733d116ed549818b1d9aa472d725cec88c2209b8e5b8
7
+ data.tar.gz: c763bced17752180653844b5d6893f477542fe913eceea3d0be084d7a78d1e8d23d94287302f494a743a1da502f04bf86746f0c1e94d9468fd50153c82377133
data/README.md CHANGED
@@ -1,7 +1,4 @@
1
- # The Opt Struct [![Build Status][travis-image]][travis-link]
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 on the each struct instance.
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 `#options` Hash.
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 = []
@@ -348,3 +345,7 @@ Available callbacks
348
345
  ## Inheritance, Expanded
349
346
 
350
347
  See `spec/inheritance_spec.rb` for examples of just how crazy you can get.
348
+
349
+ ## Ruby Versions
350
+
351
+ This gem is expected to work with rubies as old as ~2.0, though official support and automated testing only covers non-EOL (End of Life) versions. Currently, that's ruby 3.3+. Tests are also performed against jruby and truffleruby to ensure portability.
@@ -59,9 +59,14 @@ module OptStruct
59
59
  option_writer *keys, **options
60
60
  end
61
61
 
62
- def option(key, default = OptStruct::DEFAULT, required: false, **options)
63
- default = options[:default] if options.key?(:default)
64
- defaults[key] = default unless default == OptStruct::DEFAULT
62
+ def option(key, default = OptStruct::DEFAULT, required: false, **options, &default_block)
63
+ if options.key?(:default)
64
+ defaults[key] = options[:default]
65
+ elsif default != OptStruct::DEFAULT
66
+ defaults[key] = default
67
+ elsif default_block
68
+ defaults[key] = default_block
69
+ end
65
70
  required_keys << key if required
66
71
  option_accessor key, **options
67
72
  end
@@ -17,6 +17,10 @@ module OptStruct
17
17
  self.class.defaults
18
18
  end
19
19
 
20
+ def ==(other)
21
+ options == other.options
22
+ end
23
+
20
24
  private
21
25
 
22
26
  def check_required_keys
@@ -32,7 +36,11 @@ module OptStruct
32
36
  options[key] =
33
37
  case default_value
34
38
  when Proc
35
- instance_exec(&default_value)
39
+ if default_value.arity == 0
40
+ instance_exec(&default_value)
41
+ else
42
+ instance_exec(self, &default_value)
43
+ end
36
44
  when Symbol
37
45
  respond_to?(default_value, true) ? send(default_value) : default_value
38
46
  else
@@ -1,3 +1,3 @@
1
1
  module OptStruct
2
- VERSION = "1.6.0"
2
+ VERSION = "1.8.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opt_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Zulauf
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-01-01 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Struct with support for keyword params and mixin support
14
13
  email:
@@ -28,7 +27,6 @@ homepage: https://github.com/carlzulauf/opt_struct
28
27
  licenses:
29
28
  - MIT
30
29
  metadata: {}
31
- post_install_message:
32
30
  rdoc_options: []
33
31
  require_paths:
34
32
  - lib
@@ -43,8 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
41
  - !ruby/object:Gem::Version
44
42
  version: '0'
45
43
  requirements: []
46
- rubygems_version: 3.4.22
47
- signing_key:
44
+ rubygems_version: 3.7.2
48
45
  specification_version: 4
49
46
  summary: The Option Struct
50
47
  test_files: []