rewire 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b8c5b21459cdeb7d24dc60323d2da26c802dcf3
4
+ data.tar.gz: 1fc9b208773bd2ce473649c7e936a387443d21f4
5
+ SHA512:
6
+ metadata.gz: f8b88b42d79adb8340ad17640081bde3f8198fee3c8d412218eedc093068398413d9fe2de80996ea8c36cee32528c7350f2d1ee1a29b8aef36adecc6b1a0f5ac
7
+ data.tar.gz: a35f6758f244cc41a1aefc50be1edb93b0620911db1894e72d2c1682af95bc5f0879054ac133905dd3c44881127dd48b8a22305935546dd09aef34286dfd786f
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
File without changes
@@ -0,0 +1 @@
1
+ 2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rewire.gemspec
4
+ gemspec
@@ -0,0 +1,5 @@
1
+ guard :rspec do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ronie Uliana
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,81 @@
1
+ # Rewire
2
+
3
+ Flow Based Programming in Ruby (or "Pipes in Ruby").
4
+
5
+ (The next step after "DeadlySerious"[https://rubygems.org/gems/deadly_serious] gem)
6
+
7
+ I think I finally figured out why components in a flow based program are not "functions" or "methods". I believe the main point is the ability to pass its own result forward several times.
8
+
9
+ --------------- ---------------
10
+ | component A | --> | component B |
11
+ --------------- ---------------
12
+
13
+ "A" can call "B" an *arbitrary* number of times. That works very differently of a function or method "return"
14
+
15
+ So, adding to the other benefits, as easy parallelism and high modificability, I strongly believe the paradigm itself is really strong for pratical programs.
16
+
17
+ The main goal of this gem is to provide the foundation of a "killer application" based solely on pipes (a new web framework, probably :p). Other than that, I'm using it to study the paradigm.
18
+
19
+ The plan is to keep this gem minimal and add new features in new (other) gems.
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ gem 'rewire'
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install rewire
34
+
35
+ ## Usage
36
+
37
+ The result of the computations is always collected in an object. The object ("output" below), needs to respond to the "<<" method, so arrays, strings and IOs can be used.
38
+
39
+ ```ruby
40
+ require 'rewire'
41
+
42
+ # Declare the component.
43
+ # It receives 2 parameters, the output ("out" below) and the packet,
44
+ # meaning the data passed to the pipe.
45
+ upcaser = Rewire::Pipe.new { |out, packet| out << packet.upcase }
46
+
47
+ output = []
48
+
49
+ # Connect pipe to output.
50
+ pipe = upcaser | output
51
+
52
+ # Pass things to the pipe.
53
+ pipe << 'test'
54
+ pipe << 'another test'
55
+
56
+ # Output collects results.
57
+ puts output # => ['TEST', 'ANOTHER TEST']
58
+ ```
59
+
60
+ ### A more complex example
61
+
62
+ ```ruby
63
+ require 'rewire'
64
+
65
+ pipeline =
66
+ Pipe { |out, packet|
67
+ ```
68
+
69
+
70
+ ## TODO
71
+
72
+ - Fiber based pipes
73
+ - Process based pipes
74
+
75
+ ## Contributing
76
+
77
+ 1. Fork it ( http://github.com/<my-github-username>/rewire/fork )
78
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
79
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
80
+ 4. Push to the branch (`git push origin my-new-feature`)
81
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,10 @@
1
+ require "rewire/version"
2
+
3
+ module Rewire; end
4
+
5
+ require 'rewire/emitter'
6
+ require 'rewire/pipe'
7
+
8
+ def Pipe(&logic)
9
+ Rewire::Pipe.new(&logic)
10
+ end
@@ -0,0 +1,39 @@
1
+ module Rewire
2
+ class Emitter
3
+ def self.new(pipe, next_emitter = nil)
4
+ if next_emitter
5
+ TrunkEmitter.new(pipe, next_emitter)
6
+ else
7
+ LeafEmitter.new(pipe)
8
+ end
9
+ end
10
+ end
11
+
12
+ class LeafEmitter
13
+ def initialize(pipe)
14
+ @pipe = pipe
15
+ end
16
+
17
+ def |(pipe)
18
+ Emitter.new(@pipe, Emitter.new(pipe))
19
+ end
20
+
21
+ def <<(pack)
22
+ @pipe << pack
23
+ end
24
+ end
25
+
26
+ class TrunkEmitter
27
+ def initialize(pipe, next_emitter)
28
+ @pipe, @next_emitter = pipe, next_emitter
29
+ end
30
+
31
+ def |(pipe)
32
+ Emitter.new(@pipe, @next_emitter | pipe)
33
+ end
34
+
35
+ def <<(pack)
36
+ @pipe.process(@next_emitter, pack)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ module Rewire
2
+ class Pipe
3
+ def initialize(&logic)
4
+ @logic = logic
5
+ end
6
+
7
+ def |(pipe)
8
+ Emitter.new(self) | pipe
9
+ end
10
+
11
+ def <<(pack)
12
+ output = []
13
+ (self | output) << pack
14
+ output
15
+ end
16
+
17
+ def process(receiver, pack)
18
+ @logic[receiver, pack]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Rewire
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rewire/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rewire"
8
+ spec.version = Rewire::VERSION
9
+ spec.authors = ["Ronie Uliana"]
10
+ spec.email = ["ronie.uliana@vagas.com.br"]
11
+ spec.summary = %q{A gem to create pipelines}
12
+ spec.description = %q{Perfect to process lots of data using several processors or machines and low memory.}
13
+ spec.homepage = "http://github.com/ruliana/rewire"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "guard-rspec"
24
+
25
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rewire::Pipe do
4
+ it 'runs a simple pipe' do
5
+ upcaser = Rewire::Pipe.new { |out, packet| out << packet.upcase }
6
+
7
+ output = []
8
+ pipe = upcaser | output
9
+
10
+ pipe << 'test'
11
+ pipe << 'another test'
12
+
13
+ expect(output).to eq ['TEST', 'ANOTHER TEST']
14
+ end
15
+
16
+ it 'creates its own output (sink) when none is provided' do
17
+ pipe = Rewire::Pipe.new { |out, packet| out << packet }
18
+ output = pipe << 'test'
19
+ expect(output).to eq ['test']
20
+ end
21
+
22
+ it 'outputs empty array if no "emission" (filter)' do
23
+ output = []
24
+ pipe = Rewire::Pipe.new do |out, packet|
25
+ # no emission
26
+ end | output
27
+ pipe << 'test'
28
+ expect(output).to eq []
29
+ end
30
+
31
+ it 'emits can be used more times in a single Pipe' do
32
+ output = []
33
+ pipe = Rewire::Pipe.new do |out, packet|
34
+ out << packet
35
+ out << packet
36
+ end | output
37
+ pipe << 'test'
38
+ expect(output).to eq ['test', 'test']
39
+ end
40
+
41
+ it 'creates a pipe using the "Pipe" function' do
42
+ pipe = Pipe { |out, packet| }
43
+ expect(pipe.class).to eq Rewire::Pipe
44
+ end
45
+
46
+ it 'connects two pipes' do
47
+ output = []
48
+ pipe = Pipe { |out, packet| out << packet.upcase } | Pipe { |out, packet| out << [packet.size, packet] } | output
49
+ pipe << 'test'
50
+ expect(output).to eq [[4, 'TEST']]
51
+ end
52
+
53
+ it 'creates its own output (sink) even with connected pipes' do
54
+ pipe = Pipe { |out, packet| out << packet.upcase } | Pipe { |out, packet| out << [packet.size, packet] }
55
+ output = pipe << 'test'
56
+ expect(output).to eq [[4, 'TEST']]
57
+ end
58
+
59
+ it 'run iteratively' do
60
+ source = [1, 2, 3]
61
+ sink = []
62
+
63
+ pipe =
64
+ Pipe { |out, n| out << n * 2 } |
65
+ Pipe { |out, n| out << n + 1 } |
66
+ sink
67
+
68
+ source.each { |it| pipe << it }
69
+
70
+ expect(sink).to eq [3, 5, 7]
71
+ end
72
+
73
+ it 'compounds immutable pipes (they can be reused)' do
74
+ pipe_a =
75
+ Pipe { |out, n| out << n * 2 } |
76
+ Pipe { |out, n| out << n + 1 }
77
+
78
+ pipe_b =
79
+ Pipe { |out, n| out << n + 2 }
80
+
81
+ compound_pipe_1 = pipe_a | pipe_b
82
+ compound_pipe_2 = pipe_b | pipe_a
83
+
84
+ expect(compound_pipe_1 << 3).to eq [9]
85
+ expect(compound_pipe_2 << 3).to eq [11]
86
+ end
87
+ end
@@ -0,0 +1 @@
1
+ require 'rewire'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rewire
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ronie Uliana
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Perfect to process lots of data using several processors or machines
56
+ and low memory.
57
+ email:
58
+ - ronie.uliana@vagas.com.br
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".ruby-gemset"
65
+ - ".ruby-version"
66
+ - Gemfile
67
+ - Guardfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - lib/rewire.rb
72
+ - lib/rewire/emitter.rb
73
+ - lib/rewire/pipe.rb
74
+ - lib/rewire/version.rb
75
+ - rewire.gemspec
76
+ - spec/lib/rewire/pipe_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: http://github.com/ruliana/rewire
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A gem to create pipelines
102
+ test_files:
103
+ - spec/lib/rewire/pipe_spec.rb
104
+ - spec/spec_helper.rb