Deprecate Types::Array#member

This commit is contained in:
Nikita Shilnikov 2017-08-26 22:44:12 +03:00
parent f5e920b1fd
commit ea270b4859
No known key found for this signature in database
GPG Key ID: E569D1D64C40E241
14 changed files with 48 additions and 20 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@
/pkg/ /pkg/
/spec/reports/ /spec/reports/
/tmp/ /tmp/
log/

View File

@ -1,3 +1,11 @@
# to-be-released
## Changed
* `Types::Array#member` was deprecated in favor of `Types::Array#of` (flash-gordon)
[Compare v0.11.1...master](https://github.com/dry-rb/dry-types/compare/v0.11.1...master)
# v0.11.1 2017-08-14 # v0.11.1 2017-08-14
## Changed ## Changed

View File

@ -84,7 +84,7 @@ module Dry
if result if result
type_id, member_id = result[1..2] type_id, member_id = result[1..2]
container[type_id].member(self[member_id]) container[type_id].of(self[member_id])
else else
container[name] container[name]
end end

View File

@ -1,21 +1,26 @@
require 'dry/types/array/member' require 'dry/types/array/member'
require 'dry/core/deprecations'
module Dry module Dry
module Types module Types
class Array < Definition class Array < Definition
extend Dry::Core::Deprecations[:'dry-types']
# @param [Type] type # @param [Type] type
# @return [Array::Member] # @return [Array::Member]
def member(type) def of(type)
member = member =
case type case type
when String, Class then Types[type] when String, Class then Types[type]
else type else type
end end
Array::Member.new(primitive, options.merge(member: member)) Array::Member.new(primitive, **options, member: member)
end end
alias_method :of, :member alias_method :member, :of
deprecate :member, :of
end end
end end
end end

View File

@ -45,7 +45,7 @@ module Dry
def visit_array(node) def visit_array(node)
member, meta = node member, meta = node
registry['array'].member(visit(member)).meta(meta) registry['array'].of(visit(member)).meta(meta)
end end
def visit_hash(node) def visit_hash(node)
@ -60,7 +60,7 @@ module Dry
def visit_json_array(node) def visit_json_array(node)
member, meta = node member, meta = node
registry['json.array'].member(visit(member)).meta(meta) registry['json.array'].of(visit(member)).meta(meta)
end end
def visit_form_hash(node) def visit_form_hash(node)
@ -70,7 +70,7 @@ module Dry
def visit_form_array(node) def visit_form_array(node)
member, meta = node member, meta = node
registry['form.array'].member(visit(member)).meta(meta) registry['form.array'].of(visit(member)).meta(meta)
end end
def visit_member(node) def visit_member(node)

0
log/.gitkeep Normal file
View File

View File

@ -1,7 +1,7 @@
require 'spec/dry/types' require 'spec/dry/types'
RSpec.describe Dry::Types::Array do RSpec.describe Dry::Types::Array do
describe '#member' do describe '#of' do
context 'primitive' do context 'primitive' do
shared_context 'array with a member type' do shared_context 'array with a member type' do
it 'returns an array with correct member values' do it 'returns an array with correct member values' do
@ -20,14 +20,14 @@ RSpec.describe Dry::Types::Array do
end end
context 'using method' do context 'using method' do
subject(:array) { Dry::Types['coercible.array'].member(Dry::Types['coercible.string']) } subject(:array) { Dry::Types['coercible.array'].of(Dry::Types['coercible.string']) }
include_context 'array with a member type' include_context 'array with a member type'
end end
context 'using a constrained type' do context 'using a constrained type' do
subject(:array) do subject(:array) do
Dry::Types['array'].member(Dry::Types['coercible.int'].constrained(gt: 2)) Dry::Types['array'].of(Dry::Types['coercible.int'].constrained(gt: 2))
end end
it 'passes values through member type' do it 'passes values through member type' do
@ -48,8 +48,16 @@ RSpec.describe Dry::Types::Array do
end end
end end
describe '#member' do
subject(:array) { Dry::Types['coercible.array'].member(Dry::Types['coercible.string']) }
it 'still works though deprecated' do
expect(array[Set[1, 2, 3]]).to eql(%w(1 2 3))
end
end
describe '#valid?' do describe '#valid?' do
subject(:array) { Dry::Types['strict.array'].member(Dry::Types['strict.string']) } subject(:array) { Dry::Types['strict.array'].of(Dry::Types['strict.string']) }
it 'detects invalid input of the completely wrong type' do it 'detects invalid input of the completely wrong type' do
expect(array.valid?(5)).to be(false) expect(array.valid?(5)).to be(false)

View File

@ -128,7 +128,7 @@ RSpec.describe Dry::Types::Compiler, '#call' do
end end
it 'builds an array' do it 'builds an array' do
ast = Dry::Types['array'].member( ast = Dry::Types['array'].of(
Dry::Types['hash'].symbolized( Dry::Types['hash'].symbolized(
email: Dry::Types['string'], email: Dry::Types['string'],
age: Dry::Types['form.int'], age: Dry::Types['form.int'],
@ -164,7 +164,7 @@ RSpec.describe Dry::Types::Compiler, '#call' do
end end
it 'builds a safe form array with member' do it 'builds a safe form array with member' do
ast = Dry::Types['form.array'].member(Dry::Types['coercible.int']).to_ast ast = Dry::Types['form.array'].of(Dry::Types['coercible.int']).to_ast
arr = compiler.(ast) arr = compiler.(ast)

View File

@ -111,7 +111,7 @@ RSpec.describe Dry::Types::Constrained do
subject(:type) do subject(:type) do
Dry::Types['strict.array'] Dry::Types['strict.array']
.constrained(size: 3) .constrained(size: 3)
.member(Dry::Types['coercible.string']) .of(Dry::Types['coercible.string'])
end end
it_behaves_like Dry::Types::Definition it_behaves_like Dry::Types::Definition

View File

@ -79,15 +79,15 @@ RSpec.describe Dry::Types::Sum do
it 'works with two complex types with constraints' do it 'works with two complex types with constraints' do
pair = Dry::Types['strict.array'] pair = Dry::Types['strict.array']
.member(Dry::Types['coercible.string']) .of(Dry::Types['coercible.string'])
.constrained(size: 2) .constrained(size: 2)
string_list = Dry::Types['strict.array'] string_list = Dry::Types['strict.array']
.member(Dry::Types['strict.string']) .of(Dry::Types['strict.string'])
.constrained(min_size: 1) .constrained(min_size: 1)
string_pairs = Dry::Types['strict.array'] string_pairs = Dry::Types['strict.array']
.member(pair) .of(pair)
.constrained(min_size: 1) .constrained(min_size: 1)
type = string_list | string_pairs type = string_list | string_pairs

View File

@ -181,7 +181,7 @@ RSpec.describe Dry::Types, '#to_ast' do
context 'Member' do context 'Member' do
subject(:type) do subject(:type) do
Dry::Types['array'].member(Dry::Types['string']) Dry::Types['array'].of(Dry::Types['string'])
end end
specify do specify do

View File

@ -231,7 +231,7 @@ RSpec.describe Dry::Types::Definition do
end end
describe 'form.array' do describe 'form.array' do
subject(:type) { Dry::Types['form.array'].member(Dry::Types['form.int']) } subject(:type) { Dry::Types['form.array'].of(Dry::Types['form.int']) }
it 'returns coerced array' do it 'returns coerced array' do
arr = %w(1 2 3) arr = %w(1 2 3)

View File

@ -64,7 +64,7 @@ RSpec.describe Dry::Types::Definition do
end end
describe 'json.array' do describe 'json.array' do
subject(:type) { Dry::Types['json.array'].member(Dry::Types['int']) } subject(:type) { Dry::Types['json.array'].of(Dry::Types['int']) }
it 'returns original value when it is not an array' do it 'returns original value when it is not an array' do
foo = 'foo' foo = 'foo'

View File

@ -14,6 +14,9 @@ end
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'pathname' require 'pathname'
SPEC_ROOT = Pathname(__FILE__).dirname
require 'dry-types' require 'dry-types'
begin begin
@ -36,6 +39,9 @@ require_relative '../lib/spec/dry/types'
Undefined = Dry::Core::Constants::Undefined Undefined = Dry::Core::Constants::Undefined
require 'dry/core/deprecations'
Dry::Core::Deprecations.set_logger!(SPEC_ROOT.join('../log/deprecations.log'))
RSpec.configure do |config| RSpec.configure do |config|
config.before(:example, :maybe) do config.before(:example, :maybe) do
Dry::Types.load_extensions(:maybe) Dry::Types.load_extensions(:maybe)