-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make PageType an ObjectType, fix its export
- Loading branch information
Showing
11 changed files
with
81 additions
and
61 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
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
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 |
---|---|---|
@@ -1,21 +1,27 @@ | ||
module Taro::Types::Shared::DerivedTypes | ||
def derived_types | ||
@derived_types ||= {} | ||
end | ||
|
||
def define_derived_type(name) | ||
type = self | ||
# Adds `name` as a method to all type classes and adds | ||
# `name`_of as a supported key to the Coercion module. | ||
# When `name` is called on a type class T, it returns a new subclass | ||
# S inheriting from `type` and passes T to S::derive_from. | ||
def define_derived_type(name, type) | ||
root = Taro::Types::BaseType | ||
raise ArgumentError, "#{name} is already in use" if root.respond_to?(name) | ||
|
||
key = :"#{name}_of" | ||
keys = Taro::Types::Coercion.keys | ||
raise ArgumentError, "#{key} is already in use" if keys.include?(key) | ||
ckey = :"#{name}#{Taro::Types::Coercion.derived_suffix}" | ||
ckeys = Taro::Types::Coercion.keys | ||
raise ArgumentError, "#{ckey} is already in use" if ckeys.include?(ckey) | ||
|
||
root.define_singleton_method(name) do | ||
derived_types[type] ||= Class.new(type).tap { |t| t.derive_from(self) } | ||
derived_types[type] ||= begin | ||
type_class = Taro::Types::Coercion.call(type:) | ||
Class.new(type_class).tap { |t| t.derive_from(self) } | ||
end | ||
end | ||
|
||
keys << key | ||
ckeys << ckey | ||
end | ||
|
||
def derived_types | ||
@derived_types ||= {} | ||
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
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
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
describe Taro::Types::Shared::DerivedTypes do | ||
it 'raises an error if the deriving method is already in use' do | ||
expect do | ||
Taro::Types::BaseType.define_derived_type(:inspect) | ||
Taro::Types::BaseType.define_derived_type(:inspect, 'String') | ||
end.to raise_error(ArgumentError, 'inspect is already in use') | ||
end | ||
|
||
it 'raises an error if the coercion key is already in use' do | ||
allow(Taro::Types::Coercion).to receive(:keys).and_return([:blob_of]) | ||
expect do | ||
Taro::Types::BaseType.define_derived_type(:blob) | ||
Taro::Types::BaseType.define_derived_type(:blob, 'String') | ||
end.to raise_error(ArgumentError, 'blob_of is already in use') | ||
end | ||
end |