Skip to content

Cleaning up code to fit Ruby style guide. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

# RSpec test task
RSpec::Core::RakeTask.new

# Make sure the default is to run RSpec
task :default => "spec"
task default: 'spec'
4 changes: 2 additions & 2 deletions lib/middleware.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require "middleware/builder"
require "middleware/runner"
require 'middleware/builder'
require 'middleware/runner'
25 changes: 11 additions & 14 deletions lib/middleware/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Builder
# in which knows how to run them.
# @yield [] Evaluated in this instance which allows you to use methods
# like {#use} and such.
def initialize(opts=nil, &block)
def initialize(opts = nil, &block)
opts ||= {}
@runner_class = opts[:runner_class] || Runner

Expand All @@ -55,7 +55,7 @@ def initialize(opts=nil, &block)
# of being treated as a separate single middleware.
def flatten
lambda do |env|
self.call(env)
call(env)
end
end

Expand All @@ -65,11 +65,11 @@ def flatten
#
# @param [Class] middleware The middleware class
def use(middleware, *args, &block)
if middleware.kind_of?(Builder)
if middleware.is_a?(Builder)
# Merge in the other builder's stack into our own
self.stack.concat(middleware.stack)
stack.concat(middleware.stack)
else
self.stack << [middleware, args, block]
stack << [middleware, args, block]
end

self
Expand All @@ -79,7 +79,7 @@ def use(middleware, *args, &block)
# given middleware object.
def insert(index, middleware, *args, &block)
index = self.index(index) unless index.is_a?(Integer)
raise "no such middleware to insert before: #{index.inspect}" unless index
fail "no such middleware to insert before: #{index.inspect}" unless index
stack.insert(index, [middleware, args, block])
end

Expand All @@ -88,7 +88,7 @@ def insert(index, middleware, *args, &block)
# Inserts a middleware after the given index or middleware object.
def insert_after(index, middleware, *args, &block)
index = self.index(index) unless index.is_a?(Integer)
raise "no such middleware to insert after: #{index.inspect}" unless index
fail "no such middleware to insert after: #{index.inspect}" unless index
insert(index + 1, middleware, *args, &block)
end

Expand Down Expand Up @@ -125,14 +125,14 @@ def delete(index)
end

# Runs the builder stack with the given environment.
def call(env=nil)
def call(env = nil)
to_app.call(env)
end

def inspect
"[" + stack.reduce([]) { |carry, middleware|
'[' + stack.reduce([]) do |carry, middleware|
carry << "#{middleware[0].class.name}(#{middleware[1].join(', ')})"
}.join(', ') + "]"
end.join(', ') + ']'
end

protected
Expand Down Expand Up @@ -160,16 +160,13 @@ def stack
# you shouldn't use this method
#
# @return [Array]
def stack= stack
@stack = stack
end
attr_writer :stack

# Converts the builder stack to a runnable action sequence.
#
# @return [Object] A callable object
def to_app
@runner_class.new(stack.dup)
end

end
end
4 changes: 2 additions & 2 deletions lib/middleware/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Middleware
# in order, then reversing the order.
class Runner
# A middleware which does nothing
EMPTY_MIDDLEWARE = lambda { |env| env }
EMPTY_MIDDLEWARE = ->(env) { env }

# Build a new middleware runner with the given middleware
# stack.
Expand Down Expand Up @@ -60,7 +60,7 @@ def build_call_chain(stack)
next_middleware.call(klass.call(env, *args))
end
else
raise "Invalid middleware, doesn't respond to `call`: #{klass.inspect}"
fail "Invalid middleware, doesn't respond to `call`: #{klass.inspect}"
end
end
end
Expand Down
26 changes: 13 additions & 13 deletions middleware.gemspec
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# -*- encoding: utf-8 -*-

