-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Grape::ParameterTypes, but doesn't use it yet.
- Loading branch information
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Grape | ||
module ParameterTypes | ||
# Classes which we expect to be able to coerce through Virtus, | ||
# or through any coercion logic built in to Grape. | ||
KNOWN_TYPES = [ | ||
# Numerical | ||
Integer, | ||
Float, | ||
BigDecimal, | ||
Numeric, | ||
|
||
# Data structures | ||
Hash, | ||
Array, | ||
Set, | ||
|
||
# Date/time | ||
Date, | ||
DateTime, | ||
Time, | ||
|
||
# Misc | ||
Virtus::Attribute::Boolean, | ||
String, | ||
Symbol, | ||
Rack::Multipart::UploadedFile | ||
] | ||
|
||
# @param type [Class] type to check | ||
# @returns [Boolean] whether or not the type is known | ||
# by Grape as an acceptable parameter type | ||
def self.known_type?(type) | ||
KNOWN_TYPES.include?(type) | ||
end | ||
|
||
# @param type [Class] type to check | ||
# @returns [Boolean] whether or not the type can be used | ||
# as a custom type | ||
def self.custom_type?(type) | ||
!known_type?(type) && type.respond_to?(:parse) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::ParameterTypes do | ||
class FooType | ||
def self.parse | ||
end | ||
end | ||
|
||
describe '::known_type?' do | ||
[ | ||
Integer, Float, Numeric, BigDecimal, | ||
Virtus::Attribute::Boolean, String, Symbol, | ||
Hash, Array, Set, | ||
Date, DateTime, Time | ||
].each do |type| | ||
it "recognizes #{type} as a known type" do | ||
expect(described_class.known_type?(type)).to be_truthy | ||
end | ||
end | ||
|
||
it 'identifies unknown types' do | ||
expect(described_class.known_type?(Object)).to be_falsy | ||
expect(described_class.known_type?(FooType)).to be_falsy | ||
end | ||
end | ||
|
||
describe '::custom_type?' do | ||
it 'returns false if the type does not respond to :parse' do | ||
expect(described_class.custom_type?(Object)).to be_falsy | ||
end | ||
|
||
it 'returns true if the type responds to :parse' do | ||
expect(described_class.custom_type?(FooType)).to be_truthy | ||
end | ||
end | ||
end |