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 +4 -4
- data/CHANGELOG.md +6 -1
- data/README.md +28 -7
- data/lib/mandate/initializer_injector.rb +22 -21
- data/lib/mandate/version.rb +1 -1
- data/mandate.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6da8270b314f00464d035de5d4693038ca29de9a55bca052597931c3afaa44b7
|
4
|
+
data.tar.gz: ebd1428fb13336073a2d4a2d9c32c73b777683b99b69c8941b82f7cb94bd61cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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, :
|
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
|
56
|
+
For example, this...
|
55
57
|
|
56
58
|
```ruby
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
-
|
63
|
-
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
13
|
-
|
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
|
-
|
23
|
-
|
24
|
-
end
|
19
|
+
kwargs.each do |name, value|
|
20
|
+
raise ArgumentError, "unknown keyword: #{name}" unless kwattrs.key?(name)
|
25
21
|
|
26
|
-
|
27
|
-
|
22
|
+
instance_variable_set("@#{name}", value)
|
23
|
+
end
|
28
24
|
|
29
|
-
|
30
|
-
|
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|
|
data/lib/mandate/version.rb
CHANGED
data/mandate.gemspec
CHANGED
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.
|
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-
|
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
|