-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor of the coercion and validation logic (#62)
- Loading branch information
1 parent
8235f1b
commit 7ef3018
Showing
57 changed files
with
1,818 additions
and
238 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ README.html | |
.bundle | ||
Gemfile.lock | ||
*.gem | ||
.idea | ||
.idea | ||
tmp/ | ||
coverage/ |
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 |
---|---|---|
@@ -1 +1 @@ | ||
2.3.1 | ||
2.7.1 |
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
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
require 'rails_param/param' | ||
Dir[File.join(__dir__, 'rails_param/validator', '*.rb')].sort.each { |file| require file } | ||
Dir[File.join(__dir__, 'rails_param/coercion', '*.rb')].sort.reverse.each { |file| require file } | ||
Dir[File.join(__dir__, 'rails_param', '*.rb')].sort.each { |file| require file } | ||
|
||
ActiveSupport.on_load(:action_controller) do | ||
include RailsParam::Param | ||
include RailsParam | ||
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,41 @@ | ||
module RailsParam | ||
class Coercion | ||
attr_reader :coercion, :param | ||
|
||
PARAM_TYPE_MAPPING = { | ||
Integer => IntegerParam, | ||
Float => FloatParam, | ||
String => StringParam, | ||
Array => ArrayParam, | ||
Hash => HashParam, | ||
BigDecimal => BigDecimalParam, | ||
Date => TimeParam, | ||
DateTime => TimeParam, | ||
Time => TimeParam, | ||
TrueClass => BooleanParam, | ||
FalseClass => BooleanParam, | ||
boolean: BooleanParam | ||
}.freeze | ||
|
||
TIME_TYPES = [Date, DateTime, Time].freeze | ||
BOOLEAN_TYPES = [TrueClass, FalseClass, :boolean].freeze | ||
|
||
def initialize(param, type, options) | ||
@param = param | ||
@coercion = klass_for(type).new(param: param, options: options, type: type) | ||
end | ||
|
||
def klass_for(type) | ||
klass = PARAM_TYPE_MAPPING[type] | ||
return klass if klass | ||
|
||
raise TypeError | ||
end | ||
|
||
def coerce | ||
return nil if param.nil? | ||
|
||
coercion.coerce | ||
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,18 @@ | ||
module RailsParam | ||
class Coercion | ||
class ArrayParam < VirtualParam | ||
def coerce | ||
return param if param.is_a?(Array) | ||
|
||
Array(param.split(options[:delimiter] || ",")) | ||
end | ||
|
||
private | ||
|
||
def argument_validation | ||
raise ArgumentError unless type == Array | ||
raise ArgumentError unless param.respond_to?(:split) | ||
end | ||
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,17 @@ | ||
module RailsParam | ||
class Coercion | ||
class BigDecimalParam < VirtualParam | ||
DEFAULT_PRECISION = 14 | ||
|
||
def coerce | ||
stripped_param = if param.is_a?(String) | ||
param.delete('$,').strip.to_f | ||
else | ||
param | ||
end | ||
|
||
BigDecimal(stripped_param, options[:precision] || DEFAULT_PRECISION) | ||
end | ||
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,15 @@ | ||
module RailsParam | ||
class Coercion | ||
class BooleanParam < VirtualParam | ||
FALSEY = /^(false|f|no|n|0)$/i.freeze | ||
TRUTHY = /^(true|t|yes|y|1)$/i.freeze | ||
|
||
def coerce | ||
return false if FALSEY === param.to_s | ||
return true if TRUTHY === param.to_s | ||
|
||
raise ArgumentError | ||
end | ||
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,9 @@ | ||
module RailsParam | ||
class Coercion | ||
class FloatParam < VirtualParam | ||
def coerce | ||
Float(param) | ||
end | ||
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,18 @@ | ||
module RailsParam | ||
class Coercion | ||
class HashParam < VirtualParam | ||
def coerce | ||
return param if param.is_a?(ActionController::Parameters) | ||
raise ArgumentError unless param.respond_to?(:split) | ||
|
||
Hash[param.split(options[:delimiter] || ",").map { |c| c.split(options[:separator] || ":") }] | ||
end | ||
|
||
private | ||
|
||
def argument_validation | ||
raise ArgumentError unless type == Hash | ||
end | ||
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,9 @@ | ||
module RailsParam | ||
class Coercion | ||
class IntegerParam < VirtualParam | ||
def coerce | ||
Integer(param) | ||
end | ||
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,9 @@ | ||
module RailsParam | ||
class Coercion | ||
class StringParam < VirtualParam | ||
def coerce | ||
String(param) | ||
end | ||
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,17 @@ | ||
module RailsParam | ||
class Coercion | ||
class TimeParam < VirtualParam | ||
def coerce | ||
return type.strptime(param, options[:format]) if options[:format].present? | ||
|
||
type.parse(param) | ||
end | ||
|
||
private | ||
|
||
def argument_validation | ||
raise ArgumentError unless type.respond_to?(:parse) | ||
end | ||
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,24 @@ | ||
module RailsParam | ||
class Coercion | ||
class VirtualParam | ||
attr_reader :param, :options, :type | ||
|
||
def initialize(param:, options: nil, type: nil) | ||
@param = param | ||
@options = options | ||
@type = type | ||
argument_validation | ||
end | ||
|
||
def coerce | ||
nil | ||
end | ||
|
||
private | ||
|
||
def argument_validation | ||
nil | ||
end | ||
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,17 @@ | ||
module RailsParam | ||
class InvalidParameterError < StandardError | ||
attr_accessor :param, :options | ||
|
||
def initialize(message, param: nil, options: {}) | ||
self.param = param | ||
self.options = options | ||
super(message) | ||
end | ||
|
||
def message | ||
return options[:message] if options.is_a?(Hash) && options.key?(:message) | ||
|
||
super | ||
end | ||
end | ||
end |
Oops, something went wrong.