Gem::Specification.new do |gem|
gem.authors = ["Mitchell Hashimoto", "Arnaud Lemaire"]
gem.email = ["[email protected]", "[email protected]"]
gem.description = %q{Generalized implementation of the rack middleware abstraction for Ruby.}
gem.summary = %q{Generalized implementation of the rack middleware abstraction for Ruby (chain of responsibility design pattern).}
gem.homepage = "https://github.com/ibsciss/ruby-middleware"
gem.license = "MIT"
gem.authors = ['Mitchell Hashimoto', 'Arnaud Lemaire']
gem.email = ['[email protected]', '[email protected]']
gem.description = 'Generalized implementation of the rack middleware abstraction for Ruby.'
gem.summary = 'Generalized implementation of the rack middleware abstraction for Ruby (chain of responsibility design pattern).'
gem.homepage = 'https://github.com/ibsciss/ruby-middleware'
gem.license = 'MIT'

gem.add_development_dependency "rake", "~> 10.4.2"
gem.add_development_dependency "rspec-core", "~> 3.2"
gem.add_development_dependency "rspec-expectations", "~> 3.2"
gem.add_development_dependency "rspec-mocks", "~> 3.2"
gem.add_development_dependency 'rake', '~> 10.4.2'
gem.add_development_dependency 'rspec-core', '~> 3.2'
gem.add_development_dependency 'rspec-expectations', '~> 3.2'
gem.add_development_dependency 'rspec-mocks', '~> 3.2'
gem.add_development_dependency 'codeclimate-test-reporter', '~> 0.4.7'

gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.name = "ibsciss-middleware"
gem.require_paths = ["lib"]
gem.name = 'ibsciss-middleware'
gem.require_paths = ['lib']
gem.version = '0.3.2'
end
84 changes: 42 additions & 42 deletions spec/middleware/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require "middleware"
require 'middleware'

describe Middleware::Builder do
let(:data) { { :data => [] } }
let(:data) { { data: [] } }
let(:instance) { described_class.new }

# This returns a proc that can be used with the builder
# that simply appends data to an array in the env.
def appender_proc(data)
Proc.new { |obj| obj.tap { |env| env[:data] << data }}
proc { |obj| obj.tap { |env| env[:data] << data } }
end

context "initialized with a block" do
context "without explicit receiver" do
it "instance evals the block" do
context 'initialized with a block' do
context 'without explicit receiver' do
it 'instance evals the block' do
data = {}
proc = Proc.new { |env| env[:data] = true }
proc = proc { |env| env[:data] = true }

app = described_class.new do
use proc
Expand All @@ -26,10 +26,10 @@ def appender_proc(data)
end
end

context "with explicit receiver" do
it "yields self to the block" do
context 'with explicit receiver' do
it 'yields self to the block' do
data = {}
proc = Proc.new { |env| env[:data] = true }
proc = proc { |env| env[:data] = true }

app = described_class.new do |b|
b.use proc
Expand All @@ -42,21 +42,21 @@ def appender_proc(data)
end
end

context "basic `use`" do
it "should add items to the stack and make them callable" do
context 'basic `use`' do
it 'should add items to the stack and make them callable' do
data = {}
proc = Proc.new { |env| env[:data] = true }
proc = proc { |env| env[:data] = true }

instance.use proc
instance.call(data)

expect(data[:data]).to be_truthy
end

it "should be able to add multiple items" do
it 'should be able to add multiple items' do
data = {}
proc1 = Proc.new { |env| env.tap { |obj| obj[:one] = true }}
proc2 = Proc.new { |env| env.tap { |obj| obj[:two] = true }}
proc1 = proc { |env| env.tap { |obj| obj[:one] = true } }
proc2 = proc { |env| env.tap { |obj| obj[:two] = true } }

instance.use proc1
instance.use proc2
Expand All @@ -66,44 +66,44 @@ def appender_proc(data)
expect(data[:two]).to be_truthy
end

