vcdry 1.0.1 → 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 +5 -0
- data/README.md +26 -0
- data/lib/vcdry/core.rb +59 -0
- data/lib/vcdry/dsl.rb +13 -56
- 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
data/README.md
CHANGED
@@ -300,6 +300,32 @@ VCDry::Types.add_type(:boolean, ->(value) { ActiveRecord::Type::Boolean.new.cast
|
|
300
300
|
VCDry::Types.add_type(:custom_hash, ->(value) { CustomHash.new(value) })
|
301
301
|
```
|
302
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
|
+
|
303
329
|
## Development
|
304
330
|
|
305
331
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
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,36 +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
|
+
|
12
|
+
included do
|
13
|
+
extend ActiveModel::Callbacks
|
14
|
+
|
15
|
+
define_model_callbacks :initialize
|
16
|
+
end
|
11
17
|
|
12
18
|
def initialize(**kwargs)
|
13
19
|
run_callbacks :initialize do
|
14
|
-
|
15
|
-
|
16
|
-
vcdry_parse_unknown_keywords(**kwargs)
|
20
|
+
unknown_kwargs = vcdry_parse_keywords(kwargs)
|
21
|
+
vcdry_parse_unknown_keywords(unknown_kwargs)
|
17
22
|
super
|
18
23
|
end
|
19
24
|
end
|
20
25
|
|
21
|
-
def
|
22
|
-
self.class.vcdry.keyword_configs.each do |config|
|
23
|
-
if config.required? && !kwargs.key?(config.name)
|
24
|
-
raise MissingRequiredKeywordError.new(config.name)
|
25
|
-
end
|
26
|
-
|
27
|
-
value = kwargs.fetch(config.name, config.default)
|
28
|
-
instance_variable_set(config.instance_variable, config.type_cast(value))
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def vcdry_parse_unknown_keywords(**kwargs)
|
33
|
-
unknown_kwargs = kwargs.except(*self.class.vcdry.keywords)
|
26
|
+
def vcdry_parse_unknown_keywords(unknown_kwargs = {})
|
34
27
|
raise UnknownArgumentError.new(*unknown_kwargs.keys) if self.class.vcdry.strict? && unknown_kwargs.present?
|
35
28
|
return unless self.class.vcdry.gather_unknown_keywords?
|
36
29
|
|
@@ -38,44 +31,8 @@ module VCDry
|
|
38
31
|
instance_variable_set(config.instance_variable, config.type_cast(unknown_kwargs))
|
39
32
|
end
|
40
33
|
|
41
|
-
included do
|
42
|
-
extend ActiveModel::Callbacks
|
43
|
-
|
44
|
-
define_model_callbacks :initialize
|
45
|
-
end
|
46
|
-
|
47
34
|
class_methods do
|
48
|
-
delegate :other_keywords, :
|
49
|
-
|
50
|
-
def inherited(subclass)
|
51
|
-
subclass.instance_variable_set(:@vcdry, @vcdry&.dup)
|
52
|
-
super
|
53
|
-
end
|
54
|
-
|
55
|
-
def keyword(name, type = nil, **options)
|
56
|
-
name = name.to_sym
|
57
|
-
config = vcdry.keyword(name, type, options)
|
58
|
-
vcutils_define_helper_methods(config)
|
59
|
-
end
|
60
|
-
|
61
|
-
def vcdry
|
62
|
-
@vcdry ||= Registry.new
|
63
|
-
end
|
64
|
-
|
65
|
-
def vcutils_define_helper_methods(config)
|
66
|
-
if config.enum?
|
67
|
-
config.enum_values.each do |value|
|
68
|
-
define_method "#{config.name}_#{value}?" do
|
69
|
-
instance_variable_get(config.instance_variable) == value
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
if config.optional?
|
74
|
-
define_method "#{config.name}?" do
|
75
|
-
instance_variable_get(config.instance_variable).present?
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
35
|
+
delegate :other_keywords, :strict_keywords, to: :vcdry
|
79
36
|
end
|
80
37
|
end
|
81
38
|
end
|
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
|