vcdry 1.0.0 → 1.1.1
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/CHANGELOG.md +12 -1
- data/README.md +46 -0
- data/lib/vcdry/config.rb +8 -1
- data/lib/vcdry/core.rb +59 -0
- data/lib/vcdry/dsl.rb +14 -47
- data/lib/vcdry/registry.rb +1 -1
- data/lib/vcdry/version.rb +1 -1
- data/lib/vcdry.rb +2 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23e7e6597fb4e286a35c06290cce1efbc90421cf7b9272e6927060ec7afa1836
|
4
|
+
data.tar.gz: 1d85c66ceaa4402ba2b2132cf0659c45e8d28aafa99ec9297a925ecee2c1c860
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5100e330ab5256cdc3bc7449543265f6334034be7876370f0f007b3cc5f51f96dd5ad99e98efa5d7a75029fc7a01487d2285b9294ea180cfb6329abf808085e1
|
7
|
+
data.tar.gz: c09be2d8cbc5188d80c82ebd693530ee9cc51e12018f08ea388d662ce9fd93a8d27b3e2b629094e089eb3371c16a11a2fdb3cf099ff1a561749ee83e1f16fe07
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,15 @@
|
|
1
|
-
## [
|
1
|
+
## [1.1.1] - 2023-05-25
|
2
|
+
|
3
|
+
- Breakout `VCDry::Core` from `VCDry::DSL` to better support mixing in `keyword`
|
4
|
+
behavior in other gems like `vcfb`.
|
5
|
+
|
6
|
+
## [1.0.1] - 2023-04-28
|
7
|
+
|
8
|
+
- Added initializer callbacks.
|
9
|
+
- Treat `default: nil` and `optional: true` identically.
|
10
|
+
- Fix for `other_keywords` to accept the optional type as intended.
|
11
|
+
- Fix for inheritance issues when including VCDry::DSL on a parent component.
|
12
|
+
- Fix to allow passing `nil` to an optional keyword.
|
2
13
|
|
3
14
|
## [1.0.0] - 2023-04-16
|
4
15
|
|
data/README.md
CHANGED
@@ -40,6 +40,7 @@ end
|
|
40
40
|
- [other_keywords](#other_keywords)
|
41
41
|
- [strict_keywords](#strict_keywords)
|
42
42
|
- [remove_keyword](#remove_keyword)
|
43
|
+
- [Callbacks](#callbacks)
|
43
44
|
- [Types](#types)
|
44
45
|
- [Development](#development)
|
45
46
|
- [Contributing](#contributing)
|
@@ -260,6 +261,25 @@ class MyOtherComponent < MyComponent
|
|
260
261
|
end
|
261
262
|
```
|
262
263
|
|
264
|
+
### Callbacks
|
265
|
+
|
266
|
+
Including `VCDry::DSL` adds support through `ActiveModel::Callbacks` for the
|
267
|
+
`before_initialize`, `after_initialization`, and `around_initialize` callbacks
|
268
|
+
to minimize the need to override the initlialize method.
|
269
|
+
|
270
|
+
```ruby
|
271
|
+
class MyComponent < MyComponent
|
272
|
+
before_initialize ->() { @links = [] }
|
273
|
+
after_intialize :some_method
|
274
|
+
|
275
|
+
private
|
276
|
+
|
277
|
+
def some_method
|
278
|
+
# do something fancy
|
279
|
+
end
|
280
|
+
end
|
281
|
+
```
|
282
|
+
|
263
283
|
## Types
|
264
284
|
|
265
285
|
The following types are built-in to `vcdry`.
|
@@ -280,6 +300,32 @@ VCDry::Types.add_type(:boolean, ->(value) { ActiveRecord::Type::Boolean.new.cast
|
|
280
300
|
VCDry::Types.add_type(:custom_hash, ->(value) { CustomHash.new(value) })
|
281
301
|
```
|
282
302
|
|
303
|
+
## Mix-in Behavior
|
304
|
+
|
305
|
+
To mix-in the `keyword` behavior without using the `VCDry::DSL` (and its
|
306
|
+
initialize method), you can call include `VCDry::Core` instead and then call
|
307
|
+
`vcdry_parse_keywords` against the hash you wish to parse out keywords from.
|
308
|
+
|
309
|
+
The `vcdry_parse_keywords` accepts a hash and returns a hash of all key/value
|
310
|
+
pairs that were not pulled out as a `keyword`.
|
311
|
+
|
312
|
+
> **Note**: Including `VCDry::Core` does not include support for
|
313
|
+
> `other_keywords`, `strict_keywords`, or enable support for callbacks.
|
314
|
+
|
315
|
+
```ruby
|
316
|
+
class HeadingComponent
|
317
|
+
include VCDry::Core
|
318
|
+
|
319
|
+
keyword :size, :symbol, default: :md
|
320
|
+
other_keywords :options
|
321
|
+
|
322
|
+
def initialize(text, **options)
|
323
|
+
@text = text
|
324
|
+
@options = vcdry_parse_keywords(options)
|
325
|
+
end
|
326
|
+
end
|
327
|
+
```
|
328
|
+
|
283
329
|
## Development
|
284
330
|
|
285
331
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
data/lib/vcdry/config.rb
CHANGED
@@ -13,6 +13,12 @@ module VCDry
|
|
13
13
|
@name = name.to_sym
|
14
14
|
@type = (type.nil? || type.respond_to?(:call)) ? type : VCDry::Types[type]
|
15
15
|
@options = options
|
16
|
+
|
17
|
+
# Translate default: nil to optional: true
|
18
|
+
if @options.key?(:default) && @options[:default].nil?
|
19
|
+
@options.delete(:default)
|
20
|
+
@options[:optional] = true
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
def array?
|
@@ -48,7 +54,7 @@ module VCDry
|
|
48
54
|
end
|
49
55
|
|
50
56
|
def optional?
|
51
|
-
default? ||
|
57
|
+
default? || @options[:optional]
|
52
58
|
end
|
53
59
|
|
54
60
|
def required?
|
@@ -72,6 +78,7 @@ module VCDry
|
|
72
78
|
def type_cast_value(value)
|
73
79
|
return if value == NOT_DEFINED
|
74
80
|
return value if @type.nil?
|
81
|
+
return if value.nil? && @options[:optional]
|
75
82
|
|
76
83
|
value = @type.call(value)
|
77
84
|
if enum? && enum_values.exclude?(value)
|
data/lib/vcdry/core.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
require "active_support/core_ext/hash"
|
3
|
+
require "active_support/core_ext/module/delegation"
|
4
|
+
|
5
|
+
require_relative "error"
|
6
|
+
require_relative "registry"
|
7
|
+
|
8
|
+
module VCDry
|
9
|
+
module Core
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
def vcdry_parse_keywords(kwargs = {})
|
13
|
+
kwargs = kwargs.symbolize_keys
|
14
|
+
self.class.vcdry.keyword_configs.each do |config|
|
15
|
+
if config.required? && !kwargs.key?(config.name)
|
16
|
+
raise MissingRequiredKeywordError.new(config.name)
|
17
|
+
end
|
18
|
+
|
19
|
+
value = kwargs.fetch(config.name, config.default)
|
20
|
+
instance_variable_set(config.instance_variable, config.type_cast(value))
|
21
|
+
end
|
22
|
+
kwargs.except(*self.class.vcdry.keywords)
|
23
|
+
end
|
24
|
+
|
25
|
+
class_methods do
|
26
|
+
delegate :remove_keyword, to: :vcdry
|
27
|
+
|
28
|
+
def inherited(subclass)
|
29
|
+
subclass.instance_variable_set(:@vcdry, @vcdry&.dup)
|
30
|
+
super
|
31
|
+
end
|
32
|
+
|
33
|
+
def keyword(name, type = nil, **options)
|
34
|
+
name = name.to_sym
|
35
|
+
config = vcdry.keyword(name, type, options)
|
36
|
+
vcutils_define_helper_methods(config)
|
37
|
+
end
|
38
|
+
|
39
|
+
def vcdry
|
40
|
+
@vcdry ||= Registry.new
|
41
|
+
end
|
42
|
+
|
43
|
+
def vcutils_define_helper_methods(config)
|
44
|
+
if config.enum?
|
45
|
+
config.enum_values.each do |value|
|
46
|
+
define_method "#{config.name}_#{value}?" do
|
47
|
+
instance_variable_get(config.instance_variable) == value
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
if config.optional?
|
52
|
+
define_method "#{config.name}?" do
|
53
|
+
instance_variable_get(config.instance_variable).present?
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/vcdry/dsl.rb
CHANGED
@@ -1,33 +1,29 @@
|
|
1
|
+
require "active_model/callbacks"
|
1
2
|
require "active_support/concern"
|
2
|
-
require "active_support/core_ext/hash"
|
3
|
-
require "active_support/core_ext/module/delegation"
|
4
3
|
|
4
|
+
require_relative "core"
|
5
5
|
require_relative "error"
|
6
|
-
require_relative "registry"
|
7
6
|
|
8
7
|
module VCDry
|
9
8
|
module DSL
|
10
9
|
extend ActiveSupport::Concern
|
10
|
+
include Core
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
vcdry_parse_keywords(**kwargs)
|
15
|
-
vcdry_parse_unknown_keywords(**kwargs)
|
16
|
-
end
|
12
|
+
included do
|
13
|
+
extend ActiveModel::Callbacks
|
17
14
|
|
18
|
-
|
19
|
-
|
20
|
-
if config.required? && !kwargs.key?(config.name)
|
21
|
-
raise MissingRequiredKeywordError.new(config.name)
|
22
|
-
end
|
15
|
+
define_model_callbacks :initialize
|
16
|
+
end
|
23
17
|
|
24
|
-
|
25
|
-
|
18
|
+
def initialize(**kwargs)
|
19
|
+
run_callbacks :initialize do
|
20
|
+
unknown_kwargs = vcdry_parse_keywords(kwargs)
|
21
|
+
vcdry_parse_unknown_keywords(unknown_kwargs)
|
22
|
+
super
|
26
23
|
end
|
27
24
|
end
|
28
25
|
|
29
|
-
def vcdry_parse_unknown_keywords(
|
30
|
-
unknown_kwargs = kwargs.except(*self.class.vcdry.keywords)
|
26
|
+
def vcdry_parse_unknown_keywords(unknown_kwargs = {})
|
31
27
|
raise UnknownArgumentError.new(*unknown_kwargs.keys) if self.class.vcdry.strict? && unknown_kwargs.present?
|
32
28
|
return unless self.class.vcdry.gather_unknown_keywords?
|
33
29
|
|
@@ -36,36 +32,7 @@ module VCDry
|
|
36
32
|
end
|
37
33
|
|
38
34
|
class_methods do
|
39
|
-
delegate :other_keywords, :
|
40
|
-
|
41
|
-
def inherited(subclass)
|
42
|
-
subclass.instance_variable_set(:@vcdry, @vcdry&.dup)
|
43
|
-
end
|
44
|
-
|
45
|
-
def keyword(name, type = nil, **options)
|
46
|
-
name = name.to_sym
|
47
|
-
config = vcdry.keyword(name, type, options)
|
48
|
-
vcutils_define_helper_methods(config)
|
49
|
-
end
|
50
|
-
|
51
|
-
def vcdry
|
52
|
-
@vcdry ||= Registry.new
|
53
|
-
end
|
54
|
-
|
55
|
-
def vcutils_define_helper_methods(config)
|
56
|
-
if config.enum?
|
57
|
-
config.enum_values.each do |value|
|
58
|
-
define_method "#{config.name}_#{value}?" do
|
59
|
-
instance_variable_get(config.instance_variable) == value
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
if config.optional?
|
64
|
-
define_method "#{config.name}?" do
|
65
|
-
instance_variable_get(config.instance_variable).present?
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
35
|
+
delegate :other_keywords, :strict_keywords, to: :vcdry
|
69
36
|
end
|
70
37
|
end
|
71
38
|
end
|
data/lib/vcdry/registry.rb
CHANGED
data/lib/vcdry/version.rb
CHANGED
data/lib/vcdry.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
+
require_relative "vcdry/config"
|
2
|
+
require_relative "vcdry/core"
|
1
3
|
require_relative "vcdry/dsl"
|
2
4
|
require_relative "vcdry/error"
|
3
|
-
require_relative "vcdry/config"
|
4
5
|
require_relative "vcdry/registry"
|
5
6
|
require_relative "vcdry/types"
|
6
7
|
require_relative "vcdry/version"
|
7
|
-
|
8
|
-
module VCDry
|
9
|
-
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vcdry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Monroe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- Rakefile
|
77
77
|
- lib/vcdry.rb
|
78
78
|
- lib/vcdry/config.rb
|
79
|
+
- lib/vcdry/core.rb
|
79
80
|
- lib/vcdry/dsl.rb
|
80
81
|
- lib/vcdry/error.rb
|
81
82
|
- lib/vcdry/registry.rb
|