it "should be able to add another builder" do
it 'should be able to add another builder' do
data = {}
proc1 = Proc.new { |env| env[:one] = true }
proc1 = proc { |env| env[:one] = true }

# Build the first builder
one = described_class.new
one.use proc1

# Add it to this builder
two = described_class.new
two = described_class.new
two.use one

# Call the 2nd and verify results
two.call(data)
expect(data[:one]).to be_truthy
end

it "should default the env to `nil` if not given" do
it 'should default the env to `nil` if not given' do
result = false
proc = Proc.new { |env| result = env.nil? }
proc = proc { |env| result = env.nil? }

instance.use proc
instance.call

expect(result).to be_truthy
end
end
end

context "inserting" do
it "can insert at an index" do
context 'inserting' do
it 'can insert at an index' do
instance.use appender_proc(1)
instance.insert(0, appender_proc(2))
instance.call(data)

expect(data[:data]).to eq [2, 1]
end

it "can insert next to a previous object" do
it 'can insert next to a previous object' do
proc2 = appender_proc(2)
instance.use appender_proc(1)
instance.use proc2
Expand All @@ -113,20 +113,20 @@ def appender_proc(data)
expect(data[:data]).to eq [1, 3, 2]
end

it "can insert before" do
it 'can insert before' do
instance.use appender_proc(1)
instance.insert_before 0, appender_proc(2)
instance.call(data)

expect(data[:data]).to eq [2, 1]
end

it "raises an exception if attempting to insert before an invalid object" do
expect { instance.insert "object", appender_proc(1) }.
to raise_error(RuntimeError)
it 'raises an exception if attempting to insert before an invalid object' do
expect { instance.insert 'object', appender_proc(1) }
.to raise_error(RuntimeError)
end

it "can insert after each" do
it 'can insert after each' do
instance.use appender_proc(1)
instance.use appender_proc(2)
instance.use appender_proc(3)
Expand All @@ -135,7 +135,7 @@ def appender_proc(data)
expect(data[:data]).to eq [1, 9, 2, 9, 3, 9]
end

it "can insert before each" do
it 'can insert before each' do
instance.use appender_proc(1)
instance.use appender_proc(2)
instance.use appender_proc(3)
Expand All @@ -144,14 +144,14 @@ def appender_proc(data)
expect(data[:data]).to eq [9, 1, 9, 2, 9, 3]
end

it "raises an exception if attempting to insert after an invalid object" do
expect { instance.insert_after "object", appender_proc(1) }.
to raise_error(RuntimeError)
it 'raises an exception if attempting to insert after an invalid object' do
expect { instance.insert_after 'object', appender_proc(1) }
.to raise_error(RuntimeError)
end
end

context "replace" do
it "can replace an object" do
context 'replace' do
it 'can replace an object' do
proc1 = appender_proc(1)
proc2 = appender_proc(2)

Expand All @@ -162,7 +162,7 @@ def appender_proc(data)
expect(data[:data]).to eq [2]
end

it "can replace by index" do
it 'can replace by index' do
proc1 = appender_proc(1)
proc2 = appender_proc(2)

Expand All @@ -174,8 +174,8 @@ def appender_proc(data)
end
end

context "deleting" do
it "can delete by object" do
context 'deleting' do
it 'can delete by object' do
proc1 = appender_proc(1)

instance.use proc1
Expand All @@ -186,7 +186,7 @@ def appender_proc(data)
expect(data[:data]).to eq [2]
end

it "can delete by index" do
it 'can delete by index' do
proc1 = appender_proc(1)

instance.use proc1
Expand All @@ -198,8 +198,8 @@ def appender_proc(data)
end
end

context "debugging" do
it "has an inspect method" do
context 'debugging' do
it 'has an inspect method' do
instance.use appender_proc(1)
instance.use appender_proc(1), 2
expect(instance.inspect).to eq '[Proc(), Proc(2)]'
Expand Down
Loading