Module: LuckyParam
- Defined in:
- lib/lucky_param.rb,
lib/lucky_param/checker.rb,
lib/lucky_param/version.rb
Defined Under Namespace
Classes: ParamFormatError, ParamMissError, UnknownCheckerError
Constant Summary
collapse
- CHECKER =
{
NONE: [
->(_obj) { true }
],
String: [
->(obj) { obj.is_a?(String) },
"must be valid String"
],
Boolean: [
->(obj) { %w[true false 1 0].include?(obj.to_s) },
"must be one of [true false 1 0]"
],
Integer: [
->(obj) { obj.to_s =~ /\A(0|[1-9]\d*)\Z$/ },
"must be valid Integer"
],
Float: [
->(obj) { obj.to_s =~ /\A^[-+]?[0-9]+([,.][0-9]+)?\Z$/ },
"must be valid Float"
],
Number: [
->(obj) { Float(obj) rescue false }
],
Email: [
->(obj) { obj =~ /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ },
"must be valid Email"
],
Timestamp: [
->(obj) { obj =~ /^(\+|\-)?\d+$/ },
"must be valid Timestamp"
],
ArrayJSON: [
lambda { |obj|
begin
JSON.parse(obj).is_a?(Array)
rescue StandardError
false
end
},
"must be valid Array JSON"
],
HashJSON: [
lambda { |obj|
begin
JSON.parse(obj).is_a?(Hash)
rescue StandardError
false
end
},
"must be valid Hash JSON"
]
}.freeze
- VERSION =
"0.1.2".freeze
Instance Method Summary
collapse
Instance Method Details
#optional(column, checker_type) ⇒ Object
19
20
21
22
23
|
# File 'lib/lucky_param.rb', line 19
def optional(column, checker_type)
return if params[column].blank?
message = checker_message(column, checker_type)
raise ParamFormatError.new("Wrong Params Format: #{message}") if message
end
|
#required(column, checker_type) ⇒ Object
13
14
15
16
17
|
# File 'lib/lucky_param.rb', line 13
def required(column, checker_type)
raise ParamMissError.new("Missing Params: #{column}") if params[column].blank?
message = checker_message(column, checker_type)
raise ParamFormatError.new("Wrong Params Format: #{message}") if message
end
|