invokable 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +23 -1
- data/lib/invokable.rb +11 -3
- data/lib/invokable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4908901ef4becc2a2de44b1ce8a23d656ac5f2a5b00b3122b59d087fdc30e7c7
|
4
|
+
data.tar.gz: a042c77682ff40a6d481d14cb20840113eac45757b21c13ec0e2e40f52abdad5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e320d788d71a963d844f262a56a3c9413a016b89f050a9249d17e5cd57157d39588410f190f0b85b682c91b11c183597955a72ea508cea0460dcf1f033adf86
|
7
|
+
data.tar.gz: f7b08dcbf9311e95791c98df269e2533ace49abc7aba539c767aa4755db60e3d81754f4e65574094ede2f01c621167cd5270b135ea5329b36810441cf49e7dfb
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -60,6 +60,28 @@ A function is a mapping of one value to another with the additional constraint t
|
|
60
60
|
always get the same output value. So, conceptually, Ruby Hashes, Arrays, and Sets are all functions. Also, there are
|
61
61
|
many one method objects out there (e.g. ServiceObjects) that are essentially functions. Why not treat them as such?
|
62
62
|
|
63
|
+
# API
|
64
|
+
|
65
|
+
## to_proc -> a_proc
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
hash = { a: 1, b, 2 }
|
69
|
+
hash.class # => Hash
|
70
|
+
hash.to_proc.class # => Proc
|
71
|
+
[:a, :b].map(&hash) # => [1, 2]
|
72
|
+
```
|
73
|
+
|
74
|
+
Convert an object into a proc. When the `Invokable` module is included in a class it will do this by
|
75
|
+
returning a proc that passes it's arguments to the object's `call` method. When `invokable/data` is
|
76
|
+
loaded `Hash#call` is mapped to `Hash#dig`, `Array#call` is mapped to `Array#at`, and `Set#call`
|
77
|
+
is mapped to `Set#include?`.
|
78
|
+
|
79
|
+
## curry -> a_proc
|
80
|
+
## curry(arity) -> a_proc
|
81
|
+
|
82
|
+
Returns a curried proc. If the `arity` is given, it determines the number of arguments.
|
83
|
+
(see [Proc#curry](https://ruby-doc.org/core-2.7.0/Proc.html#method-i-curry)).
|
84
|
+
|
63
85
|
# Installation
|
64
86
|
|
65
87
|
Add this line to your application's Gemfile:
|
@@ -83,7 +105,7 @@ Or install it yourself as:
|
|
83
105
|
|
84
106
|
# TODO
|
85
107
|
|
86
|
-
- add support for `
|
108
|
+
- add support for `memoize` and maybe transducers.
|
87
109
|
- benchmark Invokable#to_proc
|
88
110
|
|
89
111
|
# License
|
data/lib/invokable.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'invokable/version'
|
2
2
|
|
3
|
-
# TODO: Add
|
3
|
+
# TODO: Add memoize, transducers?
|
4
4
|
module Invokable
|
5
5
|
# If object responds to `call` convert into a Proc forwards it's arguments along to `call`.
|
6
6
|
#
|
7
|
+
# @see [Proc#call]
|
7
8
|
# @return [Proc]
|
8
9
|
def to_proc
|
9
10
|
if respond_to?(:call)
|
@@ -16,7 +17,14 @@ module Invokable
|
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
|
20
|
-
|
20
|
+
# Return a curried proc. If the optional arity argument is given, it determines the number of arguments.
|
21
|
+
# A curried proc receives some arguments. If a sufficient number of arguments are supplied, it passes the
|
22
|
+
# supplied arguments to the original proc and returns the result. Otherwise, returns another curried proc
|
23
|
+
# that takes the rest of arguments.
|
24
|
+
#
|
25
|
+
# @see [Proc#curry]
|
26
|
+
# @return [Proc]
|
27
|
+
def curry(*args)
|
28
|
+
to_proc.curry(*args)
|
21
29
|
end
|
22
30
|
end
|
data/lib/invokable/version.rb
CHANGED