phase_shift 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in phase_shift.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jurgens du Toit
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,29 @@
1
+ # PhaseShift
2
+
3
+ A simple pipeline processing implementation similiar to rack middleware
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'phase_shift'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install phase_shift
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
3
+ require 'phase_shift'
4
+
5
+ runner = PhaseShift::Runner.new
6
+ runner.run
@@ -0,0 +1,3 @@
1
+ require 'phase_shift/version'
2
+ require 'phase_shift/runner'
3
+ require 'phase_shift/builder'
@@ -0,0 +1,30 @@
1
+ module PhaseShift
2
+ # Create a pipeline by building up stages
3
+ class Builder
4
+ attr_reader :stages
5
+
6
+ def self.parse_file(config, file = '(phase_shift)')
7
+ builder_script = ::File.read(config)
8
+ code = "PhaseShift::Builder.new {\n" + builder_script + "\n}.pipeline"
9
+ eval code, TOPLEVEL_BINDING, file, 0
10
+ end
11
+
12
+ def initialize(default_stage = nil, &block)
13
+ @default_stage = default_stage || proc { [] }
14
+ @stages = []
15
+ instance_eval(&block) if block_given?
16
+ end
17
+
18
+ def use(stage, *args)
19
+ @stages << proc { |pipeline| stage.new(pipeline, *args) }
20
+ end
21
+
22
+ def pipeline
23
+ stages.inject(@default_stage) { |a, e| e[a] }
24
+ end
25
+
26
+ def run
27
+ pipeline.call
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'builder'
2
+
3
+ module PhaseShift
4
+ # Run a defined pipeline
5
+ class Runner
6
+ def initialize(options = nil)
7
+ fail 'No specified pipeline' unless options.include? :pipeline
8
+ @options = options
9
+ end
10
+
11
+ def run
12
+ pipeline.call
13
+ end
14
+
15
+ def pipeline
16
+ @pipeline ||= Builder.parse_file "#{options[:pipeline]}.rb"
17
+ end
18
+
19
+ def options
20
+ @options ||= parse_options(ARGV)
21
+ end
22
+
23
+ def default_options
24
+ {}
25
+ end
26
+
27
+ private
28
+
29
+ def parse_options(argv)
30
+ @options = default_options
31
+ @options[:pipeline] = argv.first if argv.length > 0
32
+ @options
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,4 @@
1
+ # A simple pipeline processing implementation similiar to rack middleware
2
+ module PhaseShift
3
+ VERSION = '0.0.1'
4
+ 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 'phase_shift/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'phase_shift'
8
+ spec.version = PhaseShift::VERSION
9
+ spec.authors = ['Jurgens du Toit']
10
+ spec.email = ['jrgns@jadeit.co.za']
11
+ spec.description = spec.summary
12
+ spec.summary = 'A simple pipeline processing implementation'
13
+ spec.homepage = 'https://github.com/hackerplanet/phase_shift'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($RS)
17
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rubocop'
24
+ spec.add_development_dependency 'rspec'
25
+ end
@@ -0,0 +1,13 @@
1
+ # Simple counter class
2
+ class Messager
3
+ def initialize(app, message = 'Message')
4
+ @app = app
5
+ @message = message
6
+ end
7
+
8
+ def call
9
+ result = @app.call
10
+ result.push @message
11
+ result
12
+ end
13
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+ require 'phase_shift/builder'
3
+
4
+ describe 'PhaseShift::Builder' do
5
+ subject { PhaseShift::Builder }
6
+
7
+ let(:stage) do
8
+ double.as_null_object
9
+ end
10
+
11
+ let(:stage_two) do
12
+ double.as_null_object
13
+ end
14
+
15
+ context '.parse_file' do
16
+ it 'evaluates the passed file to a callable object' do
17
+ expect(subject.parse_file('spec/test.rb')).to respond_to :call
18
+ end
19
+ end
20
+
21
+ context '#initialize' do
22
+ it 'evaluates the passed block in the context of the object' do
23
+ expect { subject.new { fail "Stage Count: #{stages.count}" } }\
24
+ .to raise_error('Stage Count: 0')
25
+ end
26
+ end
27
+
28
+ context '#use' do
29
+ it 'adds the passed stage to the pipeline' do
30
+ builder = subject.new
31
+ builder.use stage
32
+
33
+ builder.run
34
+
35
+ expect(stage).to have_received(:new).with(duck_type(:call))
36
+ end
37
+
38
+ it 'passes the specified arguments to the stage' do
39
+ builder = subject.new
40
+ string = 'This'
41
+ builder.use stage, string
42
+
43
+ builder.run
44
+
45
+ expect(stage).to have_received(:new).with(duck_type(:call), be(string))
46
+ end
47
+
48
+ it 'adds all the specified stages to the pipeline' do
49
+ builder = subject.new
50
+ builder.use stage
51
+ builder.use stage_two
52
+
53
+ expect(builder.stages.count).to eq(2)
54
+ end
55
+ end
56
+
57
+ context '#pipeline' do
58
+ it 'has a default stage that returns an empty array' do
59
+ expect(subject.new.pipeline.call).to eq([])
60
+ end
61
+
62
+ it 'allows the first stage of the pipeline to be customized' do
63
+ expect(subject.new(proc { [1] }).pipeline.call).to eq([1])
64
+ end
65
+
66
+ it 'build the pipeline out of stages' do
67
+ builder = subject.new
68
+ builder.use stage
69
+ builder.use stage_two
70
+
71
+ builder.pipeline
72
+
73
+ expect(stage).to have_received(:new).with(duck_type(:call))
74
+ expect(stage_two).to have_received(:new).with(duck_type(:call))
75
+ end
76
+ end
77
+
78
+ context '#run' do
79
+ it 'calls the stages in the pipeline' do
80
+ builder = subject.new
81
+ builder.use stage
82
+ builder.run
83
+
84
+ expect(stage).to have_received(:call)
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require 'phase_shift/runner'
3
+
4
+ describe 'PhaseShift::Runner' do
5
+ subject { PhaseShift::Runner.new(pipeline: 'spec/test') }
6
+
7
+ context '#initialize' do
8
+ it 'requires a specified pipeline' do
9
+ expect {PhaseShift::Runner.new}.to raise_error
10
+ end
11
+
12
+ it 'sets the passed options' do
13
+ runner = PhaseShift::Runner.new(pipeline: 'spec/test', test: 'value')
14
+ expect(runner.options).to include(:test)
15
+ expect(runner.options[:test]).to eq 'value'
16
+ end
17
+ end
18
+
19
+ context '#run' do
20
+ it 'calls the pipeline' do
21
+ result = subject.run
22
+ expect(result).to eq ['Phase 1', 'Phase 2', 'Phase 3', 'Phase 4']
23
+ end
24
+ end
25
+
26
+ context '#pipeline' do
27
+ it 'returns the pipeline' do
28
+ expect(subject.pipeline).to respond_to :call
29
+ expect(subject.pipeline.call).to \
30
+ eq ['Phase 1', 'Phase 2', 'Phase 3', 'Phase 4']
31
+ end
32
+ end
33
+
34
+ context '#options' do
35
+ it 'returns the options hash' do
36
+ expect(subject.options).to eq pipeline: 'spec/test'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.configure do |config|
2
+ config.alias_example_to :fit, focus: true
3
+ config.filter_run focus: true
4
+ config.run_all_when_everything_filtered = true
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'messager'
2
+
3
+ use Messager, 'Phase 1'
4
+ use Messager, 'Phase 2'
5
+ use Messager, 'Phase 3'
6
+ use Messager, 'Phase 4'
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phase_shift
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jurgens du Toit
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rubocop
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: ''
79
+ email:
80
+ - jrgns@jadeit.co.za
81
+ executables:
82
+ - phase_shift.rb
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - bin/phase_shift.rb
92
+ - lib/phase_shift.rb
93
+ - lib/phase_shift/builder.rb
94
+ - lib/phase_shift/runner.rb
95
+ - lib/phase_shift/version.rb
96
+ - phase_shift.gemspec
97
+ - spec/messager.rb
98
+ - spec/phase_shift/builder_spec.rb
99
+ - spec/phase_shift/runner_spec.rb
100
+ - spec/spec_helper.rb
101
+ - spec/test.rb
102
+ homepage: https://github.com/hackerplanet/phase_shift
103
+ licenses:
104
+ - MIT
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.23
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: A simple pipeline processing implementation
127
+ test_files:
128
+ - spec/messager.rb
129
+ - spec/phase_shift/builder_spec.rb
130
+ - spec/phase_shift/runner_spec.rb
131
+ - spec/spec_helper.rb
132
+ - spec/test.rb