piped_ruby 0.1.0 → 0.2.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
  SHA1:
3
- metadata.gz: 005c1764bc7845f7f02b2828042107818c715627
4
- data.tar.gz: 07a97f6a1b1dfdeb7079a9ae1aeac0e76d75aba9
3
+ metadata.gz: a304ea3db4e8015621d0464fb87513565ec11037
4
+ data.tar.gz: 808ec638afd890440f88bb37081ba72c4cbdc89a
5
5
  SHA512:
6
- metadata.gz: 8ae57b57993db4e0f843c252698a348230d962a8429e092d1639da3e541acd746088f49e81c82094b40cb876eb3adadf1d3051f90afad9d67476572d2f45158c
7
- data.tar.gz: d01316c53748dc6461dd503efa5ef36ad8f9d8f4d7c336f929df0399f63e5e6c4c0001e2827f87b674c6785967a4005dfe0a823a9ef436a0237cfa7885da445c
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
- .| { |e| puts e } # Pipe things in Ruby!
6
+ .unwrap #=> "Pipe things in Ruby!"
7
7
  ```
8
8
 
9
- Piped Ruby is a tiny piece of code that brings an awesome feature to Ruby: pipe operators.
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 pipe operator](http://elixir-lang.org/getting-started/enumerables-and-streams.html#the-pipe-operator) that is the source of inspiration for this gem :-)
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
- Print max element of a given array:
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
- array = [1, 3, 2, 5]
40
- -> { array }.| { |e| e.length == 4 ? e.push(4) : e }
41
- .| { |e| e.max }
42
- .| { |e| puts e } # 5
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
- Making some fun with strings:
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 }.| { |e| e + 1 }
55
- .| { |e| e * 21 }
56
- .| { |e| Foo.answer(e) }
57
- .| { |e| e + ' :-)' }
58
- .| { |e| puts(e) } # So the answer of the life, the universe and everything is... 42! :-)"
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
 
@@ -1,3 +1,3 @@
1
1
  module PipedRuby
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/piped_ruby.rb CHANGED
@@ -3,7 +3,7 @@ require 'piped_ruby/version'
3
3
  module PipedRuby
4
4
  attr_accessor :piped
5
5
 
6
- def |(&chained)
6
+ def >>(&chained)
7
7
  self.piped = call if piped.nil?
8
8
  chained.piped = chained.call(piped)
9
9
  chained
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.1.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-06-01 00:00:00.000000000 Z
11
+ date: 2016-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler