invokable 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/Gemfile.lock +11 -1
- data/README.md +15 -12
- data/invokable.gemspec +1 -1
- data/lib/invokable.rb +10 -1
- data/lib/invokable/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4908866aac724a3af3d142852f6412f3cc93ee7ab8c85f2dd856b08f0039c6c
|
4
|
+
data.tar.gz: bf5f3d83df9c69f5357eacef3bdc2ecc44384852124f4fc012fa6d3e14716cd5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f71bcec50cf1e9d0049259803eae9d5b19e102f78f14fe0854d5e2c238c3b7679552d64bb66608305c497379418f717fa29a0fe1d921c835c3397a58b37d5a47
|
7
|
+
data.tar.gz: 23827b487b5bb767871585d3bb641f53e8aaa4f28c1e4ef1934e93f200b7665a4a62a34f855767a7bc5424fa472ca62854021e4726b05c0901618ca119a3030c
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,18 +1,27 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
invokable (0.
|
4
|
+
invokable (0.3.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
coderay (1.1.2)
|
10
|
+
concurrent-ruby (1.1.5)
|
10
11
|
diff-lcs (1.3)
|
12
|
+
faker (1.9.6)
|
13
|
+
i18n (>= 0.7)
|
14
|
+
gen-test (0.1.1)
|
15
|
+
faker (~> 1.9.6)
|
16
|
+
regexp-examples (~> 1.5.0)
|
17
|
+
i18n (1.8.2)
|
18
|
+
concurrent-ruby (~> 1.0)
|
11
19
|
method_source (0.9.2)
|
12
20
|
pry (0.12.2)
|
13
21
|
coderay (~> 1.1.0)
|
14
22
|
method_source (~> 0.9.0)
|
15
23
|
rake (10.5.0)
|
24
|
+
regexp-examples (1.5.1)
|
16
25
|
rspec (3.9.0)
|
17
26
|
rspec-core (~> 3.9.0)
|
18
27
|
rspec-expectations (~> 3.9.0)
|
@@ -33,6 +42,7 @@ PLATFORMS
|
|
33
42
|
|
34
43
|
DEPENDENCIES
|
35
44
|
bundler (~> 1.17)
|
45
|
+
gen-test
|
36
46
|
invokable!
|
37
47
|
pry
|
38
48
|
rake (~> 10.0)
|
data/README.md
CHANGED
@@ -3,9 +3,9 @@
|
|
3
3
|
|
4
4
|
# Invokable
|
5
5
|
|
6
|
-
Treat any Object, Hashes, Arrays, and Sets as Procs (like Enumerable but for Procs)
|
6
|
+
Objects are functions! Treat any Object, Hashes, Arrays, and Sets as Procs (like Enumerable but for Procs)
|
7
7
|
|
8
|
-
|
8
|
+
## Synopsis
|
9
9
|
|
10
10
|
```ruby
|
11
11
|
require 'invokable/hash'
|
@@ -40,7 +40,7 @@ Treat any Object, Hashes, Arrays, and Sets as Procs (like Enumerable but for Pro
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
data_for_user = GetDataFromSomeService.new
|
43
|
+
data_for_user = GetDataFromSomeService.new.memoize # 'memoize' makes a proc that caches results
|
44
44
|
User.all.map(&data_for_user)
|
45
45
|
```
|
46
46
|
|
@@ -54,15 +54,15 @@ require 'invokable/set' # loads set patch
|
|
54
54
|
require 'invokable/data' # loads all patches
|
55
55
|
```
|
56
56
|
|
57
|
-
|
57
|
+
## Why?
|
58
58
|
|
59
59
|
A function is a mapping of one value to another with the additional constraint that for the one input value you will
|
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
|
-
|
63
|
+
## API
|
64
64
|
|
65
|
-
|
65
|
+
### `to_proc => Proc`
|
66
66
|
|
67
67
|
```ruby
|
68
68
|
hash = { a: 1, b, 2 }
|
@@ -76,12 +76,16 @@ returning a proc that passes it's arguments to the object's `call` method. When
|
|
76
76
|
loaded `Hash#call` is mapped to `Hash#dig`, `Array#call` is mapped to `Array#at`, and `Set#call`
|
77
77
|
is mapped to `Set#include?`.
|
78
78
|
|
79
|
-
|
79
|
+
### `curry(arity = nil) => Proc`
|
80
80
|
|
81
81
|
Returns a curried proc. If the `arity` is given, it determines the number of arguments.
|
82
82
|
(see [Proc#curry](https://ruby-doc.org/core-2.7.0/Proc.html#method-i-curry)).
|
83
83
|
|
84
|
-
|
84
|
+
### `memoize => Proc`
|
85
|
+
|
86
|
+
Returns a memoized proc, that is, a proc that caches it return values by it's arguments.
|
87
|
+
|
88
|
+
## Installation
|
85
89
|
|
86
90
|
Add this line to your application's Gemfile:
|
87
91
|
|
@@ -97,16 +101,15 @@ Or install it yourself as:
|
|
97
101
|
|
98
102
|
> gem install invokable
|
99
103
|
|
100
|
-
|
104
|
+
## See Also
|
101
105
|
|
102
106
|
- [Clojure](https://clojure.org)
|
103
107
|
- [Arc](http://www.arclanguage.org)
|
104
108
|
|
105
|
-
|
109
|
+
## TODO
|
106
110
|
|
107
|
-
- add support for `memoize` and maybe transducers.
|
108
111
|
- benchmark Invokable#to_proc
|
109
112
|
|
110
|
-
|
113
|
+
## License
|
111
114
|
|
112
115
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/invokable.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ["Delon Newman"]
|
9
9
|
spec.email = ["contact@delonnewman.name"]
|
10
10
|
|
11
|
-
spec.summary = %q{Treat any Object, Hashes, Arrays and Sets as Procs (like Enumerable but for Proc-like objects)}
|
11
|
+
spec.summary = %q{Objects are functions! Treat any Object, Hashes, Arrays and Sets as Procs (like Enumerable but for Proc-like objects)}
|
12
12
|
spec.description = spec.summary
|
13
13
|
spec.homepage = "https://github.com/delonnewman/invokable"
|
14
14
|
spec.license = "MIT"
|
data/lib/invokable.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'invokable/version'
|
2
2
|
|
3
|
-
# TODO: Add memoize, transducers?
|
4
3
|
module Invokable
|
5
4
|
# If object responds to `call` convert into a Proc forwards it's arguments along to `call`.
|
6
5
|
#
|
@@ -28,4 +27,14 @@ module Invokable
|
|
28
27
|
def curry(arity = nil)
|
29
28
|
to_proc.curry(arity)
|
30
29
|
end
|
30
|
+
|
31
|
+
# Return a memoized proc, that is, a proc that caches it's return values by it's arguments.
|
32
|
+
#
|
33
|
+
# @return [Proc]
|
34
|
+
def memoize
|
35
|
+
Proc.new do |*args|
|
36
|
+
@memo ||= {}
|
37
|
+
@memo[args.hash] ||= call(*args)
|
38
|
+
end
|
39
|
+
end
|
31
40
|
end
|
data/lib/invokable/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: invokable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Delon Newman
|
@@ -52,8 +52,8 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
description: Treat any Object, Hashes, Arrays and Sets as Procs
|
56
|
-
for Proc-like objects)
|
55
|
+
description: Objects are functions! Treat any Object, Hashes, Arrays and Sets as Procs
|
56
|
+
(like Enumerable but for Proc-like objects)
|
57
57
|
email:
|
58
58
|
- contact@delonnewman.name
|
59
59
|
executables: []
|
@@ -102,6 +102,6 @@ requirements: []
|
|
102
102
|
rubygems_version: 3.0.6
|
103
103
|
signing_key:
|
104
104
|
specification_version: 4
|
105
|
-
summary: Treat any Object, Hashes, Arrays and Sets as Procs
|
106
|
-
Proc-like objects)
|
105
|
+
summary: Objects are functions! Treat any Object, Hashes, Arrays and Sets as Procs
|
106
|
+
(like Enumerable but for Proc-like objects)
|
107
107
|
test_files: []
|