Skip to content

Commit

Permalink
Fix RuboCop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jan 18, 2014
1 parent db82cdf commit 87c90f5
Show file tree
Hide file tree
Showing 30 changed files with 177 additions and 155 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)
Expand Down
2 changes: 1 addition & 1 deletion lib/naught.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "naught/version"
require 'naught/version'
require 'naught/null_class_builder'
require 'naught/null_class_builder/commands'

Expand Down
14 changes: 6 additions & 8 deletions lib/naught/conversions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Naught
module Conversions

def self.included(null_class)
unless class_variable_defined?(:@@included) && @@included
@@null_class = null_class
Expand All @@ -10,18 +9,18 @@ def self.included(null_class)
super
end

def Null(object=:nothing_passed)
def Null(object = :nothing_passed)
case object
when NullObjectTag
object
when :nothing_passed, *@@null_equivs
@@null_class.get(:caller => caller(1))
else
raise ArgumentError, "#{object.inspect} is not null!"
fail ArgumentError, "#{object.inspect} is not null!"
end
end

def Maybe(object=nil)
def Maybe(object = nil)
object = yield if block_given?
case object
when NullObjectTag
Expand All @@ -33,17 +32,17 @@ def Maybe(object=nil)
end
end

def Just(object=nil)
def Just(object = nil)
object = yield if block_given?
case object
when NullObjectTag, *@@null_equivs
raise ArgumentError, "Null value: #{object.inspect}"
fail ArgumentError, "Null value: #{object.inspect}"
else
object
end
end

def Actual(object=nil)
def Actual(object = nil)
object = yield if block_given?
case object
when NullObjectTag
Expand All @@ -52,6 +51,5 @@ def Actual(object=nil)
object
end
end

end
end
18 changes: 9 additions & 9 deletions lib/naught/null_class_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ module Commands
def initialize
@interface_defined = false
@base_class = Naught::BasicObject
@inspect_proc = lambda { "<null>" }
@inspect_proc = lambda { '<null>' }
@stub_strategy = :stub_method_returning_nil
define_basic_methods
end

def interface_defined?
@interface_defined
!!@interface_defined
end

def customize(&customization_block)
Expand Down Expand Up @@ -74,14 +74,14 @@ def method_missing(method_name, *args, &block)
end

if RUBY_VERSION >= '1.9'
def respond_to_missing?(method_name, include_private=false)
def respond_to_missing?(method_name, include_private = false)
command_name = command_name_for_method(method_name)
Commands.const_defined?(command_name) || super
rescue NameError
super
end
else
def respond_to?(method_name, include_private=false)
def respond_to?(method_name, include_private = false)
command_name = command_name_for_method(method_name)
Commands.const_defined?(command_name) || super
rescue NameError
Expand Down Expand Up @@ -111,7 +111,7 @@ def respond_to?(*)
@interface_defined = true
end

def defer(options={}, &deferred_operation)
def defer(options = {}, &deferred_operation)
list = options[:class] ? class_operations : operations
if options[:prepend]
list.unshift(deferred_operation)
Expand Down Expand Up @@ -151,7 +151,7 @@ def define_basic_class_methods
defer(:class => true) do |subject|
subject.module_eval do
class << self
alias get new
alias_method :get, :new
end
klass = self
define_method(:class) { klass }
Expand All @@ -169,18 +169,18 @@ def operations

def stub_method_returning_nil(subject, name)
subject.module_eval do
define_method(name) {|*| nil }
define_method(name) { |*| nil }
end
end

def stub_method_returning_self(subject, name)
subject.module_eval do
define_method(name) {|*| self }
define_method(name) { |*| self }
end
end

def command_name_for_method(method_name)
method_name.to_s.gsub(/(?:^|_)([a-z])/) { $1.upcase }
method_name.to_s.gsub(/(?:^|_)([a-z])/) { Regexp.last_match[1].upcase }
end
end
end
6 changes: 3 additions & 3 deletions lib/naught/null_class_builder/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def initialize(builder)
end

def call
raise NotImplementedError,
"Method #call should be overriden in child classes"
fail NotImplementedError,
'Method #call should be overriden in child classes'
end

def defer(options={}, &block)
def defer(options = {}, &block)
@builder.defer(options, &block)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ class DefineImplicitConversions < ::Naught::NullClassBuilder::Command
def call
defer do |subject|
subject.module_eval do
def to_ary; []; end
def to_str; ''; end
def to_ary
[]
end

def to_str
''
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/naught/null_class_builder/commands/impersonate.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Naught::NullClassBuilder::Commands
class Impersonate < Naught::NullClassBuilder::Commands::Mimic
def initialize(builder, class_to_impersonate, options={})
def initialize(builder, class_to_impersonate, options = {})
super

builder.base_class = class_to_impersonate
Expand Down
2 changes: 1 addition & 1 deletion lib/naught/null_class_builder/commands/mimic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Naught::NullClassBuilder::Commands
class Mimic < Naught::NullClassBuilder::Command
attr_reader :class_to_mimic, :include_super

def initialize(builder, class_to_mimic, options={})
def initialize(builder, class_to_mimic, options = {})
super(builder)

@class_to_mimic = class_to_mimic
Expand Down
8 changes: 3 additions & 5 deletions lib/naught/null_class_builder/commands/pebble.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module Naught
class NullClassBuilder
module Commands
class Pebble < ::Naught::NullClassBuilder::Command

def initialize(builder, output=$stdout)
def initialize(builder, output = $stdout)
@builder = builder
@output = output
end
Expand All @@ -15,18 +14,17 @@ def call
subject.module_exec(@output) do |output|

