piped_ruby 0.1.0 → 0.2.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 +67 -14
- data/lib/piped_ruby/version.rb +1 -1
- data/lib/piped_ruby.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a304ea3db4e8015621d0464fb87513565ec11037
|
4
|
+
data.tar.gz: 808ec638afd890440f88bb37081ba72c4cbdc89a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abb245a6fd266c16899e7341e8b206bcb0996457b2a4863746d25ea8cee88deb8704ca57a18c5bad6b41e74cd3e41c1b9b63744a59e98be3b8ee0f46fb61e7b3
|
7
|
+
data.tar.gz: 55661b1c7bd5da2696bf79ff2cc98954d46af5918041efaa7585206e3c31dcaf46f722082a818a80a0667b4605ec30e131924eb4d2b22125c5149edebd67c0f6
|
data/README.md
CHANGED
@@ -3,12 +3,12 @@
|
|
3
3
|
```ruby
|
4
4
|
-> { "Pipe" }.| { |e| "#{e} things" }
|
5
5
|
.| { |e| "#{e} in Ruby!" }
|
6
|
-
|
6
|
+
.unwrap #=> "Pipe things in Ruby!"
|
7
7
|
```
|
8
8
|
|
9
|
-
Piped Ruby is a tiny piece of code that
|
9
|
+
Piped Ruby is a tiny piece of code that aims to bring the beauty of pipe operators and data transformation to your Ruby application.
|
10
10
|
|
11
|
-
Credit to [Elixir's
|
11
|
+
Credit to [Elixir's Pipe Operator](http://elixir-lang.org/getting-started/enumerables-and-streams.html#the-pipe-operator) and [Chainable Methods](https://github.com/akitaonrails/chainable_methods) gem which were the source of inspiration for this gem :-)
|
12
12
|
|
13
13
|
## Installation
|
14
14
|
|
@@ -34,15 +34,44 @@ require 'piped_ruby'
|
|
34
34
|
|
35
35
|
## Usage
|
36
36
|
|
37
|
-
|
37
|
+
With Piped Ruby doing this:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
-> { some_text.upcase }.>> { |e| MyModule.method_a(e) }
|
41
|
+
.>> { |e| MyModule.method_b(e, "something") }
|
42
|
+
.>> { |e| MyModule.method_c(e) { |c| do_something3(c) } }
|
43
|
+
.unwrap
|
44
|
+
```
|
45
|
+
|
46
|
+
...is equivalent to this:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
a = some_text.upcase
|
50
|
+
b = MyModule.method_a(a)
|
51
|
+
c = MyModule.method_b(b, "something")
|
52
|
+
d = MyModule.method_c(c) { |c| do_something3(c) }
|
53
|
+
```
|
54
|
+
|
55
|
+
### More examples
|
56
|
+
|
57
|
+
Exporting clients from CSV file:
|
58
|
+
|
38
59
|
```ruby
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
60
|
+
#...
|
61
|
+
def call
|
62
|
+
File.foreach(file) do |line|
|
63
|
+
-> { values_from(line) }
|
64
|
+
.>> { |e| match_fields_for(e) }
|
65
|
+
.>> { |e| sanitize(e) }
|
66
|
+
.>> { |e| Save.new(attributes: sanitize(e)) }
|
67
|
+
.>> { |save| save.call }
|
68
|
+
.unwrap
|
69
|
+
end
|
70
|
+
end
|
43
71
|
```
|
44
72
|
|
45
|
-
|
73
|
+
Fun with strings:
|
74
|
+
|
46
75
|
```ruby
|
47
76
|
module Foo
|
48
77
|
class << self
|
@@ -51,13 +80,37 @@ module Foo
|
|
51
80
|
end
|
52
81
|
end
|
53
82
|
|
54
|
-
-> { Foo.number }
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
83
|
+
-> { Foo.number }.>> { |e| e + 1 }
|
84
|
+
.>> { |e| e * 21 }
|
85
|
+
.>> { |e| Foo.answer(e) }
|
86
|
+
.>> { |e| e + ' :-)' }
|
87
|
+
.unwrap #=> "So the answer of the life, the universe and everything is... 42! :-)"
|
88
|
+
```
|
89
|
+
|
90
|
+
|
91
|
+
## Backstage
|
92
|
+
|
93
|
+
Pipe operations happen between Proc objects (blocks). After requiring the gem every Proc in your Ruby program will be capable to start a pipe operation.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
operation = -> { 'Foo' }.>> { |e| "#{e}bar" }
|
97
|
+
operation.class #=> Proc
|
98
|
+
operation.unwrap #=> "Foobar"
|
99
|
+
```
|
100
|
+
|
101
|
+
The `PipedRuby#unwrap` method is the way to get the returning value from the last evaluated block:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
operation = -> { 'Foo' }.>> { |e| "#{e}bar" }
|
105
|
+
operation.>> { |e| puts e } #=> Prints "Foobar" and returns a Proc object
|
106
|
+
operation.>> { |e| puts e }.unwrap #=> Prints "Foobar" and returns nil
|
59
107
|
```
|
60
108
|
|
109
|
+
## TODO
|
110
|
+
|
111
|
+
- [ ] Write more cool examples in README;
|
112
|
+
- [ ] Introduce something similar to Elixir's Streams;
|
113
|
+
- [ ] Store Procs and be able to unwrap a returning value in different points of execution.
|
61
114
|
|
62
115
|
## Contributing
|
63
116
|
|
data/lib/piped_ruby/version.rb
CHANGED
data/lib/piped_ruby.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: piped_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tiago Guedes
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|