Skip to content

Commit 31e1f36

Browse files
authored
[URI] Allows dry types as ID (#76)
* Adds type support. * Adds dry type support to readme
1 parent 6a37292 commit 31e1f36

4 files changed

Lines changed: 68 additions & 2 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ InvoiceRegexURI = Studitemps::Utils::URI.build(from: InvoiceURI, id: /I-\d{3}/)
8484
InvoiceRegexURI.build('com.example:billing:invoice:pro_forma') # => Studitemps::Utils::URI::Base::InvalidURI
8585
InvoiceRegexURI.new(id: 'I-123') # => #<InvoiceRegexURI 'com.example:billing:invoice:I-123'>
8686
InvoiceRegexURI.new(id: 'pro_forma') # => Dry::Types::ConstraintError
87+
88+
# types are also supported
89+
itype = Dry.Types::Coercible::Integer.constrained(gteq: 1000)
90+
InvoiceTypeURI = Studitemps::Utils::URI.build(from: InvoiceURI, id: itype)
91+
92+
InvoiceTypeURI.build('com.example:billing:invoice:pro_forma') # => Sry::Types::CoercionError
93+
InvoiceTypeURI.new(id: '1042') # => #<InvoiceTypeURI 'com.example:billing:invoice:1024'>
94+
InvoiceTypeURI.new(id: 'pro_forma') # => Dry::Types::ConstraintError
95+
8796
```
8897

8998
### Extensions

lib/studitemps/utils/uri/builder.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def value_regex(value, klass, default: '[\w\-_]+')
8181
def enum_regex(value, klass, default: '[\w\-_]+')
8282
return default unless (values = klass.send(value))
8383
return values if values.is_a? Regexp
84+
return '.*' if defined?(Dry::Types::Type) && values.is_a?(Dry::Types::Type)
8485

8586
escaped_values = Array(values).map { |v| Regexp.escape(v) }
8687
"(#{escaped_values.join('|')})"

lib/studitemps/utils/uri/extensions/types.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def dynamic_type(value)
7575
when Array then enum_type(value)
7676
when String, Symbol then value_type(value)
7777
when Regexp then regexp_type(value)
78+
when Dry::Types::Type then value
7879
else
7980
default_type
8081
end

spec/studitemps/uri_spec.rb

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,66 @@ module Utils # rubocop:disable Metrics/ModuleLength
445445
}.to raise_error Dry::Types::CoercionError
446446
end
447447
end
448+
449+
describe 'Type' do
450+
let(:invoice_number_type) { Dry.Types::Coercible::Integer.constrained(gteq: 1000) }
451+
let(:invoice_klass) { URI.build(from: klass, resource: 'invoice', id: invoice_number_type) }
452+
453+
it 'validates input' do
454+
expect {
455+
invoice_klass.new(id: '1042')
456+
}.to_not raise_error
457+
458+
expect {
459+
invoice_klass.new(id: '23')
460+
}.to raise_error Dry::Types::ConstraintError
461+
end
462+
463+
it 'has uri type' do
464+
type = invoice_klass::Types::URI
465+
uri = invoice_klass.new(id: '1042')
466+
467+
expect(type[uri]).to eq uri
468+
expect(type['com.example:billing:invoice:1042']).to eq uri
469+
expect {
470+
type['com.example:billing:invoice:0023']
471+
}.to raise_error Dry::Types::CoercionError
472+
end
473+
474+
it 'hase string type' do
475+
type = invoice_klass::Types::String
476+
uri = invoice_klass.new(id: '1042')
477+
478+
expect(type[uri]).to eq uri.to_s
479+
expect(type['com.example:billing:invoice:1042']).to eq uri.to_s
480+
expect {
481+
type['com.example:billing:invoice:0023']
482+
}.to raise_error Dry::Types::CoercionError
483+
end
484+
485+
it 'has fallback regex' do
486+
regex = invoice_klass::REGEX
487+
488+
expect(regex).to match 'com.example:billing:invoice:abc'
489+
expect(regex).to match 'com.example:billing:invoice:123'
490+
expect(regex).to match 'com.example:billing:invoice:123123'
491+
expect(regex).to match 'com.example:billing:invoice:'
492+
expect(regex).to_not match 'com.example:billing:invoicX:abc'
493+
end
494+
495+
it 'can be build from string' do
496+
uri = invoice_klass.new(id: '1042')
497+
498+
expect(invoice_klass.build('com.example:billing:invoice:1042')).to eq uri
499+
500+
expect {
501+
invoice_klass.build('com.example:billing:invoice:23')
502+
}.to raise_error Dry::Types::ConstraintError
503+
end
504+
end
448505
end
449506
end
450507
end
451508
end
452509
end
453510
end
454-
455-

0 commit comments

Comments
 (0)