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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea70c4d286339f58ec30b1c77ae5ecc1579c52ec511d17aed9ba7afb82b68e36
4
- data.tar.gz: 2c0209b85f1175c3f2463b7b8c821eaa30829c88c10ea2b354d5b656079c7391
3
+ metadata.gz: 23e7e6597fb4e286a35c06290cce1efbc90421cf7b9272e6927060ec7afa1836
4
+ data.tar.gz: 1d85c66ceaa4402ba2b2132cf0659c45e8d28aafa99ec9297a925ecee2c1c860
5
5
  SHA512:
6
- metadata.gz: 3f118fe1853440ed1f5fe649851f7801000cd81787022c5ae0cafdab688330a8a3ac452f82321810dbb9906ecbc68b1b8eb5c763d0e9a256a290d59840ca470b
7
- data.tar.gz: b4bfbb7e38542cf36bd28503612e8cc4305e3120328e5b13df0305c24d4325f4f8bad71ebf63d879b497dcaff223537b662c3645c80a09dfeaa4e31a110eea43
6
+ metadata.gz: 5100e330ab5256cdc3bc7449543265f6334034be7876370f0f007b3cc5f51f96dd5ad99e98efa5d7a75029fc7a01487d2285b9294ea180cfb6329abf808085e1
7
+ data.tar.gz: c09be2d8cbc5188d80c82ebd693530ee9cc51e12018f08ea388d662ce9fd93a8d27b3e2b629094e089eb3371c16a11a2fdb3cf099ff1a561749ee83e1f16fe07
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
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
+
1
6
  ## [1.0.1] - 2023-04-28
2
7
 
3
8
  - Added initializer callbacks.
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
- kwargs = kwargs.symbolize_keys
15
- vcdry_parse_keywords(**kwargs)
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 vcdry_parse_keywords(**kwargs)
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, :remove_keyword, :strict_keywords, to: :vcdry
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
@@ -1,3 +1,3 @@
1
1
  module VCDry
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.1"
3
3
  end
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.0.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-04-28 00:00:00.000000000 Z
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