mandate 1.0.0.beta1 → 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/.github/workflows/rubocop.yml +2 -2
- data/.github/workflows/tests.yml +2 -2
- data/.rubocop.yml +1 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +13 -2
- data/README.md +30 -7
- data/lib/mandate/call_injector.rb +2 -10
- data/lib/mandate/initializer_injector.rb +22 -21
- data/lib/mandate/version.rb +1 -1
- data/mandate.gemspec +2 -1
- metadata +20 -6
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
|
@@ -14,9 +14,9 @@ jobs:
|
|
14
14
|
- uses: actions/checkout@v2
|
15
15
|
|
16
16
|
- name: Set up Ruby
|
17
|
-
uses: ruby/setup-ruby@
|
17
|
+
uses: ruby/setup-ruby@4726835508d9debb3220096c92d7bf6388485faa
|
18
18
|
with:
|
19
|
-
ruby-version:
|
19
|
+
ruby-version: '3.1.0'
|
20
20
|
|
21
21
|
- name: Install gems
|
22
22
|
run: |
|
data/.github/workflows/tests.yml
CHANGED
@@ -15,13 +15,13 @@ jobs:
|
|
15
15
|
matrix:
|
16
16
|
os:
|
17
17
|
- ubuntu-20.04
|
18
|
-
ruby-version: [
|
18
|
+
ruby-version: ['3.0', '3.1']
|
19
19
|
|
20
20
|
steps:
|
21
21
|
- uses: actions/checkout@v2
|
22
22
|
|
23
23
|
- name: Set up Ruby
|
24
|
-
uses: ruby/setup-ruby@
|
24
|
+
uses: ruby/setup-ruby@4726835508d9debb3220096c92d7bf6388485faa
|
25
25
|
with:
|
26
26
|
ruby-version: ${{ matrix.ruby-version }}
|
27
27
|
bundler-cache: true
|
data/.rubocop.yml
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
3.1.0
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,16 @@
|
|
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
|
+
|
6
|
+
# 2.0.0 / 2022-04-19
|
7
|
+
|
8
|
+
- [FEATURE] Simplify codebase for Ruby 3 (thanks to @erikschierboom)
|
9
|
+
|
1
10
|
# 0.3.0 / 2019-09-06
|
2
|
-
|
11
|
+
|
12
|
+
- [FEATURE] Add success/failure callbacks
|
3
13
|
|
4
14
|
# 0.2.0 / 2018-08-11
|
5
|
-
|
15
|
+
|
16
|
+
- [FEATURE] Add initialize_with
|
data/README.md
CHANGED
@@ -4,6 +4,8 @@
|
|
4
4
|
|
5
5
|
A simple command-pattern helper gem for Ruby.
|
6
6
|
|
7
|
+
**Note: As of 2.0.0, Mandate only supports Ruby 3.x. For Ruby 2.x, please use the 1.0.0 release.**
|
8
|
+
|
7
9
|
## Installation
|
8
10
|
|
9
11
|
Add this line to your application's Gemfile:
|
@@ -26,7 +28,7 @@ Or install it yourself as:
|
|
26
28
|
class Multiplies
|
27
29
|
include Mandate
|
28
30
|
|
29
|
-
initialize_with :number_1, :
|
31
|
+
initialize_with :number_1, number_2: 0
|
30
32
|
|
31
33
|
def call
|
32
34
|
do_the_maths
|
@@ -48,17 +50,38 @@ Multiplies.(20, 3)
|
|
48
50
|
### `initialize_with`
|
49
51
|
|
50
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.
|
51
55
|
|
52
|
-
For example
|
56
|
+
For example, this...
|
53
57
|
|
54
58
|
```ruby
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
58
65
|
end
|
66
|
+
```
|
67
|
+
|
68
|
+
...is the equivalent of...
|
59
69
|
|
60
|
-
|
61
|
-
|
70
|
+
```ruby
|
71
|
+
MODELS = ...
|
72
|
+
|
73
|
+
class Foobar
|
74
|
+
def initialize(model, color: "blue", owner: )
|
75
|
+
@model = model
|
76
|
+
@color = color
|
77
|
+
@owner = owner
|
78
|
+
|
79
|
+
raise unless MODELS[model].colors.include?(color)
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
attr_reader :model, :color, :owner
|
84
|
+
end
|
62
85
|
```
|
63
86
|
|
64
87
|
### Using on_success/on_failure callbacks
|
@@ -6,16 +6,8 @@ module Mandate
|
|
6
6
|
# which internally calls:
|
7
7
|
# Foobar.new(some, args).call()
|
8
8
|
class << base
|
9
|
-
def call(
|
10
|
-
|
11
|
-
# its type being :key, the we should pass the hash in in using the **kwords syntax.
|
12
|
-
# This fixes a deprecation issue in Ruby 2.7.
|
13
|
-
if args.last.is_a?(Hash) &&
|
14
|
-
instance_method(:initialize).parameters.last&.first == :key
|
15
|
-
new(*args[0..-2], **args[-1]).()
|
16
|
-
else
|
17
|
-
new(*args).()
|
18
|
-
end
|
9
|
+
def call(...)
|
10
|
+
new(...).()
|
19
11
|
end
|
20
12
|
end
|
21
13
|
end
|
@@ -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
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
3
|
require "mandate/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
|
-
spec.required_ruby_version = '>=
|
6
|
+
spec.required_ruby_version = '>= 3.0.0'
|
7
7
|
spec.name = "mandate"
|
8
8
|
spec.version = Mandate::VERSION
|
9
9
|
spec.authors = ["Jeremy Walker"]
|
@@ -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: 1.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:
|
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
|
@@ -95,14 +109,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
95
109
|
requirements:
|
96
110
|
- - ">="
|
97
111
|
- !ruby/object:Gem::Version
|
98
|
-
version:
|
112
|
+
version: 3.0.0
|
99
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
|
-
- - "
|
115
|
+
- - ">="
|
102
116
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
117
|
+
version: '0'
|
104
118
|
requirements: []
|
105
|
-
rubygems_version: 3.
|
119
|
+
rubygems_version: 3.3.3
|
106
120
|
signing_key:
|
107
121
|
specification_version: 4
|
108
122
|
summary: A simple command-pattern helper gem for Ruby
|