mighty_tap 0.2.0 → 0.3.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/README.md +70 -5
- data/bin/console +15 -0
- data/bin/setup +7 -0
- data/lib/mighty_tap/version.rb +1 -1
- data/lib/mighty_tap.rb +8 -12
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc6bf0254dafee2cfd74c1ddd10bc2e9f60195ea
|
4
|
+
data.tar.gz: 5105254ec2a78d5ce987b5afc9a3cda4b7495314
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19c0b1e0aa425751cf215722cfff5edd82151c30f9193cabc47d0d2def58b348d45abd498843ffa567bf1786ccdca1c9304eb8be042f2992f926ec07b8f233f8
|
7
|
+
data.tar.gz: ed222ab149152578c2b14f97b904a6f8ca9ea5a5b26a87a9bf98389e5ebd491b36fd618b6347b8c5af82ced65a4c771014c0bc4997a125c2179b81aa8982b121
|
data/README.md
CHANGED
@@ -1,4 +1,73 @@
|
|
1
|
-
#
|
1
|
+
# mighty_tap
|
2
|
+
|
3
|
+
Rubys `tap` is a awesome. mighty_tap tries to make it even more awesome by adding some missing features, while maintining full compatibility to the orginal `tap`. In order to make its usage more pleasant, `mighty_tap` is defined as in instance method on `Object` and aliased to `mtap`.
|
4
|
+
|
5
|
+
## What is it even more awesome than `tap` ?
|
6
|
+
* you can give it a method name
|
7
|
+
* you can give it arguments and blocks for methods to call
|
8
|
+
* dispite calling methods on the object itself, you can provide a callable
|
9
|
+
* in fact you can provide anything that responds to :call
|
10
|
+
* dispite the added features it acts like the original `tap` (can act as drop-in replacement)
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
require "mighty_tap"
|
16
|
+
|
17
|
+
#
|
18
|
+
# it can be used just like tap
|
19
|
+
#
|
20
|
+
[[[1,2,3]]].mtap(&:flatten!) # => [1,2,3]
|
21
|
+
|
22
|
+
#
|
23
|
+
# dispite the implicite &: block syntax, it can take a method name
|
24
|
+
#
|
25
|
+
[[[1,2,3]]].mtap(:flatten!) # => [1,2,3]
|
26
|
+
|
27
|
+
#
|
28
|
+
# it also takes method arguments
|
29
|
+
#
|
30
|
+
[[[1,2,3]]].mtap(:flatten!, 1) # => [[1,2,3]]
|
31
|
+
|
32
|
+
#
|
33
|
+
# if the last argument is a proc, the method is called with the procs block variant
|
34
|
+
#
|
35
|
+
[1,2,3].mtap(:map!, -> (number) { number * 2 }) => [2,4,6]
|
36
|
+
|
37
|
+
#
|
38
|
+
# you can also give it a callable (something that responds to #call)
|
39
|
+
#
|
40
|
+
class ArrayDoubler
|
41
|
+
def call(array)
|
42
|
+
array.map! { |element| element * 2 }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
[1,2,3].mtap(ArrayDoubler.new) # => [2,4,6]
|
47
|
+
|
48
|
+
#
|
49
|
+
# callables can have arguments and blocks, too
|
50
|
+
#
|
51
|
+
class ArrayMultiplier
|
52
|
+
def call(array, factor, &reducer)
|
53
|
+
multiplied_array = array.map! { |element| element * factor }
|
54
|
+
|
55
|
+
if block_given?
|
56
|
+
yield multiplied_array
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
[1,2,3].mtap(ArrayMultiplier.new, 3) # => [3,6,9]
|
62
|
+
[1,2,3].mtap(ArrayMultiplier.new, 3, -> (array) { array.delete_if { |int| int < 9 } }) # => [9]
|
63
|
+
|
64
|
+
#
|
65
|
+
# this can all be combinded with taps original block syntax
|
66
|
+
#
|
67
|
+
[1,2,3].mtap(ArrayDoubler.new) do |doubled_array|
|
68
|
+
doubled_array.map! { |element| element * element }
|
69
|
+
end # => [4, 16, 36]
|
70
|
+
```
|
2
71
|
|
3
72
|
## Installation
|
4
73
|
|
@@ -16,10 +85,6 @@ Or install it yourself as:
|
|
16
85
|
|
17
86
|
$ gem install mighty_tap
|
18
87
|
|
19
|
-
## Usage
|
20
|
-
|
21
|
-
TODO: Write usage instructions here
|
22
|
-
|
23
88
|
## Development
|
24
89
|
|
25
90
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mighty_tap"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
begin
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
12
|
+
rescue LoadError
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
15
|
+
end
|
data/bin/setup
ADDED
data/lib/mighty_tap/version.rb
CHANGED
data/lib/mighty_tap.rb
CHANGED
@@ -2,18 +2,14 @@ require "mighty_tap/version"
|
|
2
2
|
|
3
3
|
class Object
|
4
4
|
def mighty_tap(*args, &block)
|
5
|
-
if args.length
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
self.send(args[0], args[1])
|
14
|
-
end
|
15
|
-
elsif args.length > 2
|
16
|
-
self.send(args[0], *args[1..-1])
|
5
|
+
if args.length > 1 && args.last.is_a?(Proc)
|
6
|
+
method_block_proc = args.pop
|
7
|
+
end
|
8
|
+
|
9
|
+
if args[0].is_a?(String) || args[0].is_a?(Symbol)
|
10
|
+
self.send(args[0], *args[1..-1], &method_block_proc)
|
11
|
+
elsif args[0].respond_to?(:call)
|
12
|
+
args[0].call(self, *args[1..-1], &method_block_proc)
|
17
13
|
end
|
18
14
|
|
19
15
|
if block_given?
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mighty_tap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Sievers
|
@@ -85,6 +85,8 @@ files:
|
|
85
85
|
- LICENSE.txt
|
86
86
|
- README.md
|
87
87
|
- Rakefile
|
88
|
+
- bin/console
|
89
|
+
- bin/setup
|
88
90
|
- lib/mighty_tap.rb
|
89
91
|
- lib/mighty_tap/version.rb
|
90
92
|
- mighty_tap.gemspec
|