invokable 0.3.0 → 0.4.0
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 +26 -18
- data/lib/invokable.rb +10 -35
- data/lib/invokable/array.rb +2 -2
- data/lib/invokable/compose.rb +25 -0
- data/lib/invokable/core.rb +40 -0
- data/lib/invokable/data.rb +1 -1
- data/lib/invokable/hash.rb +5 -3
- data/lib/invokable/method.rb +5 -0
- data/lib/invokable/proc.rb +5 -0
- data/lib/invokable/set.rb +2 -2
- data/lib/invokable/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8be1208734fdbbf1e11e1974834bfb6a65e294c7814a7df1f65775b4a898ae9
|
4
|
+
data.tar.gz: 2693e344b7eb7c1e9e12615c1c6831b6c842219adf39e70aa924bbd65c7edaee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bc791de5268235f8f7f2715e14f7d14bd6d6155f38aafa85f5a976e037a81b0df57e03def253fb9d55d3881e9845db243a6fffc723cc306d2536b0daa15c931
|
7
|
+
data.tar.gz: e36ab920d191d24022954f4cd4d055db571390a7e7e0252a8ad38b456283b344678d534bebf58cbe96b0653af2c10e745ca96de0969a10d8371da4b54c361d48
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -47,18 +47,34 @@ Objects are functions! Treat any Object, Hashes, Arrays, and Sets as Procs (like
|
|
47
47
|
Use as much or a little as you need:
|
48
48
|
|
49
49
|
```ruby
|
50
|
-
require 'invokable'
|
51
|
-
require 'invokable/hash'
|
52
|
-
require 'invokable/array'
|
53
|
-
require 'invokable/set'
|
54
|
-
require 'invokable/data'
|
50
|
+
require 'invokable' # loads Invokable module
|
51
|
+
require 'invokable/hash' # loads hash patch
|
52
|
+
require 'invokable/array' # loads array patch
|
53
|
+
require 'invokable/set' # loads set patch
|
54
|
+
require 'invokable/data' # loads hash, array, and set 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
|
-
many one method objects out there (e.g.
|
61
|
+
many one method objects out there (e.g. Service Objects) that are essentially functions. Why not treat them as such?
|
62
|
+
|
63
|
+
## Installation
|
64
|
+
|
65
|
+
Add this line to your application's Gemfile:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
gem 'invokable'
|
69
|
+
```
|
70
|
+
|
71
|
+
And then execute:
|
72
|
+
|
73
|
+
> bundle
|
74
|
+
|
75
|
+
Or install it yourself as:
|
76
|
+
|
77
|
+
> gem install invokable
|
62
78
|
|
63
79
|
## API
|
64
80
|
|
@@ -85,21 +101,13 @@ Returns a curried proc. If the `arity` is given, it determines the number of arg
|
|
85
101
|
|
86
102
|
Returns a memoized proc, that is, a proc that caches it return values by it's arguments.
|
87
103
|
|
88
|
-
|
89
|
-
|
90
|
-
Add this line to your application's Gemfile:
|
91
|
-
|
92
|
-
```ruby
|
93
|
-
gem 'invokable'
|
94
|
-
```
|
95
|
-
|
96
|
-
And then execute:
|
104
|
+
### `<<(invokable) => Proc`
|
97
105
|
|
98
|
-
|
106
|
+
Returns a proc that is a composition of this invokable and the given invokable.
|
99
107
|
|
100
|
-
|
108
|
+
### `>>(invokable) => Proc`
|
101
109
|
|
102
|
-
|
110
|
+
Returns a proc that is a composition of this invokable and the given invokable.
|
103
111
|
|
104
112
|
## See Also
|
105
113
|
|
data/lib/invokable.rb
CHANGED
@@ -1,40 +1,15 @@
|
|
1
1
|
require 'invokable/version'
|
2
|
+
require 'invokable/core'
|
3
|
+
require 'invokable/compose'
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
# @return [Proc]
|
8
|
-
def to_proc
|
9
|
-
if respond_to?(:call)
|
10
|
-
# TODO: Would method(:call) be more performant? We need benchmarks.
|
11
|
-
Proc.new do |*args|
|
12
|
-
call(*args)
|
13
|
-
end
|
14
|
-
else
|
15
|
-
raise "Don't know how to convert #{self.inspect} into a Proc"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
# Return a curried proc. If the optional `arity` argument is given, it determines the number of arguments.
|
20
|
-
# A curried proc receives some arguments. If a sufficient number of arguments are supplied, it passes the
|
21
|
-
# supplied arguments to the original proc and returns the result. Otherwise, returns another curried proc
|
22
|
-
# that takes the rest of arguments.
|
23
|
-
#
|
24
|
-
# @see https://ruby-doc.org/core-2.7.0/Proc.html#method-i-curry Proc#curry
|
25
|
-
# @param arity [Number]
|
26
|
-
# @return [Proc]
|
27
|
-
def curry(arity = nil)
|
28
|
-
to_proc.curry(arity)
|
29
|
-
end
|
5
|
+
if RUBY_VERSION.split('.').take(2).join('.').to_f < 2.6
|
6
|
+
require 'invokable/proc'
|
7
|
+
require 'invokable/method'
|
8
|
+
end
|
30
9
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
Proc.new do |*args|
|
36
|
-
@memo ||= {}
|
37
|
-
@memo[args.hash] ||= call(*args)
|
38
|
-
end
|
10
|
+
module Invokable
|
11
|
+
def self.included(base)
|
12
|
+
base.include(Invokable::Core)
|
13
|
+
base.include(Invokable::Compose)
|
39
14
|
end
|
40
15
|
end
|
data/lib/invokable/array.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require_relative '
|
1
|
+
require_relative 'core'
|
2
2
|
|
3
3
|
# Extend core Array object by aliasing it's `[]` method as `call`,
|
4
4
|
# and including the `Invokable` module.
|
5
5
|
#
|
6
6
|
# @see https://ruby-doc.org/core-2.7.0/Array.html#method-i-5B-5D Array#[]
|
7
7
|
class Array
|
8
|
-
include Invokable
|
8
|
+
include Invokable::Core
|
9
9
|
alias call []
|
10
10
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Invokable
|
2
|
+
module Compose
|
3
|
+
# Return a proc that is the composition of this invokable and the given `invokable`.
|
4
|
+
# The returned proc takes a variable number of arguments, calls `invokable` with
|
5
|
+
# them then calls this proc with the result.
|
6
|
+
#
|
7
|
+
# @return [Proc]
|
8
|
+
def <<(invokable)
|
9
|
+
Proc.new do |*args|
|
10
|
+
call(invokable.call(*args))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Return a proc that is the composition of this invokable and the given `invokable`.
|
15
|
+
# The returned proc takes a variable number of arguments, calls `invokable` with
|
16
|
+
# them then calls this proc with the result.
|
17
|
+
#
|
18
|
+
# @return [Proc]
|
19
|
+
def >>(invokable)
|
20
|
+
Proc.new do |*args|
|
21
|
+
invokable.call(call(*args))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Invokable
|
2
|
+
module Core
|
3
|
+
# If object responds to `call` convert into a Proc forwards it's arguments along to `call`.
|
4
|
+
#
|
5
|
+
# @see https://ruby-doc.org/core-2.7.0/Proc.html#method-i-call Proc#call
|
6
|
+
# @return [Proc]
|
7
|
+
def to_proc
|
8
|
+
if respond_to?(:call)
|
9
|
+
# TODO: Would method(:call) be more performant? We need benchmarks.
|
10
|
+
Proc.new do |*args|
|
11
|
+
call(*args)
|
12
|
+
end
|
13
|
+
else
|
14
|
+
raise "Don't know how to convert #{self.inspect} into a Proc"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Return a curried proc. If the optional `arity` argument is given, it determines the number of arguments.
|
19
|
+
# A curried proc receives some arguments. If a sufficient number of arguments are supplied, it passes the
|
20
|
+
# supplied arguments to the original proc and returns the result. Otherwise, returns another curried proc
|
21
|
+
# that takes the rest of arguments.
|
22
|
+
#
|
23
|
+
# @see https://ruby-doc.org/core-2.7.0/Proc.html#method-i-curry Proc#curry
|
24
|
+
# @param arity [Integer]
|
25
|
+
# @return [Proc]
|
26
|
+
def curry(arity = nil)
|
27
|
+
to_proc.curry(arity)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Return a memoized proc, that is, a proc that caches it's return values by it's arguments.
|
31
|
+
#
|
32
|
+
# @return [Proc]
|
33
|
+
def memoize
|
34
|
+
Proc.new do |*args|
|
35
|
+
@memo ||= {}
|
36
|
+
@memo[args.hash] ||= call(*args)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/invokable/data.rb
CHANGED
data/lib/invokable/hash.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
-
require_relative '
|
1
|
+
require_relative 'core'
|
2
2
|
|
3
3
|
# Extend core Hash object by aliasing it's `dig` method as `call`,
|
4
4
|
# and including the `Invokable` module.
|
5
5
|
#
|
6
6
|
# @see https://ruby-doc.org/core-2.7.0/Hash.html#method-i-dig Hash#dig
|
7
7
|
class Hash
|
8
|
-
|
9
|
-
|
8
|
+
if RUBY_VERSION.split('.').take(2).join('.').to_f < 2.7
|
9
|
+
include Invokable::Core
|
10
|
+
alias call dig
|
11
|
+
end
|
10
12
|
end
|
data/lib/invokable/set.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'set'
|
2
|
-
require_relative '
|
2
|
+
require_relative 'core'
|
3
3
|
|
4
4
|
# Extend stdlib Set object by aliasing it's `include?` method as `call`,
|
5
5
|
# and including the `Invokable` module.
|
6
6
|
#
|
7
7
|
# @see https://ruby-doc.org/stdlib-2.7.0/libdoc/set/rdoc/Set.html#method-i-include-3F Set#include?
|
8
8
|
class Set
|
9
|
-
include Invokable
|
9
|
+
include Invokable::Core
|
10
10
|
alias call include?
|
11
11
|
end
|
data/lib/invokable/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: invokable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Delon Newman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-02-
|
11
|
+
date: 2020-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,8 +71,12 @@ files:
|
|
71
71
|
- invokable.gemspec
|
72
72
|
- lib/invokable.rb
|
73
73
|
- lib/invokable/array.rb
|
74
|
+
- lib/invokable/compose.rb
|
75
|
+
- lib/invokable/core.rb
|
74
76
|
- lib/invokable/data.rb
|
75
77
|
- lib/invokable/hash.rb
|
78
|
+
- lib/invokable/method.rb
|
79
|
+
- lib/invokable/proc.rb
|
76
80
|
- lib/invokable/set.rb
|
77
81
|
- lib/invokable/version.rb
|
78
82
|
homepage: https://github.com/delonnewman/invokable
|