Class: RailsParam::Coercion
- Inherits:
-
Object
- Object
- RailsParam::Coercion
- Defined in:
- lib/rails_param/coercion.rb,
lib/rails_param/coercion/hash_param.rb,
lib/rails_param/coercion/time_param.rb,
lib/rails_param/coercion/array_param.rb,
lib/rails_param/coercion/float_param.rb,
lib/rails_param/coercion/string_param.rb,
lib/rails_param/coercion/boolean_param.rb,
lib/rails_param/coercion/integer_param.rb,
lib/rails_param/coercion/virtual_param.rb,
lib/rails_param/coercion/big_decimal_param.rb
Defined Under Namespace
Classes: ArrayParam, BigDecimalParam, BooleanParam, FloatParam, HashParam, IntegerParam, StringParam, TimeParam, VirtualParam
Constant Summary collapse
- 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
Instance Attribute Summary collapse
-
#coercion ⇒ Object
readonly
Returns the value of attribute coercion.
-
#param ⇒ Object
readonly
Returns the value of attribute param.
Instance Method Summary collapse
- #coerce ⇒ Object
-
#initialize(param, type, options) ⇒ Coercion
constructor
A new instance of Coercion.
- #klass_for(type) ⇒ Object
Constructor Details
#initialize(param, type, options) ⇒ Coercion
Returns a new instance of Coercion.
23 24 25 26 |
# File 'lib/rails_param/coercion.rb', line 23 def initialize(param, type, ) @param = param @coercion = klass_for(type).new(param: param, options: , type: type) end |
Instance Attribute Details
#coercion ⇒ Object (readonly)
Returns the value of attribute coercion.
3 4 5 |
# File 'lib/rails_param/coercion.rb', line 3 def coercion @coercion end |
#param ⇒ Object (readonly)
Returns the value of attribute param.
3 4 5 |
# File 'lib/rails_param/coercion.rb', line 3 def param @param end |
Instance Method Details
#coerce ⇒ Object
35 36 37 38 39 |
# File 'lib/rails_param/coercion.rb', line 35 def coerce return nil if param.nil? coercion.coerce end |
#klass_for(type) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/rails_param/coercion.rb', line 28 def klass_for(type) klass = PARAM_TYPE_MAPPING[type] return klass if klass raise TypeError end |