Skip to content
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

Add to_enum to the list of defined explicit conversions #46

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--order random
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bundler_args: --without development
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- ruby-head
Expand Down
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
Features:

- New "pebble" mode (Guilherme Carvalho)

6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ gem 'rake'

group :development do
gem 'guard'
gem 'guard-rspec'
gem 'guard-bundler'
gem 'guard-rspec'
gem 'pry'
gem 'pry-rescue'
end

group :test do
gem "libnotify"
gem 'coveralls', require: false
gem 'libnotify'
gem 'rspec', '~> 2.14'
end
11 changes: 6 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ null.to_a # => []
null.to_h # => {}
null.to_c # => (0+0i)
null.to_r # => (0/1)
null.to_enum # => #<Enumerator: nil:each>
```

#### Ah, but what about implicit conversions such as `#to_str`? Like what if I want a null object that implicitly splats the same way as an empty array?
Expand Down Expand Up @@ -176,8 +177,8 @@ null_io = NullIO.new

null_io << "foo" # => nil
null_io.readline # => nil
null_io.foobar # =>
# ~> -:11:in `<main>': undefined method `foobar' for
null_io.foobar # =>
# ~> -:11:in `<main>': undefined method `foobar' for
# <null:IO>:NullIO (NoMethodError)
```

Expand Down Expand Up @@ -245,8 +246,8 @@ null = NullObject.instance

null.__id__ # => 17844080
NullObject.instance.__id__ # => 17844080
NullObject.new # =>
# ~> -:11:in `<main>': private method `new' called for
NullObject.new # =>
# ~> -:11:in `<main>': private method `new' called for
# NullObject:Class (NoMethodError)
```

Expand Down Expand Up @@ -280,7 +281,7 @@ NullObject = Naught.build do |config|
else
config.singleton
end
end
end
```

The only caveat is that when swapping between singleton and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def call
defer do |subject|
subject.module_eval do
extend Forwardable
def_delegators :nil, :to_a, :to_c, :to_f, :to_h, :to_i, :to_r, :to_s
def_delegators :nil, :to_a, :to_c, :to_enum, :to_f, :to_h, :to_i, :to_r, :to_s
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/basic_null_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
expect(null_class.get.class).to be(null_class)
end

end
end
2 changes: 1 addition & 1 deletion spec/blackhole_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
expect(null.foobaz).to be(null)
expect(null << "bar").to be(null)
end
end
end
20 changes: 0 additions & 20 deletions spec/conversions_spec.rb

This file was deleted.

17 changes: 17 additions & 0 deletions spec/explicit_conversions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper.rb'

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

NIL_CONVERSION_METHODS.each do |conversion_method|
it "##{conversion_method} responds like nil" do
# We need to call inspect to compare Enumerators
expect(null.__send__(conversion_method).inspect).to eq(nil.send(conversion_method).inspect)
end
end
end
2 changes: 1 addition & 1 deletion spec/functions/actual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
expect(Actual{ConvertableNull.new}).to be_nil
expect(Actual{"foo"}).to eq("foo")
end
end
end
2 changes: 1 addition & 1 deletion spec/functions/just_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
expect{Just{nil}.class}.to raise_error(ArgumentError)
expect(Just{"foo"}).to eq("foo")
end
end
end
2 changes: 1 addition & 1 deletion spec/functions/maybe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
expect(Maybe{nil}.class).to eq(ConvertableNull)
expect(Maybe{"foo"}).to eq("foo")
end
end
end
2 changes: 1 addition & 1 deletion spec/functions/null_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
expect(null.__file__).to eq(__FILE__)
end

end
end
2 changes: 1 addition & 1 deletion spec/implicit_conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
expect(null.to_str).to eq("")
end

end
end
4 changes: 2 additions & 2 deletions spec/naught/null_object_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class TestCommand

it 'translates method calls into command invocations including arguments' do
test_command = double
NullClassBuilder::Commands::TestCommand.should_receive(:new).
expect(NullClassBuilder::Commands::TestCommand).to receive(:new).
with(builder, "foo", 42).
and_return(test_command)
test_command.should_receive(:call).and_return("COMMAND RESULT")
expect(test_command).to receive(:call).and_return("COMMAND RESULT")
expect(builder.test_command("foo", 42)).to eq("COMMAND RESULT")
end

Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
GEM_ROOT = File.expand_path("../../", __FILE__)
NIL_CONVERSION_METHODS = nil.methods.select{|method| method.to_s =~ /^to_\w+$/}
$:.unshift File.join(GEM_ROOT, "lib")

if ENV["TRAVIS"]
Expand All @@ -10,4 +11,4 @@
end

require 'naught'
Dir[File.join(GEM_ROOT, "spec", "support", "**/*.rb")].each { |f| require f }
Dir[File.join(GEM_ROOT, "spec", "support", "**/*.rb")].each { |f| require f }
2 changes: 1 addition & 1 deletion spec/support/convertable_null.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ConvertableNull = Naught.build do |b|
b.null_equivalents << ""
b.traceable
end
end