Module: ParamParam::Std::Actions
- Defined in:
- lib/param_param/std.rb
Overview
Actions definitions.
Instance Method Summary collapse
-
#blank?(value) ⇒ Boolean
Verifies if provided value is nil, empty string or string consisting only from spaces.
-
#blank_to_nil_or ⇒ Object
Converts blank value to nil or passes non blank value to next rule.
-
#bool ⇒ Object
Returns lambda { |fn, option| … }.
-
#decimal ⇒ Object
Returns lambda { |fn, option| … }.
-
#gt ⇒ Object
Returns lambda { |limit, option| … }.
-
#gte ⇒ Object
Returns lambda { |limit, option| … }.
-
#included_in ⇒ Object
Verifies inclusion of a value in a collection.
-
#integer ⇒ Object
Returns lambda { |fn, option| … }.
-
#lt ⇒ Object
Returns lambda { |limit, option| … }.
-
#lte ⇒ Object
Returns lambda { |limit, option| … }.
-
#max_size ⇒ Object
Returns lambda { |limit, option| … }.
-
#min_size ⇒ Object
Returns lambda { |limit, option| … }.
-
#not_blank ⇒ Object
Verifies if value is not blank.
-
#optional ⇒ Object
Describes an optional value.
-
#required ⇒ Object
Describes a required value.
-
#string ⇒ Object
Returns lambda { |fn, option| … }.
-
#stripped ⇒ Object
Returns lambda { |option| … }.
Instance Method Details
#blank?(value) ⇒ Boolean
Verifies if provided value is nil, empty string or string consisting only from spaces.
108 109 110 |
# File 'lib/param_param/std.rb', line 108 def blank?(value) value.nil? || (value.is_a?(String) && value.strip.empty?) end |
#blank_to_nil_or ⇒ Object
Converts blank value to nil or passes non blank value to next rule.
Returns
lambda { |fn, option| ... }.
If provided option
‘s value is blank it succeeds with nil
otherwise executes provided function for the option
.
91 92 93 94 95 |
# File 'lib/param_param/std.rb', line 91 def blank_to_nil_or lambda { |fn, option| blank?(option.value) ? Success.new(Optiomist.some(nil)) : fn.call(option) }.curry end |
#bool ⇒ Object
Returns
lambda { |fn, option| ... }.
Converts provided option
‘s value to boolean. If the conversion is not possible it fails, otherwise executes the provider function fn
for the converted boolean value.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/param_param/std.rb', line 212 def bool lambda { |fn, option| case option in Optiomist::Some if [true, *TRUE_VALUES].include?(option.value) fn.call(Optiomist.some(true)) elsif [false, *FALSE_VALUES].include?(option.value) fn.call(Optiomist.some(false)) else Failure.new(Messages::NON_BOOL) end in Optiomist::None Failure.new(Messages::NON_BOOL) end }.curry end |
#decimal ⇒ Object
Returns
lambda { |fn, option| ... }.
Converts provided option
‘s value to float. If the conversion is not possible it fails, otherwise executes the provider function fn
for the converted float value.
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/param_param/std.rb', line 195 def decimal lambda { |fn, option| begin float_value = Float(option.value) rescue StandardError return Failure.new(Messages::NON_DECIMAL) end fn.call(Optiomist.some(float_value)) }.curry end |
#gt ⇒ Object
Returns
lambda { |limit, option| ... }.
Checks if the option
‘s value is greater than the provided limit
.
124 125 126 |
# File 'lib/param_param/std.rb', line 124 def gt ->(limit, option) { option.value > limit ? Success.new(option) : Failure.new(Messages::NOT_GT) }.curry end |
#gte ⇒ Object
Returns
lambda { |limit, option| ... }.
Checks if the option
‘s value is greater than or equal to the provided limit
.
116 117 118 |
# File 'lib/param_param/std.rb', line 116 def gte ->(limit, option) { option.value >= limit ? Success.new(option) : Failure.new(Messages::NOT_GTE) }.curry end |
#included_in ⇒ Object
Verifies inclusion of a value in a collection.
Returns
lambda { |collection, option| ... }.
Verifies if value of the option
is included in the provided collection
.
43 44 45 46 47 |
# File 'lib/param_param/std.rb', line 43 def included_in lambda { |collection, option| collection.include?(option.value) ? Success.new(option) : Failure.new(Messages::NOT_INCLUDED) }.curry end |
#integer ⇒ Object
Returns
lambda { |fn, option| ... }.
Converts provided option
‘s value to integer. If the conversion is not possible it fails, otherwise executes the provider function fn
for the converted integer value.
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/param_param/std.rb', line 178 def integer lambda { |fn, option| begin integer_value = Integer(option.value) rescue StandardError return Failure.new(Messages::NON_INTEGER) end fn.call(Optiomist.some(integer_value)) }.curry end |
#lt ⇒ Object
Returns
lambda { |limit, option| ... }.
Checks if the option
‘s value is less than the provided limit
.
140 141 142 |
# File 'lib/param_param/std.rb', line 140 def lt ->(limit, option) { option.value < limit ? Success.new(option) : Failure.new(Messages::NOT_LT) }.curry end |
#lte ⇒ Object
Returns
lambda { |limit, option| ... }.
Checks if the option
‘s value is less than or equal to the provided limit
.
132 133 134 |
# File 'lib/param_param/std.rb', line 132 def lte ->(limit, option) { option.value <= limit ? Success.new(option) : Failure.new(Messages::NOT_LTE) }.curry end |
#max_size ⇒ Object
Returns
lambda { |limit, option| ... }.
Checks if the size of the value in option
does not exceed provided limit
.
148 149 150 151 152 |
# File 'lib/param_param/std.rb', line 148 def max_size lambda { |limit, option| option.value.size <= limit ? Success.new(option) : Failure.new(Messages::TOO_LONG) }.curry end |
#min_size ⇒ Object
Returns
lambda { |limit, option| ... }.
Checks if the size of the value in option
is not lower than the provided limit
.
158 159 160 161 162 |
# File 'lib/param_param/std.rb', line 158 def min_size lambda { |limit, option| option.value.size >= limit ? Success.new(option) : Failure.new(Messages::TOO_SHORT) }.curry end |
#not_blank ⇒ Object
Verifies if value is not blank.
Returns
lambda { |option| ... }.
It fails if provided option
is blank, otherwise succeeds with the option
.
103 104 105 |
# File 'lib/param_param/std.rb', line 103 def not_blank ->(option) { blank?(option.value) ? Failure.new(Messages::BLANK) : Success.new(option) } end |
#optional ⇒ Object
Describes an optional value.
Returns
lambda { |fn, option| ... }.
If option
is the Optiomist::None
it succeeds causing the parameter not to be included in the final result. Otherwise executes the funciton fn
for the option.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/param_param/std.rb', line 56 def optional lambda { |fn, option| case option in Optiomist::None Success.new(option) in Optiomist::Some fn.call(option) end }.curry end |
#required ⇒ Object
Describes a required value.
Returns
lambda { |fn, option| ... }.
If option
is a Optiomist::None
it fails otherwise executes the funciton fn
for the option.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/param_param/std.rb', line 73 def required lambda { |fn, option| case option in Optiomist::None Failure.new(Messages::MISSING) in Optiomist::Some fn.call(option) end }.curry end |
#string ⇒ Object
Returns
lambda { |fn, option| ... }.
Converts provided option
‘s value to string. If the conversion is not possible it fails, otherwise executes the provider function fn
for the converted string value.
235 236 237 238 239 240 241 242 243 244 |
# File 'lib/param_param/std.rb', line 235 def string lambda { |fn, option| case option in Optiomist::Some fn.call(Optiomist.some(option.value.to_s)) in Optiomist::None Failure.new(Messages::NON_STRING) end }.curry end |