opt_struct 1.2.1 → 1.3.0

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: ef6cb2533d486038390ac0975e38f6c0d3e9486c8c213326f48caf58c57dfb09
4
- data.tar.gz: 98c38c235c3e24588f9a3efb7d69ac4e40da9d7d6650ebb1a0aca3c669240f45
3
+ metadata.gz: b76ee40e5f6c8f06f6f80efdefa9a595b48f4d07a3c98d6830fd515133038399
4
+ data.tar.gz: a9643c5ee83f52613f47e6e079445cc5912853ffb17b195f00b3e6496b2cfc03
5
5
  SHA512:
6
- metadata.gz: 3f7346d74b2ddcdef6ff61ba638e794247fbc714c9e796a0f13297fae9d77d7482f452e43f5b2830c54129b3d6cf22f19d8315d75c360d1eb575027325a33337
7
- data.tar.gz: cbeba0f215a42989eb6124fe095d6d6332fe015b844f972ba63aa15b085b0f7df6812751c736867743f86029187b829e44fd9d8aed88d78e8087076e78192e06
6
+ metadata.gz: 5d420f43e4cbc3571545846b6ee5275c32e86e5f79de7d2063564ef63f1cfc2c6857092256ee2d74a05bfce3fec9a9da7b1e5ada92ad50e3ceb69488310e8419
7
+ data.tar.gz: 8b059ca5bfab302e0515c90e49650723174cc91eb42b93d104de4fa79cf86fe6e42aea3bdc50c1612cdef36ae690538e221b9338c683d5851e88e9688c546731
@@ -1,38 +1,33 @@
1
1
  module OptStruct
2
2
  module ClassMethods
3
3
  def inherited(subclass)
4
- opt_struct_class_constants.each do |c|
5
- subclass.const_set(c, const_get(c)) if const_defined?(c)
4
+ instance_variables.each do |v|
5
+ ivar = instance_variable_get(v)
6
+ subclass.send(:instance_variable_set, v, ivar.dup) if ivar
6
7
  end
7
8
  end
8
9
 
9
- # overwritten if `required` is called
10
10
  def required_keys
11
- [].freeze
11
+ @required_keys ||= []
12
12
  end
13
13
 
14
14
  def required(*keys, **options)
15
- add_required_keys *keys
15
+ required_keys.concat keys
16
16
  option_accessor *keys, **options
17
17
  end
18
18
 
19
- def option_reader(*keys, **options)
19
+ def option_reader(*keys, **opts)
20
20
  keys.each do |key|
