defi 2.0.7 → 3.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 +4 -4
- data/LICENSE.md +1 -1
- data/README.md +36 -15
- data/lib/defi/method.rb +99 -0
- data/lib/defi/value.rb +41 -14
- data/lib/defi.rb +21 -15
- data/lib/kernel.rb +42 -0
- metadata +7 -132
- data/lib/defi/challenge.rb +0 -99
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd965274ab9b7a6dd1303d7aa7b4c1da9c87551c6197a5075ea1f6a8c409df52
|
4
|
+
data.tar.gz: 2fc6a867f949feeb94e971347313fd1b734c5851bb1f770e77bc6b2c0d72e9d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99d0012c6768b9e38db3a149cb9021c3fdad056705748079e8e33ca6b7e05f55ec551ea1b784eb2cfba31dd3bae2f5454d75eeb8469fa5372321c192c42dcf99
|
7
|
+
data.tar.gz: 789d8cd29d0cb8d608980c88b995d5d188e93febf868715113a1b274fe35cb54f2cd01f5b7a0f57d3e5ba804099c3129ab824e7ecfc0b50cf6b903df926bd935
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,20 @@
|
|
1
1
|
# Defi
|
2
2
|
|
3
|
-
[](https://github.com/fixrb/defi/
|
3
|
+
[](https://github.com/fixrb/defi/tags)
|
4
4
|
[](https://rubydoc.info/github/fixrb/defi/main)
|
5
|
-
[](https://github.com/fixrb/defi/actions?query=workflow%3Aruby+branch%3Amain)
|
6
6
|
[](https://github.com/fixrb/defi/actions?query=workflow%3Arubocop+branch%3Amain)
|
7
7
|
[](https://github.com/fixrb/defi/raw/main/LICENSE.md)
|
8
8
|
|
9
|
-
|
9
|
+
**Defi** is a streamlined Ruby library designed for the specification of method arguments while respecting their signatures.
|
10
|
+
Rather than representing method signatures themselves, Defi focuses on providing a way to furnish methods with appropriate arguments, thereby preparing them for invocation.
|
11
|
+
|
12
|
+
This approach serves as an alternative to traditional methods like `Object#method` and `UnboundMethod`.
|
13
|
+
Where `Object#method` is bound to a specific object and `UnboundMethod` requires compatibility with the method’s originating class, Defi offers a more flexible and universal way to prepare method calls.
|
14
|
+
It allows method arguments to be specified in advance and then applied to any compatible object – those equipped with corresponding methods.
|
15
|
+
|
16
|
+
Defi is particularly useful in scenarios where you need to apply a set of method arguments across different objects to observe the varying outcomes, whether they be returned values or exceptions.
|
17
|
+
This makes it an ideal tool for testing, method comparison across different implementations, or any situation where method behavior needs to be assessed dynamically across various objects.
|
10
18
|
|
11
19
|
## Installation
|
12
20
|
|
@@ -30,26 +38,39 @@ gem install defi
|
|
30
38
|
|
31
39
|
## Usage
|
32
40
|
|
33
|
-
|
41
|
+
The `Defi` library simplifies the task of applying method signatures to various objects, regardless of their type (instances, modules, etc.).
|
42
|
+
Below are some detailed examples to demonstrate its versatility and ease of use:
|
34
43
|
|
35
|
-
|
36
|
-
Defi.send(:*, 7).to(6).call # => 42
|
37
|
-
```
|
44
|
+
### Example 1: Multiplying Numbers
|
38
45
|
|
39
|
-
|
46
|
+
Suppose you want to multiply the number `6` by `7`.
|
47
|
+
With `Defi`, this can be elegantly done as follows:
|
40
48
|
|
41
49
|
```ruby
|
42
|
-
Defi
|
50
|
+
result = Defi(:*, 7).to(6).call
|
51
|
+
puts result # Output: 42
|
43
52
|
```
|
44
53
|
|
45
|
-
|
54
|
+
Here, `Defi(:*, 7)` creates a challenge with the multiplication method (`:*`) and the argument `7`.
|
55
|
+
The `.to(6)` method specifies that this challenge should be applied to the number `6`.
|
56
|
+
Finally, `.call` executes the challenge, yielding the result `42`.
|
57
|
+
|
58
|
+
### Example 2: Invoking an Undefined Method
|
59
|
+
|
60
|
+
`Defi` also elegantly handles cases where the method might not exist on the target object.
|
61
|
+
For instance:
|
46
62
|
|
47
63
|
```ruby
|
48
|
-
|
49
|
-
Defi
|
50
|
-
|
64
|
+
begin
|
65
|
+
Defi(:boom).to("foo").call
|
66
|
+
rescue NoMethodError => e
|
67
|
+
puts e.message # Output: undefined method `boom' for "foo":String
|
68
|
+
end
|
51
69
|
```
|
52
70
|
|
71
|
+
In this example, we attempt to call the non-existent method `boom` on the string `"foo"`.
|
72
|
+
`Defi` correctly raises a `NoMethodError`, showing that the method `boom` is undefined for a `String` object.
|
73
|
+
|
53
74
|
## Contact
|
54
75
|
|
55
76
|
* Source code: https://github.com/fixrb/defi/issues
|
@@ -62,11 +83,11 @@ __Defi__ follows [Semantic Versioning 2.0](https://semver.org/).
|
|
62
83
|
|
63
84
|
The [gem](https://rubygems.org/gems/defi) is available as open source under the terms of the [MIT License](https://github.com/fixrb/defi/raw/main/LICENSE.md).
|
64
85
|
|
65
|
-
|
86
|
+
---
|
66
87
|
|
67
88
|
<p>
|
68
89
|
This project is sponsored by:<br />
|
69
90
|
<a href="https://sashite.com/"><img
|
70
91
|
src="https://github.com/fixrb/defi/raw/main/img/sashite.png"
|
71
|
-
alt="
|
92
|
+
alt="Sashité" /></a>
|
72
93
|
</p>
|
data/lib/defi/method.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Defi
|
4
|
+
# Represents a method to be applied against an object.
|
5
|
+
# This class encapsulates the method name, its arguments, keyword arguments,
|
6
|
+
# and an optional block, enabling dynamic method invocation.
|
7
|
+
class Method < ::BasicObject
|
8
|
+
# Initialize a new Method object.
|
9
|
+
#
|
10
|
+
# @param name [Symbol] The name of the method.
|
11
|
+
# @param args [Array] Any arguments of the method.
|
12
|
+
# @param opts [Hash] Any keyword arguments of the method.
|
13
|
+
# @param block [Proc] Any block argument of the method.
|
14
|
+
# @raise [ArgumentError] If the name is not a symbol.
|
15
|
+
def initialize(name, *args, **opts, &block)
|
16
|
+
raise ::ArgumentError, name.class.inspect unless name.is_a?(::Symbol)
|
17
|
+
|
18
|
+
@name = name
|
19
|
+
@args = args
|
20
|
+
@opts = opts
|
21
|
+
@block = block
|
22
|
+
end
|
23
|
+
|
24
|
+
# Applies the method to the given object.
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
# add = Defi::Method.new(:+, 1)
|
28
|
+
# add.to(2).call # => 3
|
29
|
+
#
|
30
|
+
# @param object [#object_id] The object to method.
|
31
|
+
# @return [Defi::Value] The actual value, to raise or to return.
|
32
|
+
def to(object)
|
33
|
+
Value.new { object.public_send(@name, *@args, **@opts, &@block) }
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns a hash containing the method's properties.
|
37
|
+
#
|
38
|
+
# @return [Hash] The properties of the method.
|
39
|
+
def to_h
|
40
|
+
{
|
41
|
+
name: @name,
|
42
|
+
args: @args,
|
43
|
+
opts: @opts,
|
44
|
+
block: @block
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
# rubocop:disable Metrics/AbcSize
|
49
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
50
|
+
|
51
|
+
# Returns a string representation of the method.
|
52
|
+
#
|
53
|
+
# @example
|
54
|
+
# add = Defi::Method.new(:+, 1)
|
55
|
+
# add.to_s # => ".+(1)"
|
56
|
+
#
|
57
|
+
# @return [String] The string representation of the method.
|
58
|
+
def to_s
|
59
|
+
string = ".#{@name}"
|
60
|
+
return string if @args.empty? && @opts.empty? && @block.nil?
|
61
|
+
|
62
|
+
stringified_args = @args.inspect[1..-2]
|
63
|
+
stringified_opts = @opts.inspect[1..-2]
|
64
|
+
stringified_block = "<Proc>" unless @block.nil?
|
65
|
+
|
66
|
+
stringified_items = []
|
67
|
+
stringified_items << stringified_args unless @args.empty?
|
68
|
+
stringified_items << stringified_opts unless @opts.empty?
|
69
|
+
stringified_items << stringified_block unless @block.nil?
|
70
|
+
|
71
|
+
"#{string}(#{stringified_items.join(", ")})"
|
72
|
+
end
|
73
|
+
|
74
|
+
# rubocop:enable Metrics/AbcSize
|
75
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
76
|
+
|
77
|
+
# Returns a human-readable representation of the method.
|
78
|
+
#
|
79
|
+
# @example
|
80
|
+
# add = Defi::Method.new(:+, 1)
|
81
|
+
# add.inspect # => "Defi(name: :+, args: [1], opts: {}, block: nil)"
|
82
|
+
#
|
83
|
+
# @return [String] The human-readable representation of the method.
|
84
|
+
def inspect
|
85
|
+
inspected_name = @name.inspect
|
86
|
+
inspected_args = @args.inspect
|
87
|
+
inspected_opts = @opts.inspect
|
88
|
+
inspected_block = @block.nil? ? "nil" : "<Proc>"
|
89
|
+
|
90
|
+
"Defi(" \
|
91
|
+
"name: #{inspected_name}, " \
|
92
|
+
"args: #{inspected_args}, " \
|
93
|
+
"opts: #{inspected_opts}, " \
|
94
|
+
"block: #{inspected_block})"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
require_relative "value"
|
data/lib/defi/value.rb
CHANGED
@@ -1,15 +1,30 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Defi
|
4
|
-
#
|
4
|
+
# Represents a result of an operation, encapsulating either the returned value
|
5
|
+
# or an exception raised during the execution.
|
5
6
|
#
|
7
|
+
# This class is used to handle the outcome of a method invocation, allowing
|
8
|
+
# to distinguish between successful results and exceptions.
|
6
9
|
class Value
|
10
|
+
RAISE = "raise"
|
11
|
+
RETURN = "return"
|
12
|
+
|
7
13
|
# @return [#object_id] The returned or the raised object.
|
8
14
|
attr_reader :object
|
9
15
|
|
10
|
-
#
|
16
|
+
# Initializes a Value object with the result of the provided block.
|
17
|
+
# Captures any exception raised during the block execution.
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
# value = Defi::Value.new { 1 + 1 }
|
21
|
+
# value.call # => 2
|
11
22
|
#
|
12
|
-
# @
|
23
|
+
# @example Handling an exception
|
24
|
+
# value = Defi::Value.new { raise 'Error' }
|
25
|
+
# value.call # raises 'Error'
|
26
|
+
#
|
27
|
+
# @yieldreturn [#object_id] The result of the block or the exception raised.
|
13
28
|
# rubocop:disable Lint/RescueException
|
14
29
|
def initialize
|
15
30
|
@object = yield
|
@@ -20,49 +35,61 @@ module Defi
|
|
20
35
|
end
|
21
36
|
# rubocop:enable Lint/RescueException
|
22
37
|
|
23
|
-
#
|
38
|
+
# Returns the object if no exception was raised, otherwise raises the exception.
|
39
|
+
#
|
40
|
+
# @example
|
41
|
+
# value = Defi::Value.new { "Hello" }
|
42
|
+
# value.call # => "Hello"
|
24
43
|
#
|
25
|
-
# @return [#object_id]
|
44
|
+
# @return [#object_id] The returned object or raises the captured exception.
|
26
45
|
def call
|
27
46
|
raise object if raised?
|
28
47
|
|
29
48
|
object
|
30
49
|
end
|
31
50
|
|
32
|
-
#
|
51
|
+
# Checks if an exception was raised during the execution.
|
52
|
+
#
|
53
|
+
# @example
|
54
|
+
# value = Defi::Value.new { raise 'Error' }
|
55
|
+
# value.raised? # => true
|
56
|
+
#
|
57
|
+
# @return [Boolean] True if an exception was raised, otherwise false.
|
33
58
|
def raised?
|
34
59
|
@raised
|
35
60
|
end
|
36
61
|
|
37
|
-
#
|
62
|
+
# Returns a hash containing the properties of the Value object.
|
38
63
|
#
|
39
|
-
# @return [Hash] The properties of the
|
64
|
+
# @return [Hash] The properties of the Value object.
|
40
65
|
def to_h
|
41
66
|
{
|
42
67
|
raised: raised?,
|
43
|
-
object:
|
68
|
+
object:
|
44
69
|
}
|
45
70
|
end
|
46
71
|
|
47
|
-
#
|
72
|
+
# Returns a string representation of the Value object.
|
48
73
|
#
|
49
|
-
# @return [String] The string representation of the
|
74
|
+
# @return [String] The string representation of the Value object.
|
50
75
|
def to_s
|
51
76
|
"#{raise_or_return} #{object}"
|
52
77
|
end
|
53
78
|
|
54
|
-
#
|
79
|
+
# Returns a human-readable representation of the Value object.
|
55
80
|
#
|
56
|
-
# @return [String] The human-readable representation of the
|
81
|
+
# @return [String] The human-readable representation of the Value object.
|
57
82
|
def inspect
|
58
83
|
"Value(object: #{object}, raised: #{raised?})"
|
59
84
|
end
|
60
85
|
|
61
86
|
private
|
62
87
|
|
88
|
+
# Returns a string indicating whether the object was raised or returned.
|
89
|
+
#
|
63
90
|
# @return [String] A "raise" or "return" string.
|
64
91
|
def raise_or_return
|
65
|
-
raised? ?
|
92
|
+
raised? ? RAISE : RETURN
|
66
93
|
end
|
67
94
|
end
|
68
95
|
end
|
data/lib/defi.rb
CHANGED
@@ -2,21 +2,27 @@
|
|
2
2
|
|
3
3
|
# Namespace for the Defi library.
|
4
4
|
#
|
5
|
+
# This file serves as the entry point for the Defi library, establishing the
|
6
|
+
# Defi namespace and requiring necessary components. It is typically required
|
7
|
+
# at the beginning of using the Defi library in an application.
|
8
|
+
#
|
9
|
+
# @example Requiring the Defi library in a Ruby application
|
10
|
+
# require "defi"
|
11
|
+
#
|
12
|
+
# @example Adding 2 to 1
|
13
|
+
# # Create a Defi method object for addition with an argument of 2
|
14
|
+
# addition = Defi(:+, 2)
|
15
|
+
# addition.inspect # => "Defi(name: :+, args: [2], opts: {}, block: nil)"
|
16
|
+
#
|
17
|
+
# # Apply the addition to the number 1
|
18
|
+
# result = addition.to(1)
|
19
|
+
# result # => Value(object: 3, raised: false)
|
20
|
+
#
|
21
|
+
# # Execute the addition and get the result
|
22
|
+
# result.call # => 3
|
23
|
+
#
|
5
24
|
module Defi
|
6
|
-
# Expectations are built with this method.
|
7
|
-
#
|
8
|
-
# @example A :foo challenge
|
9
|
-
# send(:foo) # => #<Defi::Challenge:0x007f96a40925f8 @method=:foo, @args=[]>
|
10
|
-
#
|
11
|
-
# @param method [#to_sym] The method to send to an object.
|
12
|
-
# @param args [Array] Any arguments of the method.
|
13
|
-
# @param kwargs [Hash] Any keyword arguments of the method.
|
14
|
-
# @param block [Proc] Any block argument of the method.
|
15
|
-
#
|
16
|
-
# @return [Challenge] The challenge instance.
|
17
|
-
def self.send(method, *args, **kwargs, &block)
|
18
|
-
Challenge.new(method, *args, **kwargs, &block)
|
19
|
-
end
|
20
25
|
end
|
21
26
|
|
22
|
-
|
27
|
+
# Require additional components of the Defi library.
|
28
|
+
require_relative "kernel"
|
data/lib/kernel.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative File.join("defi", "method")
|
4
|
+
|
5
|
+
# Extension of the Kernel module to include the Defi method.
|
6
|
+
# The Defi method is a convenient way to create Method objects
|
7
|
+
# that encapsulate a method name and its arguments, offering a
|
8
|
+
# dynamic approach to method invocation.
|
9
|
+
module Kernel
|
10
|
+
# Disabling the RuboCop rule for method naming conventions
|
11
|
+
# to define a method with an uppercase name for stylistic reasons.
|
12
|
+
# rubocop:disable Naming/MethodName
|
13
|
+
|
14
|
+
# Creates a new Defi::Method instance.
|
15
|
+
# This method provides a simple and elegant way to encapsulate
|
16
|
+
# a method name and its arguments for later invocation.
|
17
|
+
#
|
18
|
+
# @example Creating a Defi method without arguments
|
19
|
+
# Defi(:foo).inspect # => "Defi(name: :foo, args: [], opts: {}, block: nil)"
|
20
|
+
#
|
21
|
+
# @example Adding 2 to 1
|
22
|
+
# # Create a Defi method object for addition with an argument of 2
|
23
|
+
# addition = Defi(:+, 2)
|
24
|
+
# addition.inspect # => "Defi(name: :+, args: [2], opts: {}, block: nil)"
|
25
|
+
#
|
26
|
+
# # Apply the addition to the number 1
|
27
|
+
# result = addition.to(1)
|
28
|
+
# result # => Value(object: 3, raised: false)
|
29
|
+
#
|
30
|
+
# # Execute the addition and get the result
|
31
|
+
# result.call # => 3
|
32
|
+
#
|
33
|
+
# @param method_name [Symbol] The method name to be sent to an object.
|
34
|
+
#
|
35
|
+
# @return [Defi::Method] An instance of Defi::Method encapsulating the method name and provided arguments.
|
36
|
+
def Defi(method_name, ...)
|
37
|
+
::Defi::Method.new(method_name, ...)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Re-enabling the RuboCop rule for method naming conventions.
|
41
|
+
# rubocop:enable Naming/MethodName
|
42
|
+
end
|
metadata
CHANGED
@@ -1,141 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: defi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Kato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: aw
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 0.2.0
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 0.2.0
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: bundler
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rake
|
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'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rubocop-md
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rubocop-performance
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: rubocop-rake
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - ">="
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: rubocop-thread_safety
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: simplecov
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
114
|
-
requirements:
|
115
|
-
- - ">="
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
requirements:
|
122
|
-
- - ">="
|
123
|
-
- !ruby/object:Gem::Version
|
124
|
-
version: '0'
|
125
|
-
- !ruby/object:Gem::Dependency
|
126
|
-
name: yard
|
127
|
-
requirement: !ruby/object:Gem::Requirement
|
128
|
-
requirements:
|
129
|
-
- - ">="
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
version: '0'
|
132
|
-
type: :development
|
133
|
-
prerelease: false
|
134
|
-
version_requirements: !ruby/object:Gem::Requirement
|
135
|
-
requirements:
|
136
|
-
- - ">="
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
version: '0'
|
11
|
+
date: 2024-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
139
13
|
description: Challenge library.
|
140
14
|
email: contact@cyril.email
|
141
15
|
executables: []
|
@@ -145,8 +19,9 @@ files:
|
|
145
19
|
- LICENSE.md
|
146
20
|
- README.md
|
147
21
|
- lib/defi.rb
|
148
|
-
- lib/defi/
|
22
|
+
- lib/defi/method.rb
|
149
23
|
- lib/defi/value.rb
|
24
|
+
- lib/kernel.rb
|
150
25
|
homepage: https://github.com/fixrb/defi
|
151
26
|
licenses:
|
152
27
|
- MIT
|
@@ -160,14 +35,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
35
|
requirements:
|
161
36
|
- - ">="
|
162
37
|
- !ruby/object:Gem::Version
|
163
|
-
version: 2.
|
38
|
+
version: 3.2.0
|
164
39
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
40
|
requirements:
|
166
41
|
- - ">="
|
167
42
|
- !ruby/object:Gem::Version
|
168
43
|
version: '0'
|
169
44
|
requirements: []
|
170
|
-
rubygems_version: 3.
|
45
|
+
rubygems_version: 3.4.19
|
171
46
|
signing_key:
|
172
47
|
specification_version: 4
|
173
48
|
summary: Challenge library.
|
data/lib/defi/challenge.rb
DELETED
@@ -1,99 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "aw"
|
4
|
-
|
5
|
-
module Defi
|
6
|
-
# This class contains a challenge to apply against an object.
|
7
|
-
class Challenge < ::BasicObject
|
8
|
-
# Initialize the challenge class.
|
9
|
-
#
|
10
|
-
# @param method [#to_sym] The method to send to an object.
|
11
|
-
# @param args [Array] Any arguments of the method.
|
12
|
-
# @param opts [Hash] Any keyword arguments of the method.
|
13
|
-
# @param block [Proc] Any block argument of the method.
|
14
|
-
def initialize(method, *args, **opts, &block)
|
15
|
-
@method = method.to_sym
|
16
|
-
@args = args
|
17
|
-
@opts = opts
|
18
|
-
@block = block
|
19
|
-
end
|
20
|
-
|
21
|
-
# @param object [#object_id] The object to challenge.
|
22
|
-
#
|
23
|
-
# @return [Defi::Value] The actual value, to raise or to return.
|
24
|
-
def to(object)
|
25
|
-
Value.new { object.public_send(@method, *@args, **@opts, &@block) }
|
26
|
-
end
|
27
|
-
|
28
|
-
# @param object [#object_id] The object to challenge in code isolation.
|
29
|
-
#
|
30
|
-
# @return [Defi::Value] The actual value, to raise or to return.
|
31
|
-
#
|
32
|
-
# @see to
|
33
|
-
def to!(object)
|
34
|
-
::Aw.fork! { to(object) }
|
35
|
-
end
|
36
|
-
|
37
|
-
# Properties of the challenge.
|
38
|
-
#
|
39
|
-
# @return [Hash] The properties of the challenge.
|
40
|
-
def to_h
|
41
|
-
{
|
42
|
-
method: @method,
|
43
|
-
args: @args,
|
44
|
-
opts: @opts,
|
45
|
-
block: @block
|
46
|
-
}
|
47
|
-
end
|
48
|
-
|
49
|
-
# rubocop:disable Metrics/AbcSize
|
50
|
-
# rubocop:disable Metrics/CyclomaticComplexity
|
51
|
-
# rubocop:disable Metrics/MethodLength
|
52
|
-
|
53
|
-
# String of the challenge.
|
54
|
-
#
|
55
|
-
# @return [String] The string representation of the challenge.
|
56
|
-
def to_s
|
57
|
-
string = ".#{@method}"
|
58
|
-
|
59
|
-
return string if @args.empty? && @opts.empty? && @block.nil?
|
60
|
-
|
61
|
-
stringified_args = @args.inspect[1..-2]
|
62
|
-
stringified_opts = @opts.inspect[1..-2]
|
63
|
-
stringified_block = "<Proc>" unless @block.nil?
|
64
|
-
|
65
|
-
string += "("
|
66
|
-
|
67
|
-
stringified_items = []
|
68
|
-
|
69
|
-
stringified_items << stringified_args unless @args.empty?
|
70
|
-
stringified_items << stringified_opts unless @opts.empty?
|
71
|
-
stringified_items << stringified_block unless @block.nil?
|
72
|
-
|
73
|
-
"#{string}#{stringified_items.join(", ")})"
|
74
|
-
end
|
75
|
-
|
76
|
-
# rubocop:enable Metrics/AbcSize
|
77
|
-
# rubocop:enable Metrics/CyclomaticComplexity
|
78
|
-
# rubocop:enable Metrics/MethodLength
|
79
|
-
|
80
|
-
# A string containing a human-readable representation of the challenge.
|
81
|
-
#
|
82
|
-
# @return [String] The human-readable representation of the challenge.
|
83
|
-
def inspect
|
84
|
-
inspected_method = @method.inspect
|
85
|
-
inspected_args = @args.inspect
|
86
|
-
inspected_opts = @opts.inspect
|
87
|
-
inspected_block = @block.nil? ? "nil" : "<Proc>"
|
88
|
-
|
89
|
-
"Defi(" \
|
90
|
-
"method: #{inspected_method}, " \
|
91
|
-
"args: #{inspected_args}, " \
|
92
|
-
"opts: #{inspected_opts}, " \
|
93
|
-
"block: #{inspected_block}" \
|
94
|
-
")"
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
require_relative "value"
|