mandate 1.0.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: 6d56daf22931d4b43f0c73217175ee9ff4c100d59086f5d4ecafbca2caa26bb8
4
- data.tar.gz: 6a3b61893705efd5b9f9bf650d53a7d147ac146fe0165b2ef41378b186ca7e20
3
+ metadata.gz: 12d7d983ad9f280460b90e764a105b84b0d25d311852f71a511ea6a9ddd69d06
4
+ data.tar.gz: 2b37f6c50d8165472db2d2c65da490a01a0047862460594a1e95c3d1cb780db6
5
5
  SHA512:
6
- metadata.gz: 8db9b3480d63328cae72171280e98584f13eb8af7c2778dacfd30cdf50002751cd415c29a431a0f97f6bb0ce2b0e8d5e98d8f27647ecaae9760587d45eaf9f35
7
- data.tar.gz: aca17005769c5c735c127e6f6f852be7eaed2d6288765858fcfa6ac48c88758a22fafbbf55adf65fad101735db267b2e1663d07a2e7e689da01e6796583f9993
6
+ metadata.gz: 53038261b0697c3ad94eb0240b0d031f5face943123a07493e369068922c8463ef1a95483eb2cec77e44ccd5636d5c35730a89ec32afeb712826d8249dd948ff
7
+ data.tar.gz: be465cc6fc64837235ec99ab7b1c071da90978611497138a3ed481eec07d3d987eae5da31b9973b65f99122bf8e44530f2db41cd00a639f9e5669c4253dfd879
@@ -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,40 @@ 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
+ You may also use the `Mandate::KWARGS` param to capture any params that aren't explicitely set.
55
+ The call also takes a block, which is run after the variables are set.
51
56
 
52
- For example `initialize_with :foo, :bar` is the equivalent of:
57
+ For example, this...
53
58
 
54
59
  ```ruby
55
- def initialize(foo, bar)
56
- @foo = foo
57
- @bar = bar
60
+ MODELS = ...
61
+
62
+ class Car
63
+ initialize_with :model, color: "blue", owner: Mandate::NO_DEFAULT, params: Mandate::KWARGS do
64
+ raise unless MODELS[model].colors.include?(color)
65
+ end
58
66
  end
67
+ ```
68
+
69
+ ...is the equivalent of...
59
70
 
60
- private
61
- attr_reader :foo, :bar
71
+ ```ruby
72
+ MODELS = ...
73
+
74
+ class Foobar
75
+ def initialize(model, color: "blue", owner:, params = {})
76
+ @model = model
77
+ @color = color
78
+ @owner = owner
79
+ @params = params
80
+
81
+ raise unless MODELS[model].colors.include?(color)
82
+ end
83
+
84
+ private
85
+ attr_reader :model, :color, :owner
86
+ end
62
87
  ```
63
88
 
64
89
  ### 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,43 @@
1
+ require 'securerandom'
2
+
1
3
  module Mandate
4
+ NO_DEFAULT = SecureRandom.uuid
5
+ KWARGS = SecureRandom.uuid
6
+
2
7
  module InitializerInjector
3
8
  def self.extended(base)
4
9
  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
10
+ def initialize_with(*attrs, **kwattrs, &block)
11
+ kwarg_capture_key = (kwattrs.find(-> { [] }) { |_name, value| value == KWARGS }).first
11
12
 
12
- attrs.zip(args).each do |attr, arg|
13
- instance_variable_set("@#{attr}", arg)
14
- end
13
+ define_method :initialize do |*args, **kwargs|
14
+ unless args.length == attrs.length
15
+ raise ArgumentError, "wrong number of arguments (given #{args.length}, expected #{attrs.length})"
15
16
  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
-
22
- attrs.zip(args).each do |attr, arg|
23
- instance_variable_set("@#{attr}", arg)
24
- end
25
17
 
26
- kwargs.each do |name, value|
27
- raise ArgumentError, "unknown keyword: #{name}" unless kwattrs.key?(name)
18
+ attrs.zip(args).each do |attr, arg|
19
+ instance_variable_set("@#{attr}", arg)
20
+ end
28
21
 
22
+ instance_variable_set("@#{kwarg_capture_key}", {}) if kwarg_capture_key
23
+ kwargs.each do |name, value|
24
+ if kwattrs.key?(name)
29
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
30
  end
31
31
  end
32
+
33
+ kwattrs.each do |key, value|
34
+ next unless value == NO_DEFAULT
35
+ next if kwargs.key?(key)
36
+
37
+ raise ArgumentError, "Keyword argument was not specified: #{key}"
38
+ end
39
+
40
+ instance_eval(&block) if block
32
41
  end
33
42
 
34
43
  attrs.each do |attr|
@@ -1,3 +1,3 @@
1
1
  module Mandate
2
- VERSION = "1.0.0".freeze
2
+ VERSION = "2.2.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
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: 2021-11-11 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
@@ -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
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