mandate 1.0.0.beta1 → 2.1.0

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: a3777f95eea23613ab020fee91200b2d2f74b158827c1c48920b7351718466d1
4
- data.tar.gz: 13f9b05d1beb3ee5327301877f12055ce9e8a4237a96980fadf23a5e1d642806
3
+ metadata.gz: 6da8270b314f00464d035de5d4693038ca29de9a55bca052597931c3afaa44b7
4
+ data.tar.gz: ebd1428fb13336073a2d4a2d9c32c73b777683b99b69c8941b82f7cb94bd61cf
5
5
  SHA512:
6
- metadata.gz: fa01cdd428d030e7eec7581318726731f2d3c72f8e7034876a3e9d1ae97f26a12a67c41ba9a80d115e260d7c1160d3e8018e5159151d67b57873598538628b23
7
- data.tar.gz: 23f4e7d68149d6d4d27d4de21d82c9b64d64a2c420197e1cbc7bc1e9972a900a303b1d20a516bb44b3b7c95834576e34287308ac48914fafe284d70be09c7dd6
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@42817531497ae2ef0168458f709e451109306bd5
17
+ uses: ruby/setup-ruby@4726835508d9debb3220096c92d7bf6388485faa
18
18
  with:
19
- ruby-version: 2.6.6
19
+ ruby-version: '3.1.0'
20
20
 
21
21
  - name: Install gems
22
22
  run: |
@@ -15,13 +15,13 @@ jobs:
15
15
  matrix:
16
16
  os:
17
17
  - ubuntu-20.04
18
- ruby-version: [2.6, 2.7, 3.0]
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@42817531497ae2ef0168458f709e451109306bd5
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
@@ -3,6 +3,7 @@ require:
3
3
  - rubocop-performance
4
4
 
5
5
  AllCops:
6
+ TargetRubyVersion: 3.0.0
6
7
  NewCops: disable
7
8
  Exclude:
8
9
  - "bin/**/*"
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.6.6
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
- * [FEATURE] Add success/failure callbacks
11
+
12
+ - [FEATURE] Add success/failure callbacks
3
13
 
4
14
  # 0.2.0 / 2018-08-11
5
- * [FEATURE] Add initialize_with
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, :number_2
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 `initialize_with :foo, :bar` is the equivalent of:
56
+ For example, this...
53
57
 
54
58
  ```ruby
55
- def initialize(foo, bar)
56
- @foo = foo
57
- @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
58
65
  end
66
+ ```
67
+
68
+ ...is the equivalent of...
59
69
 
60
- private
61
- attr_reader :foo, :bar
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(*args)
10
- # If the last argument is a hash and the last param is a keyword params (signified by
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
- 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 = "1.0.0.beta1".freeze
2
+ VERSION = "2.1.0".freeze
3
3
  end
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 = '>= 2.6.0'
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.0.beta1
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: 2021-02-25 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
@@ -95,14 +109,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
109
  requirements:
96
110
  - - ">="
97
111
  - !ruby/object:Gem::Version
98
- version: 2.6.0
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: 1.3.1
117
+ version: '0'
104
118
  requirements: []
105
- rubygems_version: 3.0.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