Skip to content

Commit

Permalink
Improve handling of Range enum type
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Nov 17, 2018
1 parent efdfc60 commit 33027fc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/experimental/tmdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TMDB < TLAW::API

endpoint :latest
endpoint :now_playing do
param :page, :to_i, default: 1 # TODO: validate: 1..1000
param :page, enum: 1..1000, default: 1
end

endpoint :upcoming
Expand Down
25 changes: 24 additions & 1 deletion lib/tlaw/params/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Type
def self.parse(type: nil, enum: nil, **)
case type
when nil
enum ? EnumType.new(enum) : Type.new(nil)
parse_enum(enum)
when Class
ClassType.new(type)
when Symbol
Expand All @@ -19,6 +19,17 @@ def self.parse(type: nil, enum: nil, **)
end
end

def self.parse_enum(enum)
case enum
when Range
RangeType.new(enum)
when nil
Type.new(nil)
else
EnumType.new(enum)
end
end

def initialize(type)
@type = type
end
Expand Down Expand Up @@ -101,5 +112,17 @@ def _convert(value)
type[value]
end
end

# @private
class RangeType < Type
def possible_values
type.inspect
end

def validation_error(value)
return 'is not an Integer' if type.begin.is_a?(Integer) && !value.is_a?(Integer)
"is not in #{possible_values}" unless type.cover?(value)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/tlaw/param_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module Params
|@param kv2 [#to_i]
|@param kv1 [Time]
|@param kv3
| Possible values: 1, 2, 3, 4, 5 (default = 3)
| Possible values: 1..5 (default = 3)
}.unindent }
end
end
Expand Down
52 changes: 51 additions & 1 deletion spec/tlaw/param_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Params
end
end

context 'enum' do
context 'hash enum' do
let(:param) { described_class.new(:p, enum: {true => 'yes', false => 'no'}) }

context 'when included' do
Expand All @@ -60,6 +60,50 @@ module Params
its_block { is_expected.to raise_error Nonconvertible }
end
end

context 'integer enum' do
let(:param) { described_class.new(:p, enum: 1..100) }

context 'when included' do
let(:value) { 42 }

it { is_expected.to eq 42 }
end

context 'when not an integer' do
let(:value) { 42.5 }

its_block { is_expected.to raise_error Nonconvertible }
end

context 'when not in range' do
let(:value) { 0 }

its_block { is_expected.to raise_error Nonconvertible }
end
end

context 'float enum' do
let(:param) { described_class.new(:p, enum: 0.0..1.0) }

context 'when included' do
let(:value) { 0.42 }

it { is_expected.to eq 0.42 }
end

context 'when is an integer in range' do
let(:value) { 0 }

it { is_expected.to eq 0 }
end

context 'when not in range' do
let(:value) { 2.0 }

its_block { is_expected.to raise_error Nonconvertible }
end
end
end

describe '#format' do
Expand Down Expand Up @@ -212,6 +256,12 @@ module Params
it { is_expected.to include('Possible values: true, false') }
end

context 'range' do
let(:param) { described_class.new(:p, enum: 1..100) }

it { is_expected.to include('Possible values: 1..100') }
end

context 'other enumerable' do
let(:param) { described_class.new(:p, enum: %w[foo bar]) }

Expand Down

0 comments on commit 33027fc

Please sign in to comment.