Skip to content

Commit

Permalink
Merge 5c9821b into ff0d775
Browse files Browse the repository at this point in the history
  • Loading branch information
tfausak committed May 3, 2014
2 parents ff0d775 + 5c9821b commit 7f68e42
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/active_interaction/concerns/runnable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ def valid?(*)
super
end

# @yield [outcome]
#
# @yieldparam outcome [Runnable]
#
# @return [Runnable]
def when_invalid
yield(self) if block_given? && invalid?

self
end

# @yield [result]
#
# @yieldparam result [Object]
#
# @return [Runnable]
def when_valid
yield(result) if block_given? && valid?

self
end

private

# @param other [Class] The other interaction.
Expand Down
48 changes: 48 additions & 0 deletions spec/active_interaction/concerns/runnable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,54 @@
end
end

describe '#when_invalid' do
include_context 'with #execute defined'

let(:outcome) { klass.run }

it 'returns self' do
expect(outcome.when_invalid).to equal outcome
end

context 'valid' do
it 'does not yield' do
expect { |b| outcome.when_invalid(&b) }.to_not yield_control
end
end

context 'invalid' do
include_context 'with a validator'

it 'yields' do
expect { |b| outcome.when_invalid(&b) }.to yield_with_args outcome
end
end
end

describe '#when_valid' do
include_context 'with #execute defined'

let(:outcome) { klass.run }

it 'returns self' do
expect(outcome.when_valid).to equal outcome
end

context 'invalid' do
include_context 'with a validator'

it 'does not yield' do
expect { |b| outcome.when_valid(&b) }.to_not yield_control
end
end

context 'valid' do
it 'yields' do
expect { |b| outcome.when_valid(&b) }.to yield_with_args outcome.result
end
end
end

describe '.run' do
let(:outcome) { klass.run }

Expand Down

0 comments on commit 7f68e42

Please sign in to comment.