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 Introspect::Graph#find! #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions lib/trailblazer/activity/introspect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ module Introspect

# @private This API is still under construction.
class Graph
class NodeNotFound < RuntimeError
def initialize(id = nil)
return super("Cannot find node with id=#{id.inspect}") if id
super("Cannot find node for given block")
end
end

def initialize(activity)
@activity = activity
@schema = activity.to_h or raise
Expand All @@ -22,6 +29,10 @@ def find(id = nil, &block)
find_with_block(&block)
end

def find!(id = nil, &block)
find(id, &block) or raise NodeNotFound.new(id)
end

def collect(strategy: :circuit)
@map.keys.each_with_index.collect { |task, i| yield find_with_block { |node| node.task == task }, i }
end
Expand Down
22 changes: 22 additions & 0 deletions test/introspect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ class IntrospectionTest < Minitest::Spec
end
end

describe "#find!" do
let(:node) { graph.find!(:B) }
it { expect(node.class).must_equal Activity::Introspect::Graph::Node }
it { expect(node[:id]).must_equal :B }

it "raises exception when {:id} is invalid" do
exception = assert_raises Activity::Introspect::Graph::NodeNotFound do
graph.find!(:Z)
end

expect(exception.message).must_equal "Cannot find node with id=:Z"
end

it "raises exception when node is not selected via block" do
exception = assert_raises Activity::Introspect::Graph::NodeNotFound do
graph.find!{ |node| nil }
end

expect(exception.message).must_equal "Cannot find node for given block"
end
end

describe "#collect" do
it "provides 1-arg {node}" do
nodes = graph.collect { |node| node }
Expand Down