mandate 2.1.0 → 2.2.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: 6da8270b314f00464d035de5d4693038ca29de9a55bca052597931c3afaa44b7
4
- data.tar.gz: ebd1428fb13336073a2d4a2d9c32c73b777683b99b69c8941b82f7cb94bd61cf
3
+ metadata.gz: 12d7d983ad9f280460b90e764a105b84b0d25d311852f71a511ea6a9ddd69d06
4
+ data.tar.gz: 2b37f6c50d8165472db2d2c65da490a01a0047862460594a1e95c3d1cb780db6
5
5
  SHA512:
6
- metadata.gz: 5a12088967f35d7469fe02657be46005e7fbb927b34c398bd59eaeeebed92d1ee4e68b87ead2f79712e62c2d699b223bec56314100e2752c23bcbfb5910bced7
7
- data.tar.gz: b3ecb3e7a5cf4f00ca8619570627a451668d05e993cfeea56e3ed0ca2e4f5cf9c6325453695c5b15aa3e279de0d54fe53bbb5fe62c20abf7da62998bf8e94d51
6
+ metadata.gz: 53038261b0697c3ad94eb0240b0d031f5face943123a07493e369068922c8463ef1a95483eb2cec77e44ccd5636d5c35730a89ec32afeb712826d8249dd948ff
7
+ data.tar.gz: be465cc6fc64837235ec99ab7b1c071da90978611497138a3ed481eec07d3d987eae5da31b9973b65f99122bf8e44530f2db41cd00a639f9e5669c4253dfd879
data/README.md CHANGED
@@ -51,6 +51,7 @@ Multiplies.(20, 3)
51
51
 
52
52
  The `initialize_with` method creates an initializer and private attr_readers for the specified variables.
53
53
  Keyword arguments can be expressed normally, but arguments without default values must be specified with the value `Mandate::NO_DEFAULT`.
54
+ You may also use the `Mandate::KWARGS` param to capture any params that aren't explicitely set.
54
55
  The call also takes a block, which is run after the variables are set.
55
56
 
56
57
  For example, this...
@@ -59,7 +60,7 @@ For example, this...
59
60
  MODELS = ...
60
61
 
61
62
  class Car
62
- initialize_with :model, color: "blue", owner: Mandate::NO_DEFAULT do
63
+ initialize_with :model, color: "blue", owner: Mandate::NO_DEFAULT, params: Mandate::KWARGS do
63
64
  raise unless MODELS[model].colors.include?(color)
64
65
  end
65
66
  end
@@ -71,10 +72,11 @@ end
71
72
  MODELS = ...
72
73
 
73
74
  class Foobar
74
- def initialize(model, color: "blue", owner: )
75
+ def initialize(model, color: "blue", owner:, params = {})
75
76
  @model = model
76
77
  @color = color
77
78
  @owner = owner
79
+ @params = params
78
80
 
79
81
  raise unless MODELS[model].colors.include?(color)
80
82
  end
@@ -2,11 +2,14 @@ require 'securerandom'
2
2
 
3
3
  module Mandate
4
4
  NO_DEFAULT = SecureRandom.uuid
5
+ KWARGS = SecureRandom.uuid
5
6
 
6
7
  module InitializerInjector
7
8
  def self.extended(base)
8
9
  class << base
9
10
  def initialize_with(*attrs, **kwattrs, &block)
11
+ kwarg_capture_key = (kwattrs.find(-> { [] }) { |_name, value| value == KWARGS }).first
12
+
10
13
  define_method :initialize do |*args, **kwargs|
11
14
  unless args.length == attrs.length
12
15
  raise ArgumentError, "wrong number of arguments (given #{args.length}, expected #{attrs.length})"
@@ -16,10 +19,15 @@ module Mandate
16
19
  instance_variable_set("@#{attr}", arg)
17
20
  end
18
21
 
22
+ instance_variable_set("@#{kwarg_capture_key}", {}) if kwarg_capture_key
19
23
  kwargs.each do |name, value|
20
- raise ArgumentError, "unknown keyword: #{name}" unless kwattrs.key?(name)
21
-
22
- instance_variable_set("@#{name}", value)
24
+ if kwattrs.key?(name)
25
+ instance_variable_set("@#{name}", value)
26
+ elsif kwarg_capture_key
27
+ instance_variable_get("@#{kwarg_capture_key}")[name] = value
28
+ else
29
+ raise ArgumentError, "unknown keyword: #{name}"
30
+ end
23
31
  end
24
32
 
25
33
  kwattrs.each do |key, value|
@@ -1,3 +1,3 @@
1
1
  module Mandate
2
- VERSION = "2.1.0".freeze
2
+ VERSION = "2.2.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mandate
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Walker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-18 00:00:00.000000000 Z
11
+ date: 2022-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler