Module: ActiveModel::AttributeFilters::Common::Convert
- Included in:
- ActiveModel::AttributeFilters::Common
- Defined in:
- lib/attribute-filters/common_filters/convert.rb
Overview
This module contains attribute filters responsible for converting the attributes.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#attributes_to_b
(also: #attributes_to_boolean, #convert_to_boolean)
Convert attributes to boolean values.
-
#attributes_to_f
(also: #attributes_to_floats, #convert_to_floats)
Convert attributes to floats.
-
#attributes_to_i
(also: #attributes_to_integers, #convert_to_integers)
Convert attributes to integers.
-
#attributes_to_numbers
(also: #convert_to_numbers, #attribute_to_numbers)
Convert attributes to numbers.
-
#attributes_to_r
(also: #attributes_to_rationals, #attributes_to_fractions, #convert_to_rationals)
Convert attributes to rationals.
-
#attributes_to_s
(also: #attributes_to_strings, #convert_to_strings)
Convert attributes to strings.
Methods included from FilteringRegistration
Instance Method Details
#attributes_to_b Also known as: attributes_to_boolean, convert_to_boolean
If a value of currently processed attribute is an array then each element of the array is changed.
This method returns an undefined value.
Convert attributes to boolean values.
The attrubutes are taken from the attribute set called should_be_boolean
.
232 233 234 |
# File 'lib/attribute-filters/common_filters/convert.rb', line 232 def attributes_to_b attributes_convert(:should_be_boolean, :to_b_default, :process_blank) { |v| !!v } end |
#attributes_to_f Also known as: attributes_to_floats, convert_to_floats
If a value of currently processed attribute is an array then each element of the array is changed.
This method returns an undefined value.
Convert attributes to floats.
The attrubutes are taken from the attribute set called should_be_floats
.
136 137 138 |
# File 'lib/attribute-filters/common_filters/convert.rb', line 136 def attributes_to_f attributes_convert(:should_be_floats, :to_f_default) { |v| v.to_f } end |
#attributes_to_i Also known as: attributes_to_integers, convert_to_integers
If a value of currently processed attribute is an array then each element of the array is changed.
This method returns an undefined value.
Convert attributes to integers.
The attrubutes are taken from the attribute set called should_be_integers
.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/attribute-filters/common_filters/convert.rb', line 92 def attributes_to_i attributes_convert(:should_be_integers, :to_i_default) do |v, atr_name, set_obj| if set_obj.has_annotation?(atr_name, :to_i_base) if v.method(:to_i).arity != 0 v.to_i(set_obj.annotation(atr_name, :to_i_base) || 10) elsif set_obj.has_annotation?(atr_name, :to_i_default) set_obj.annotation(atr_name, :to_i_default) else v end else v.to_i end end end |
#attributes_to_numbers Also known as: convert_to_numbers, attribute_to_numbers
If a value of currently processed attribute is an array then each element of the array is changed.
This method returns an undefined value.
Convert attributes to numbers.
The attrubutes are taken from the attribute set called should_be_numbers
. It works the same as converting to floats but uses different attribute set.
167 168 169 |
# File 'lib/attribute-filters/common_filters/convert.rb', line 167 def attributes_to_numbers attributes_convert(:should_be_numbers, :to_num_default) { |v| v.to_f } end |
#attributes_to_r Also known as: attributes_to_rationals, attributes_to_fractions, convert_to_rationals
If a value of currently processed attribute is an array then each element of the array is changed.
This method returns an undefined value.
Convert attributes to rationals.
The attrubutes are taken from the attribute set called should_be_rationals
.
197 198 199 |
# File 'lib/attribute-filters/common_filters/convert.rb', line 197 def attributes_to_r attributes_convert(:should_be_rationals, :to_r_default) { |v| v.to_r } end |
#attributes_to_s Also known as: attributes_to_strings, convert_to_strings
If a value of currently processed attribute is an array then each element of the array is changed.
This method returns an undefined value.
Convert attributes to strings.
The attrubutes are taken from the attribute set called should_be_strings
.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/attribute-filters/common_filters/convert.rb', line 47 def attributes_to_s attributes_convert(:should_be_strings, :to_s_default) do |v, atr_name, set_obj| if set_obj.has_annotation?(atr_name, :to_s_base) v = 0 if v.nil? if v.method(:to_s).arity != 0 v.to_s(set_obj.annotation(atr_name, :to_s_base) || 10) elsif set_obj.has_annotation?(atr_name, :to_s_default) set_obj.annotation(atr_name, :to_s_default) else v end else v.to_s end end end |