Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Primitives shouldn't have pointless #go methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Stokes committed Jan 15, 2011
1 parent 697c71a commit 93bc990
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/deferrable_gratification/combinators.rb
Expand Up @@ -23,7 +23,7 @@ def self.included(base)

module ClassMethods
def chain(*actions)
actions.inject(DG.const(nil).tap(&:go), &:>>)
actions.inject(DG.const(nil), &:>>)
end
end
end
Expand Down
21 changes: 18 additions & 3 deletions lib/deferrable_gratification/primitives.rb
Expand Up @@ -4,12 +4,27 @@

module DeferrableGratification
module Primitives
# Return a Deferrable which immediately succeeds with a constant value.
def const(value)
Constant.new(value)
blank.tap {|d| d.succeed(value) }
end

def failure(*args)
Failure.new(*args)
# Return a Deferrable which immediately fails with an exception.
def failure(class_or_message, message_or_nil = nil)
blank.tap do |d|
d.fail(
case class_or_message
when Class
class_or_message.new(message_or_nil)
else
RuntimeError.new(class_or_message.to_s)
end)
end
end

# Return a completely uninteresting Deferrable.
def blank
DeferrableGratification::DefaultDeferrable.new
end
end
end
11 changes: 0 additions & 11 deletions lib/deferrable_gratification/primitives/const.rb

This file was deleted.

20 changes: 0 additions & 20 deletions lib/deferrable_gratification/primitives/failure.rb

This file was deleted.

4 changes: 2 additions & 2 deletions spec/deferrable_gratification/combinators/bind_spec.rb
Expand Up @@ -171,8 +171,8 @@
# into inner scopes by creating closures).

class << self
def good_op(x); DG.const(x).tap(&:go); end
def bad_op(*_); DG.failure(RuntimeError, 'Oops').tap(&:go); end
def good_op(x); DG.const(x); end
def bad_op(*_); DG.failure(RuntimeError, 'Oops'); end
def transform(x); x.succ; end
def boom(*_); raise 'Boom!'; end
end
Expand Down
20 changes: 4 additions & 16 deletions spec/deferrable_gratification/primitives_spec.rb
Expand Up @@ -5,10 +5,7 @@
describe 'DG.const("Hello")' do
subject { DG.const("Hello") }

describe 'after #go' do
before { subject.go }
it { should succeed_with('Hello') }
end
it { should succeed_with('Hello') }
end
end

Expand All @@ -17,28 +14,19 @@
describe 'DG.failure("does not compute")' do
subject { DG.failure("does not compute") }

describe 'after #go' do
before { subject.go }
it { should fail_with(RuntimeError, 'does not compute') }
end
it { should fail_with(RuntimeError, 'does not compute') }
end

describe 'DG.failure(ArgumentError)' do
subject { DG.failure(ArgumentError) }

describe 'after #go' do
before { subject.go }
it { should fail_with(ArgumentError) }
end
it { should fail_with(ArgumentError) }
end

describe 'DG.failure(ArgumentError, "unacceptable command")' do
subject { DG.failure(ArgumentError, "unacceptable command") }

describe 'after #go' do
before { subject.go }
it { should fail_with(ArgumentError, 'unacceptable command') }
end
it { should fail_with(ArgumentError, 'unacceptable command') }
end
end
end
10 changes: 2 additions & 8 deletions spec/spec_helper.rb
Expand Up @@ -33,17 +33,11 @@ def query(*query_parts)
end

def stub_successful_query(*args)
stub(:query).with(*args).and_return do
DG.const(yield).
tap(&:go) # TODO
end
stub(:query).with(*args).and_return { DG.const(yield) }
end

def stub_failing_query(*args)
stub(:query).with(*args).and_return do
DG.failure(yield).
tap(&:go) # TODO
end
stub(:query).with(*args).and_return { DG.failure(yield) }
end
end
end
Expand Down

0 comments on commit 93bc990

Please sign in to comment.