21
- class_eval <<~RUBY
22
- #{options[:private] ? "private" : ""} def #{key}
23
- options[:#{key}]
24
- end
25
- RUBY
21
+ define_method(key) { options[key] }
22
+ private key if opts[:private]
26
23
  end
27
24
  end
28
25
 
29
- def option_writer(*keys, **options)
26
+ def option_writer(*keys, **opts)
30
27
  keys.each do |key|
31
- class_eval <<~RUBY
32
- #{options[:private] ? "private" : ""} def #{key}=(value)
33
- options[:#{key}] = value
34
- end
35
- RUBY
28
+ meth = "#{key}=".to_sym
29
+ define_method(meth) { |value| options[key] = value }
30
+ private meth if opts[:private]
36
31
  end
37
32
  end
38
33
 
@@ -43,40 +38,34 @@ module OptStruct
43
38
  end
44
39
 
45
40
  def option(key, default = nil, required: false, **options)
46
- default ||= options[:default]
47
- add_defaults key => default if default || options.key?(:default)
48
- add_required_keys key if required
41
+ default = options[:default] if options.key?(:default)
42
+ defaults[key] = default if default
43
+ required_keys << key if required
49
44
  option_accessor key, **options
50
45
  end
51
46
 
52
47
  def options(*keys, **keys_defaults)
53
48
  option_accessor *keys if keys.any?
54
49
  if keys_defaults.any?
55
- add_defaults keys_defaults
50
+ defaults.merge!(keys_defaults)
56
51
  option_accessor *(keys_defaults.keys - expected_arguments)
57
52
  end
58
53
  end
59
54
 
60
55
  def defaults
61
- const_defined?(:OPT_DEFAULTS) ? const_get(:OPT_DEFAULTS) : {}
62
- end
63
-
64
- # overwritten if `expect_arguments` is called
65
- def expected_arguments
66
- [].freeze
56
+ @defaults ||= {}
67
57
  end
68
58
 
69
59
  def expect_arguments(*arguments)
70
60
  required(*arguments)
71
- combined = expected_arguments + arguments
72
- class_eval <<~EVAL
73
- def self.expected_arguments
74
- #{combined.inspect}.freeze
75
- end
76
- EVAL
61
+ expected_arguments.concat(arguments)
77
62
  end
78
63
  alias_method :expect_argument, :expect_arguments
79
64
 
65
+ def expected_arguments
66
+ @expected_arguments ||= []
67
+ end
68
+
80
69
  def init(meth = nil, &blk)
81
70
  add_callback(:init, meth || blk)
82
71
  end
@@ -90,60 +79,23 @@ module OptStruct
90
79
  add_callback(:around_init, meth || blk)
91
80
  end
92
81
 
93
- def all_callbacks
94
- const_defined?(:OPT_CALLBACKS) ? const_get(:OPT_CALLBACKS) : {}.freeze
95
- end
96
-
97
- def shareable?
98
- const_defined?(:SHAREABLE) && const_get(:SHAREABLE)
82
+ def add_callback(name, callback)
83
+ @_callbacks ||= {}
84
+ @_callbacks[name] ||= []
85
+ @_callbacks[name] << callback
99
86
  end
100
87
 
101
- def shareable!
102
- return if shareable?
103
- const_set(:SHAREABLE, true)
88
+ def all_callbacks
89
+ @_callbacks
104
90
  end
105
91
 
106
92
  private
107
93
 
108
- def share(value)
109
- return value unless shareable?
110
- defined?(Ractor) ? Ractor.make_shareable(value) : value
111
- end
112
-
113
- def add_required_keys(*keys)
114
- combined = required_keys + keys
115
- class_eval <<~RUBY
116
- def self.required_keys
117
- #{combined.inspect}.freeze
118
- end
119
- RUBY
120
- end
121
-
122
- def add_defaults(defaults_to_add)
123
- freezer = defaults.dup
124
- defaults_to_add.each { |k, v| freezer[k] = share(v) }
125
- remove_const(:OPT_DEFAULTS) if const_defined?(:OPT_DEFAULTS)
126
- const_set(:OPT_DEFAULTS, freezer.freeze)
127
- end
128
-
129
- def add_callback(name, callback)
130
- if const_defined?(:OPT_CALLBACKS)
131
- callbacks_for_name = (all_callbacks[name] || []) + [callback]
132
- callbacks_hash = all_callbacks.merge(name => callbacks_for_name).freeze
133
- remove_const(:OPT_CALLBACKS)
134
- const_set(:OPT_CALLBACKS, callbacks_hash)
135
- else
136
- const_set(:OPT_CALLBACKS, { name => [ callback ] })
137
- end
138
- end
139
-
140
- def opt_struct_class_constants
141
- [:OPT_DEFAULTS, :OPT_CALLBACKS]
142
- end
94
+ RESERVED_WORDS = %i(class defaults options fetch check_required_args check_required_keys)
143
95
 
144
96
  def check_reserved_words(words)
145
97
  Array(words).each do |word|
146
- if OptStruct::RESERVED_WORDS.member?(word)
98
+ if RESERVED_WORDS.member?(word)
147
99
  raise ArgumentError, "Use of reserved word is not permitted: #{word.inspect}"
148
100
  end
149
101
  end
@@ -28,7 +28,7 @@ module OptStruct
28
28
 
29
29
  def assign_defaults
30
30
  defaults.each do |key, default_value|
31
- next if options.key?(key) # || default_value.nil?
31
+ next if options.key?(key)
32
32
  options[key] = read_default_value(default_value)
33
33
  end
34
34
  end
@@ -1,3 +1,3 @@
1
1
  module OptStruct
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.0"
3
3
  end
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.2.1
4
+ version: 1.3.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: 2021-04-19 00:00:00.000000000 Z
11
+ date: 2021-05-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Struct with support for keyword params and mixin support
14
14
  email: