Skip to content
Permalink
Browse files

Don't build constructors when class implementing type interface given

There is no point in doing this. Moreover, Dry::Struct::Constructor doesn't implement all type methods (for some reasons, I guess) and it led to regressions in hanami-model.
  • Loading branch information
flash-gordon committed Nov 10, 2019
1 parent f907aed commit cb01f0ab0c6b3d565d45a3c6c1ad1eca71480deb
Showing with 16 additions and 1 deletion.
  1. +2 −0 Gemfile
  2. +5 −1 lib/dry/types/builder_methods.rb
  3. +9 −0 spec/dry/types/module_spec.rb
@@ -12,6 +12,8 @@ group :test do
platform :mri do
gem 'simplecov', require: false
end

gem 'dry-struct'
end

group :tools do
@@ -83,7 +83,11 @@ def Constant(object)
# @return [Dry::Types::Type]
def Constructor(klass, cons = nil, &block)
if klass.is_a?(Type)
klass.constructor(cons || block || klass.method(:new))
if cons || block
klass.constructor(cons || block)
else
klass
end
else
Nominal(klass).constructor(cons || block || klass.method(:new))
end
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'dry-struct'
require 'spec_helper'

RSpec.describe Dry::Types::Module do
@@ -93,6 +94,14 @@
mod.Constructor(String, &to_s)
)
end

context 'with struct' do
let(:user_struct) { Dry.Struct(name: 'string') }

it 'returns struct back if no constructor block provided' do
expect(mod.Constructor(user_struct)).to be(user_struct)
end
end
end

describe '.Nominal' do

0 comments on commit cb01f0a

Please sign in to comment.
You can’t perform that action at this time.