ruby_arguments 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 00dd9a3e3642d15c066c4fb124c69e2252590e95f8756747e9eb09f2746aa9c7
4
+ data.tar.gz: 459b704c14888e6cd736576b6dbe1bea6113a7d2eefa13a50f332c89e480eeaf
5
+ SHA512:
6
+ metadata.gz: e98906719edca259ff5c5b6220134bcda5396dba5d78ef0c3b81d0c4411ef0f109ae6e1478009279d97f195c0672872f2ce22483d87ff04dd6ef388feb28419d
7
+ data.tar.gz: bb3e69d95db037f81da620128f0d0763f4ef177669ace0d25b6a47b239d0173fc9f472c6c22811894abeece3d4c73d43305757820d9d41907ae8eb7be264c075
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Marian Kostyk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ <!-- AUTHOR: Marian Kostyk <mariankostyk13895@gmail.com> -->
2
+ <!-- LICENSE: MIT <https://opensource.org/license/mit> -->
3
+
4
+ # Ruby Arguments
5
+
6
+ [![Ruby](https://img.shields.io/badge/ruby-%23CC342D.svg?style=for-the-badge&logo=ruby&logoColor=white)](https://www.ruby-lang.org/en/)
7
+
8
+ [![Gem Version](https://badge.fury.io/rb/ruby_arguments.svg)](https://rubygems.org/gems/ruby_arguments) [![Gem Downloads](https://img.shields.io/gem/dt/ruby_arguments.svg)](https://rubygems.org/gems/ruby_arguments) ![GitHub repo size](https://img.shields.io/github/repo-size/marian13/ruby_arguments) [![GitHub Actions CI](https://github.com/marian13/ruby_arguments/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/marian13/ruby_arguments/actions/workflows/ci.yml) [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard) [![Coverage Status](https://coveralls.io/repos/github/marian13/ruby_arguments/badge.svg)](https://coveralls.io/github/marian13/ruby_arguments?branch=main) [![yard docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://marian13.github.io/ruby_arguments)
9
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
10
+
11
+ Ruby Arguments encapsulate method positional arguments (`args`), keyword arguments (`kwargs`), and an optional block (`block`) in a single value object (null object is also available).
12
+
13
+ That may be useful for DSLs, caches, callbacks, custom RSpec matchers, hash keys, pattern matching and so on.
14
+
15
+ The initial Ruby Arguments implementation was extracted from the [Convenient Service](https://github.com/marian13/convenient_service).
16
+
17
+ ## TL;DR
18
+
19
+ ```ruby
20
+ require "ruby_arguments"
21
+
22
+ def some_method(*args, **kwargs, &block)
23
+ RubyArguments.new(*args, **kwargs, &block)
24
+ end
25
+
26
+ ##
27
+ # Or shorter from Ruby 2.7.
28
+ #
29
+ def some_method(...)
30
+ RubyArguments.new(...)
31
+ end
32
+
33
+ args = [:foo, :bar]
34
+ kwargs = {foo: :bar, baz: :qux}
35
+ block = proc { :foo }
36
+
37
+ arguments = some_method(*args, **kwargs, &block)
38
+
39
+ arguments.args
40
+ # => [:foo, :bar]
41
+
42
+ arguments.kwargs
43
+ # => {foo: :bar, baz: :qux}
44
+
45
+ arguments.block
46
+ # => #<Proc:0x000000012a97fc18>
47
+
48
+ arguments.null_arguments?
49
+ # => false
50
+
51
+ arguments.any?
52
+ # => true
53
+
54
+ arguments.none?
55
+ # => false
56
+
57
+ arguments.empty?
58
+ # => false
59
+
60
+ arguments.present?
61
+ # => true
62
+
63
+ arguments.blank?
64
+ # => false
65
+
66
+ arguments[0]
67
+ # => :foo
68
+
69
+ arguments[:foo]
70
+ # => :bar
71
+
72
+ def some_other_method
73
+ RubyArguments.null_arguments
74
+ end
75
+
76
+ null_arguments = some_other_method
77
+
78
+ null_arguments.null_arguments?
79
+ # => true
80
+
81
+ arguments == null_arguments
82
+ # => false
83
+
84
+ arguments.eql?(null_arguments)
85
+ # => false
86
+
87
+ arguments in [args, kwargs, block]
88
+ # => true
89
+
90
+ arguments in args:, kwargs:, :block
91
+ # => true
92
+ ```
93
+
94
+ ## Installation
95
+
96
+ ### Bundler
97
+
98
+ Add the following line to your Gemfile:
99
+
100
+ ```ruby
101
+ gem "ruby_arguments"
102
+ ```
103
+
104
+ And then run:
105
+
106
+ ```bash
107
+ bundle install
108
+ ```
109
+
110
+ ### Without Bundler
111
+
112
+ Execute the command below:
113
+
114
+ ```bash
115
+ gem install ruby_arguments
116
+ ```
117
+
118
+ ---
119
+
120
+ Copyright (c) 2025 Marian Kostyk.
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # @author Marian Kostyk <mariankostyk13895@gmail.com>
5
+ # @license MIT <https://opensource.org/license/mit>
6
+ ##
7
+
8
+ class RubyArguments
9
+ VERSION = "1.0.0"
10
+ end
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # @author Marian Kostyk <mariankostyk13895@gmail.com>
5
+ # @license MIT <https://opensource.org/license/mit>
6
+ ##
7
+
8
+ class RubyArguments
9
+ module Exceptions
10
+ class Base < ::StandardError
11
+ end
12
+
13
+ class InvalidKeyType < Base
14
+ class << self
15
+ ##
16
+ # @api private
17
+ # @param key [Object] Can be any type.
18
+ # @return [RubyArguments::Exceptions::InvalidKeyType]
19
+ #
20
+ def create(key:)
21
+ message = <<~TEXT
22
+ `#[]` accepts only `Integer` and `String` keys.
23
+
24
+ Key `#{key.inspect}` has `#{key.class}` class.
25
+ TEXT
26
+
27
+ new(message)
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ class NullArguments < ::RubyArguments
34
+ ##
35
+ # @api private
36
+ # @return [void]
37
+ #
38
+ def initialize
39
+ @args = []
40
+ @kwargs = {}
41
+ @block = nil
42
+ end
43
+
44
+ ##
45
+ # @api public
46
+ # @return [Boolean]
47
+ #
48
+ def null_arguments?
49
+ true
50
+ end
51
+ end
52
+
53
+ ##
54
+ # @api public
55
+ # @!attribute [r] args
56
+ # @return [Array<Object>]
57
+ #
58
+ attr_reader :args
59
+
60
+ ##
61
+ # @api public
62
+ # @!attribute [r] kwargs
63
+ # @return [Hash{Symbol => Object}]
64
+ #
65
+ attr_reader :kwargs
66
+
67
+ ##
68
+ # @api public
69
+ # @!attribute [r] block
70
+ # @return [Proc, nil]
71
+ #
72
+ attr_reader :block
73
+
74
+ ##
75
+ # @api public
76
+ # @param args [Array<Object>]
77
+ # @param kwargs [Hash{Symbol => Object}]
78
+ # @param block [Proc, nil]
79
+ # @return [void]
80
+ #
81
+ def initialize(*args, **kwargs, &block)
82
+ @args = args
83
+ @kwargs = kwargs
84
+ @block = block
85
+ end
86
+
87
+ class << self
88
+ ##
89
+ # @api public
90
+ # @return [RubyArguments::NullArguments]
91
+ #
92
+ def null_arguments
93
+ @null_arguments ||= ::RubyArguments::NullArguments.new
94
+ end
95
+ end
96
+
97
+ ##
98
+ # @api public
99
+ # @return [Boolean]
100
+ #
101
+ def null_arguments?
102
+ false
103
+ end
104
+
105
+ ##
106
+ # @api public
107
+ # @return [Booleam]
108
+ #
109
+ def any?
110
+ return true if args.any?
111
+ return true if kwargs.any?
112
+ return true if block
113
+
114
+ false
115
+ end
116
+
117
+ ##
118
+ # @api public
119
+ # @return [Booleam]
120
+ #
121
+ def none?
122
+ !any?
123
+ end
124
+
125
+ ##
126
+ # @api public
127
+ # @return [Booleam]
128
+ #
129
+ def empty?
130
+ none?
131
+ end
132
+
133
+ ##
134
+ # @api public
135
+ # @return [Booleam]
136
+ #
137
+ def present?
138
+ any?
139
+ end
140
+
141
+ ##
142
+ # @api public
143
+ # @return [Booleam]
144
+ #
145
+ def blank?
146
+ none?
147
+ end
148
+
149
+ ##
150
+ # @api public
151
+ # @param key [Integer, Symbol]
152
+ # @return [Object] Can be any type.
153
+ # @raise [RubyArguments::Exceptions::InvalidKeyType]
154
+ #
155
+ def [](key)
156
+ case key
157
+ when ::Integer then args[key]
158
+ when ::Symbol then kwargs[key]
159
+ else raise Exceptions::InvalidKeyType.create(key: key)
160
+ end
161
+ end
162
+
163
+ ##
164
+ # @api public
165
+ # @param other [Object] Can be any type.
166
+ # @return [Boolean, nil]
167
+ #
168
+ def ==(other)
169
+ return unless other.instance_of?(self.class)
170
+
171
+ return false if args != other.args
172
+ return false if kwargs != other.kwargs
173
+ return false if block != other.block
174
+
175
+ true
176
+ end
177
+
178
+ ##
179
+ # @api public
180
+ # @param other [Object] Can be any type.
181
+ # @return [Boolean, nil]
182
+ #
183
+ def eql?(other)
184
+ return unless other.instance_of?(self.class)
185
+
186
+ return false if args != other.args
187
+ return false if kwargs != other.kwargs
188
+ return false if block != other.block
189
+
190
+ true
191
+ end
192
+
193
+ ##
194
+ # @api public
195
+ # @return [Integer]
196
+ #
197
+ def hash
198
+ [self.class, args, kwargs, block].hash
199
+ end
200
+
201
+ ##
202
+ # @api public
203
+ # @return [Array]
204
+ #
205
+ def deconstruct
206
+ [args, kwargs, block]
207
+ end
208
+
209
+ ##
210
+ # @api public
211
+ # @param keys [Array<Symbol>, nil]
212
+ # @return [Hash]
213
+ #
214
+ def deconstruct_keys(keys)
215
+ keys ||= [:args, :kwargs, :block]
216
+
217
+ keys.each_with_object({}) do |key, hash|
218
+ case key
219
+ when :args
220
+ hash[key] = args
221
+ when :kwargs
222
+ hash[key] = kwargs
223
+ when :block
224
+ hash[key] = block
225
+ end
226
+ end
227
+ end
228
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_arguments
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marian Kostyk
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Ruby Arguments encapsulate method positional arguments (args), keyword
13
+ arguments (kwargs), and an optional block (block) in a single value object (null
14
+ object is also available).
15
+ email:
16
+ - mariankostyk13895@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE.txt
22
+ - README.md
23
+ - lib/ruby_arguments.rb
24
+ - lib/ruby_arguments/version.rb
25
+ homepage: https://github.com/marian13/ruby_arguments
26
+ licenses:
27
+ - MIT
28
+ metadata:
29
+ homepage_uri: https://github.com/marian13/ruby_arguments
30
+ source_code_uri: https://github.com/marian13/ruby_arguments
31
+ changelog_uri: https://github.com/marian13/ruby_arguments/CHANGELOG.md
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 2.4.0
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.6.7
47
+ specification_version: 4
48
+ summary: Ruby Arguments encapsulate method positional arguments (args), keyword arguments
49
+ (kwargs), and an optional block (block) in a single value object (null object is
50
+ also available).
51
+ test_files: []