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 +4 -4
- data/lib/opt_struct/class_methods.rb +30 -78
- data/lib/opt_struct/instance_methods.rb +1 -1
- data/lib/opt_struct/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b76ee40e5f6c8f06f6f80efdefa9a595b48f4d07a3c98d6830fd515133038399
|
4
|
+
data.tar.gz: a9643c5ee83f52613f47e6e079445cc5912853ffb17b195f00b3e6496b2cfc03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
5
|
-
|
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
|
-
[]
|
11
|
+
@required_keys ||= []
|
12
12
|
end
|
13
13
|
|
14
14
|
def required(*keys, **options)
|
15
|
-
|
15
|
+
required_keys.concat keys
|
16
16
|
option_accessor *keys, **options
|
17
17
|
end
|
18
18
|
|
19
|
-
def option_reader(*keys, **
|
19
|
+
def option_reader(*keys, **opts)
|
20
20
|
keys.each do |key|
|
21
|
-
|
22
|
-
|
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, **
|
26
|
+
def option_writer(*keys, **opts)
|
30
27
|
keys.each do |key|
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
47
|
-
|
48
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
94
|
-
|
95
|
-
|
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
|
102
|
-
|
103
|
-
const_set(:SHAREABLE, true)
|
88
|
+
def all_callbacks
|
89
|
+
@_callbacks
|
104
90
|
end
|
105
91
|
|
106
92
|
private
|
107
93
|
|
108
|
-
|
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
|
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
|
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.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-
|
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:
|