opt_struct 1.5.0 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29701f52f41c942f328cc7a9303a72baa59d96cc1b0b8d9e803e9a80bac6aef3
4
- data.tar.gz: 91c84c884b3a4ea3833db8c80083217efb8d942983aa0cb1072fd0b177c135b5
3
+ metadata.gz: d997efd436a127ccf51efc19590c55f4671c087e61ed085174a273a723d9fc66
4
+ data.tar.gz: 87f2650397718c6dad69871202ea303de8626c94d98cab01b1e14ce6e78de82e
5
5
  SHA512:
6
- metadata.gz: 27ceba23b7146f571232215d3228c59d9d6632e0628b694ded56fb8f308ad9257182c0f29edf41bbb9dc151570e5b855f40412ad066b33abfe042d18a3342344
7
- data.tar.gz: 74f73309afd32192f9ea1b7d48f193e209f0e056381d20762001f57ec6b113387d11a8e9532a713b28855e2e052f210a8f0cff1df9b1c1319e8701934e9d0687
6
+ metadata.gz: 8ba6acba66bd8d059d3cc210b5102908929458b6665972e7ba3c049fed4eb6ab1031edf987b0e1d30ba61130d72cf0cdd075587319016924445e075cae4e4a83
7
+ data.tar.gz: 9dd91af6395c02a480a9ffe413c1912dba17f30fc7f7e3f175f0b681d485aee8f9cbf91ac136158f6bf82942bd137e3e68082bd9d2d7ac8ba1c2d44e541f7d29
@@ -1,10 +1,19 @@
1
1
  module OptStruct
2
2
  module ClassMethods
3
3
  def inherited(subclass)
4
- instance_variables.each do |v|
5
- ivar = instance_variable_get(v)
6
- subclass.send(:instance_variable_set, v, ivar.dup) if ivar
4
+ # intersection of defined vars and the ones we care about
5
+ (instance_variables & OptStruct::CLASS_IVARS).each do |ivar|
6
+ # copy each to the child class
7
+ value =
8
+ case ivar
9
+ when :@_callbacks # Hash that we need to duplicate deeper
10
+ instance_variable_get(ivar).transform_values(&:dup)
11
+ else
12
+ instance_variable_get(ivar).dup
13
+ end
14
+ subclass.send(:instance_variable_set, ivar, value)
7
15
  end
16
+ super(subclass)
8
17
  end
9
18
 
10
19
  def defined_keys
@@ -23,6 +23,7 @@ module OptStruct
23
23
  expect_arguments
24
24
  ).each do |class_method|
25
25
  define_method(class_method) do |*args, **options|
26
+ @_opt_structs ||= []
26
27
  @_opt_structs << [[], {}, -> { send(class_method, *args, **options) }]
27
28
  end
28
29
  end
@@ -1,3 +1,3 @@
1
1
  module OptStruct
2
- VERSION = "1.5.0"
2
+ VERSION = "1.5.2"
3
3
  end
data/lib/opt_struct.rb CHANGED
@@ -11,15 +11,25 @@ module OptStruct
11
11
  *OptStruct::InstanceMethods.private_instance_methods,
12
12
  ].freeze
13
13
 
14
+ # list of class instance variables defined/tracked by opt_struct
15
+ CLASS_IVARS = %i[
16
+ @defined_keys @required_keys @expected_arguments @defaults
17
+ @_callbacks @_opt_structs
18
+ ]
19
+
14
20
  # Default value object allows us to distinguish unspecified defaults from nil
15
21
  DEFAULT = Object.new
16
22
 
17
23
  def self._inject_struct(target, source, args = [], **defaults, &callback)
18
- structs = Array(source.instance_variable_get(:@_opt_structs)).dup
24
+ existing = source.instance_variable_get(:@_opt_structs)
25
+ structs = Array(existing).dup
26
+
19
27
  if args.any? || defaults.any? || block_given?
20
28
  structs << [args, defaults, callback]
21
29
  end
22
- target.instance_variable_set(:@_opt_structs, structs)
30
+
31
+ target.instance_variable_set(:@_opt_structs, structs) if existing || structs.any?
32
+
23
33
  if target.is_a?(Class)
24
34
  target.instance_exec do
25
35
  extend ClassMethods
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.5.0
4
+ version: 1.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Zulauf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-25 00:00:00.000000000 Z
11
+ date: 2023-08-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Struct with support for keyword params and mixin support
14
14
  email: