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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4908866aac724a3af3d142852f6412f3cc93ee7ab8c85f2dd856b08f0039c6c
4
- data.tar.gz: bf5f3d83df9c69f5357eacef3bdc2ecc44384852124f4fc012fa6d3e14716cd5
3
+ metadata.gz: b8be1208734fdbbf1e11e1974834bfb6a65e294c7814a7df1f65775b4a898ae9
4
+ data.tar.gz: 2693e344b7eb7c1e9e12615c1c6831b6c842219adf39e70aa924bbd65c7edaee
5
5
  SHA512:
6
- metadata.gz: f71bcec50cf1e9d0049259803eae9d5b19e102f78f14fe0854d5e2c238c3b7679552d64bb66608305c497379418f717fa29a0fe1d921c835c3397a58b37d5a47
7
- data.tar.gz: 23827b487b5bb767871585d3bb641f53e8aaa4f28c1e4ef1934e93f200b7665a4a62a34f855767a7bc5424fa472ca62854021e4726b05c0901618ca119a3030c
6
+ metadata.gz: 2bc791de5268235f8f7f2715e14f7d14bd6d6155f38aafa85f5a976e037a81b0df57e03def253fb9d55d3881e9845db243a6fffc723cc306d2536b0daa15c931
7
+ data.tar.gz: e36ab920d191d24022954f4cd4d055db571390a7e7e0252a8ad38b456283b344678d534bebf58cbe96b0653af2c10e745ca96de0969a10d8371da4b54c361d48
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- invokable (0.3.0)
4
+ invokable (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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' # 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 all patches
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. ServiceObjects) that are essentially functions. Why not treat them as such?
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
- ## Installation
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
- > bundle
106
+ Returns a proc that is a composition of this invokable and the given invokable.
99
107
 
100
- Or install it yourself as:
108
+ ### `>>(invokable) => Proc`
101
109
 
102
- > gem install invokable
110
+ Returns a proc that is a composition of this invokable and the given invokable.
103
111
 
104
112
  ## See Also
105
113
 
@@ -1,40 +1,15 @@
1
1
  require 'invokable/version'
2
+ require 'invokable/core'
3
+ require 'invokable/compose'
2
4
 
3
- module Invokable
4
- # If object responds to `call` convert into a Proc forwards it's arguments along to `call`.
5
- #
6
- # @see https://ruby-doc.org/core-2.7.0/Proc.html#method-i-call Proc#call
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
- # 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
10
+ module Invokable
11
+ def self.included(base)
12
+ base.include(Invokable::Core)
13
+ base.include(Invokable::Compose)
39
14
  end
40
15
  end
@@ -1,10 +1,10 @@
1
- require_relative '../invokable'
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
@@ -1,4 +1,4 @@
1
- require_relative '../invokable'
1
+ require_relative 'core'
2
2
  require_relative 'hash'
3
3
  require_relative 'set'
4
4
  require_relative 'array'
@@ -1,10 +1,12 @@
1
- require_relative '../invokable'
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
- include Invokable
9
- alias call dig
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
@@ -0,0 +1,5 @@
1
+ require_relative 'compose'
2
+
3
+ class Method
4
+ include Invokable::Compose
5
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'compose'
2
+
3
+ class Proc
4
+ include Invokable::Compose
5
+ end
@@ -1,11 +1,11 @@
1
1
  require 'set'
2
- require_relative '../invokable'
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
@@ -1,3 +1,3 @@
1
1
  module Invokable
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
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.3.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-10 00:00:00.000000000 Z
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