define_method(:method_missing) do |method_name, *args, &block|
pretty_args = args.map(&:inspect).join(", ").gsub("\"", "'")
pretty_args = args.collect(&:inspect).join(', ').gsub("\"", "'")
output.puts "#{method_name}(#{pretty_args}) from #{parse_caller}"
self
end

private

def parse_caller
caller = Kernel.caller(2).first
method_name = caller.match(/\`([\w\s]+(\(\d+\s\w+\))?[\w\s]*)/)
method_name ? method_name[1] : caller
end
private :parse_caller
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/naught/null_class_builder/commands/traceable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def call
subject.module_eval do
attr_reader :__file__, :__line__

def initialize(options={})
def initialize(options = {})
backtrace = if RUBY_VERSION.to_f == 1.9 && RUBY_PLATFORM != 'java'
options.fetch(:caller) { Kernel.caller(4) }
else
Expand Down
2 changes: 1 addition & 1 deletion lib/naught/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Naught
VERSION = "0.0.3"
VERSION = '0.0.3'
end
22 changes: 11 additions & 11 deletions naught.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'naught/version'

Gem::Specification.new do |spec|
spec.name = "naught"
spec.name = 'naught'
spec.version = Naught::VERSION
spec.authors = ["Avdi Grimm"]
spec.email = ["avdi@avdi.org"]
spec.authors = ['Avdi Grimm']
spec.email = ['avdi@avdi.org']
spec.description = %q{Naught is a toolkit for building Null Objects}
spec.summary = %q{Naught is a toolkit for building Null Objects}
spec.homepage = "https://github.com/avdi/naught"
spec.license = "MIT"
spec.summary = spec.description
spec.homepage = 'https://github.com/avdi/naught'
spec.license = 'MIT'

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
spec.require_paths = ['lib']

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency 'bundler', '~> 1.3'
end
2 changes: 1 addition & 1 deletion spec/base_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
end

it 'respond to base class methods' do
expect(null.methods).to be_a_kind_of(Array)
expect(null.methods).to be_an Array
end

it 'respond to unknown methods' do
Expand Down
4 changes: 2 additions & 2 deletions spec/basic_null_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
end

it 'accepts any arguments for any messages' do
null.foobaz(1,2,3)
null.foobaz(1, 2, 3)
end

it 'reports that it responds to any message' do
Expand All @@ -21,7 +21,7 @@
end

it 'can be inspected' do
expect(null.inspect).to eq("<null>")
expect(null.inspect).to eq('<null>')
end

it 'knows its own class' do
Expand Down
6 changes: 3 additions & 3 deletions spec/blackhole_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

describe 'black hole null object' do
subject(:null) { null_class.new }
let(:null_class) {
let(:null_class) do
Naught.build do |b|
b.black_hole
end
}
end

it 'returns self from arbitray method calls' do
expect(null.info).to be(null)
expect(null.foobaz).to be(null)
expect(null << "bar").to be(null)
expect(null << 'bar').to be(null)
end
end
15 changes: 7 additions & 8 deletions spec/explicit_conversions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
require 'spec_helper.rb'

describe 'explicitly convertable null object' do
let(:null_class) {
let(:null_class) do
Naught.build do |b|
b.define_explicit_conversions
end
}
end
subject(:null) { null_class.new }

it "defines common explicit conversions to return zero values" do
expect(null.to_s).to eq("")
it 'defines common explicit conversions to return zero values' do
expect(null.to_s).to eq('')
expect(null.to_a).to eq([])
expect(null.to_i).to eq(0)
expect(null.to_f).to eq(0.0)
if RUBY_VERSION >= '1.9'
expect(null.to_c).to eq(Complex(0))
expect(null.to_r).to eq(Rational(0))
end
if RUBY_VERSION >= '2.0'
expect(null.to_h).to eq({})
elsif RUBY_VERSION >= '1.9'
expect(null.to_c).to eq(Complex(0))
expect(null.to_r).to eq(Rational(0))
end
end
end
6 changes: 3 additions & 3 deletions spec/functions/actual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

specify 'given anything else, returns the input unchanged' do
expect(Actual(false)).to be(false)
str = "hello"
str = 'hello'
expect(Actual(str)).to be(str)
expect(Actual(nil)).to be_nil
end

it 'also works with blocks' do
expect(Actual{ConvertableNull.new}).to be_nil
expect(Actual{"foo"}).to eq("foo")
expect(Actual { ConvertableNull.new }).to be_nil
expect(Actual { 'foo' }).to eq('foo')
end
end
12 changes: 6 additions & 6 deletions spec/functions/just_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

specify 'passes non-nullish values through' do
expect(Just(false)).to be(false)
str = "hello"
str = 'hello'
expect(Just(str)).to be(str)
end

specify 'rejects nullish values' do
expect{Just(nil)}.to raise_error(ArgumentError)
expect{Just("")}.to raise_error(ArgumentError)
expect{Just(ConvertableNull.get)}.to raise_error(ArgumentError)
expect { Just(nil) }.to raise_error(ArgumentError)
expect { Just('') }.to raise_error(ArgumentError)
expect { Just(ConvertableNull.get) }.to raise_error(ArgumentError)
end

it 'also works with blocks' do
expect{Just{nil}.class}.to raise_error(ArgumentError)
expect(Just{"foo"}).to eq("foo")
expect { Just { nil }.class }.to raise_error(ArgumentError)
expect(Just { 'foo' }).to eq('foo')
end
end
Loading

0 comments on commit 87c90f5

Please sign in to comment.