mandate 2.0.0 → 2.1.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: 1c40c3c78438f78715fd0ada9e47b9e9e3b9c9245d43bf29624d486b568852ab
4
- data.tar.gz: 8954343effdf23246d4d6359c9792ea12720a1573a41ff35fec8ff3a21dea564
3
+ metadata.gz: 6da8270b314f00464d035de5d4693038ca29de9a55bca052597931c3afaa44b7
4
+ data.tar.gz: ebd1428fb13336073a2d4a2d9c32c73b777683b99b69c8941b82f7cb94bd61cf
5
5
  SHA512:
6
- metadata.gz: 63abfab520970461986313ecf404becb3e3689ef62ddc57ffd1d90c4b91440ec211c1335744c1a700ba6602fab5303eca2109b8bd8e7c1c04bd2b82a26ff5fac
7
- data.tar.gz: fde32f5e6816c618ad6e88bfa1114729519a1eae8a24fe0462a4a7741951c43bcbe05aa86a93a57c7e911471ac4454f4bba15ee18a6526825a6f3b4e0ddb5cf8
6
+ metadata.gz: 5a12088967f35d7469fe02657be46005e7fbb927b34c398bd59eaeeebed92d1ee4e68b87ead2f79712e62c2d699b223bec56314100e2752c23bcbfb5910bced7
7
+ data.tar.gz: b3ecb3e7a5cf4f00ca8619570627a451668d05e993cfeea56e3ed0ca2e4f5cf9c6325453695c5b15aa3e279de0d54fe53bbb5fe62c20abf7da62998bf8e94d51
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
+ # 2.1.0 / 2022-04-23
2
+
3
+ - [FEATURE] Change initialize_with to take a block which is run after variables are set.
4
+ - [FEATURE] Add optional kwarg specified by using Mandate::NO_DEFAULT.
5
+
1
6
  # 2.0.0 / 2022-04-19
2
7
 
3
- - [FEATURE] Simplify code base for Ruby 3 (thanks to @erikschierboom)
8
+ - [FEATURE] Simplify codebase for Ruby 3 (thanks to @erikschierboom)
4
9
 
5
10
  # 0.3.0 / 2019-09-06
6
11
 
data/README.md CHANGED
@@ -28,7 +28,7 @@ Or install it yourself as:
28
28
  class Multiplies
29
29
  include Mandate
30
30
 
31
- initialize_with :number_1, :number_2
31
+ initialize_with :number_1, number_2: 0
32
32
 
33
33
  def call
34
34
  do_the_maths
@@ -50,17 +50,38 @@ Multiplies.(20, 3)
50
50
  ### `initialize_with`
51
51
 
52
52
  The `initialize_with` method creates an initializer and private attr_readers for the specified variables.
53
+ Keyword arguments can be expressed normally, but arguments without default values must be specified with the value `Mandate::NO_DEFAULT`.
54
+ The call also takes a block, which is run after the variables are set.
53
55
 
54
- For example `initialize_with :foo, :bar` is the equivalent of:
56
+ For example, this...
55
57
 
56
58
  ```ruby
57
- def initialize(foo, bar)
58
- @foo = foo
59
- @bar = bar
59
+ MODELS = ...
60
+
61
+ class Car
62
+ initialize_with :model, color: "blue", owner: Mandate::NO_DEFAULT do
63
+ raise unless MODELS[model].colors.include?(color)
64
+ end
60
65
  end
66
+ ```
67
+
68
+ ...is the equivalent of...
69
+
70
+ ```ruby
71
+ MODELS = ...
72
+
73
+ class Foobar
74
+ def initialize(model, color: "blue", owner: )
75
+ @model = model
76
+ @color = color
77
+ @owner = owner
61
78
 
62
- private
63
- attr_reader :foo, :bar
79
+ raise unless MODELS[model].colors.include?(color)
80
+ end
81
+
82
+ private
83
+ attr_reader :model, :color, :owner
84
+ end
64
85
  ```
65
86
 
66
87
  ### Using on_success/on_failure callbacks
@@ -1,34 +1,35 @@
1
+ require 'securerandom'
2
+
1
3
  module Mandate
4
+ NO_DEFAULT = SecureRandom.uuid
5
+
2
6
  module InitializerInjector
3
7
  def self.extended(base)
4
8
  class << base
5
- def initialize_with(*attrs, **kwattrs)
6
- if kwattrs.empty?
7
- define_method :initialize do |*args|
8
- unless args.length == attrs.length
9
- raise ArgumentError, "wrong number of arguments (given #{args.length}, expected #{attrs.length})"
10
- end
9
+ def initialize_with(*attrs, **kwattrs, &block)
10
+ define_method :initialize do |*args, **kwargs|
11
+ unless args.length == attrs.length
12
+ raise ArgumentError, "wrong number of arguments (given #{args.length}, expected #{attrs.length})"
13
+ end
11
14
 
12
- attrs.zip(args).each do |attr, arg|
13
- instance_variable_set("@#{attr}", arg)
14
- end
15
+ attrs.zip(args).each do |attr, arg|
16
+ instance_variable_set("@#{attr}", arg)
15
17
  end
16
- else
17
- define_method :initialize do |*args, **kwargs|
18
- unless args.length == attrs.length
19
- raise ArgumentError, "wrong number of arguments (given #{args.length}, expected #{attrs.length})"
20
- end
21
18
 
22
- attrs.zip(args).each do |attr, arg|
23
- instance_variable_set("@#{attr}", arg)
24
- end
19
+ kwargs.each do |name, value|
20
+ raise ArgumentError, "unknown keyword: #{name}" unless kwattrs.key?(name)
25
21
 
26
- kwargs.each do |name, value|
27
- raise ArgumentError, "unknown keyword: #{name}" unless kwattrs.key?(name)
22
+ instance_variable_set("@#{name}", value)
23
+ end
28
24
 
29
- instance_variable_set("@#{name}", value)
30
- end
25
+ kwattrs.each do |key, value|
26
+ next unless value == NO_DEFAULT
27
+ next if kwargs.key?(key)
28
+
29
+ raise ArgumentError, "Keyword argument was not specified: #{key}"
31
30
  end
31
+
32
+ instance_eval(&block) if block
32
33
  end
33
34
 
34
35
  attrs.each do |attr|
@@ -1,3 +1,3 @@
1
1
  module Mandate
2
- VERSION = "2.0.0".freeze
2
+ VERSION = "2.1.0".freeze
3
3
  end
data/mandate.gemspec CHANGED
@@ -23,5 +23,6 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 2.1"
25
25
  spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency 'mocha'
26
27
  spec.add_development_dependency "rake", "~> 12.3"
27
28
  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.0.0
4
+ version: 2.1.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-04-19 00:00:00.000000000 Z
11
+ date: 2022-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mocha
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement