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

draft: use generics in Converter #76

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 12 additions & 3 deletions lib/sorbet-coerce/converter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: ignore
# typed: true
require 'polyfill'
require 'safe_type'
require 'set'
Expand All @@ -13,24 +13,33 @@ class ShapeError < SafeType::CoercionError; end
end

class TypeCoerce::Converter
extend T::Sig
extend T::Generic

Elem = type_member # this is the generic passed in to the converter

sig {params(type: Elem).void}
def initialize(type)
@type = type
end

sig {returns(Elem)}
def type; @type; end

def new
self
end

def to_s
"#{name}#[#{@type.to_s}]"
"#{name}#[#{self.type.to_s}]"
end

def from(args, raise_coercion_error: nil, coerce_empty_to_nil: false)
if raise_coercion_error.nil?
raise_coercion_error = TypeCoerce::Configuration.raise_coercion_error
end

T.let(_convert(args, @type, raise_coercion_error, coerce_empty_to_nil), @type)
T.let(_convert(args, self.type, raise_coercion_error, coerce_empty_to_nil), self.type)
end

private
Expand Down