-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It can be used when lookup fails. ## Usage ```ruby scope_class = Scope.for(object.class) or raise Magic::Lookup::Error.for(object, Scope) ```
- Loading branch information
1 parent
275cfb3
commit 4df261a
Showing
4 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module Magic | ||
module Lookup | ||
class Error < NameError | ||
def self.for object, lookup_class | ||
default_name = lookup_class.name_for object.class | ||
|
||
new "no #{lookup_class} found for #{object.class}, default name is #{default_name}", | ||
default_name, receiver: object | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Magic | ||
module Lookup | ||
class Error < NameError | ||
def self.for: (any) -> Error | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
class Scope | ||
extend Magic::Lookup | ||
|
||
def self.name_for(object_class) = "#{object_class}Scope" | ||
end | ||
|
||
module Magic | ||
module Lookup | ||
RSpec.describe Error do | ||
subject { described_class.new } | ||
|
||
describe '.for', :method do | ||
its([[], Scope]) do | ||
is_expected.to be_instance_of described_class | ||
end | ||
|
||
its([[], Scope]) do | ||
is_expected.to be_a StandardError | ||
end | ||
|
||
its([[], Scope]) do | ||
is_expected.to have_attributes to_s: /no Scope found/, | ||
name: 'ArrayScope', receiver: [] | ||
end | ||
end | ||
end | ||
end | ||
end |