Module: ActiveForm::Mixins::Casting
- Defined in:
- lib/active_form/mixins/casting.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #cast_value(value) ⇒ Object
- #casting_for_array ⇒ Object
- #casting_for_boolean ⇒ Object
- #casting_for_date ⇒ Object
- #casting_for_datetime ⇒ Object (also: #casting_for_timestamp)
- #casting_for_float ⇒ Object
- #casting_for_integer ⇒ Object
- #casting_for_string ⇒ Object (also: #casting_for_text)
- #casting_for_time ⇒ Object
- #casting_for_yaml ⇒ Object
- #define_casting_filter(prc = nil, &block) ⇒ Object (also: #casting_filter=)
- #reset_casting_filter ⇒ Object
- #type_cast=(type = :string) ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 8 |
# File 'lib/active_form/mixins/casting.rb', line 5 def self.included(base) base.send(:extend, ClassMethods) base.send(:attr_reader, :type_cast) end |
Instance Method Details
#cast_value(value) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/active_form/mixins/casting.rb', line 10 def cast_value(value) if self.respond_to?(:casting_filter) self.casting_filter(value) elsif self.class.respond_to?(:casting_filter) self.class.casting_filter(value) else value end end |
#casting_for_array ⇒ Object
46 47 48 |
# File 'lib/active_form/mixins/casting.rb', line 46 def casting_for_array [lambda { |value| value.is_a?(Array) ? value : [*value] }, lambda { |value| [*value].join(', ') }] end |
#casting_for_boolean ⇒ Object
63 64 65 |
# File 'lib/active_form/mixins/casting.rb', line 63 def casting_for_boolean [self.class.method_to_proc(:value_to_boolean), self.class.method_to_proc(:boolean_to_value)] end |
#casting_for_date ⇒ Object
59 60 61 |
# File 'lib/active_form/mixins/casting.rb', line 59 def casting_for_date [self.class.method_to_proc(:string_to_date), self.class.method_to_proc(:date_to_string)] end |
#casting_for_datetime ⇒ Object Also known as: casting_for_timestamp
50 51 52 |
# File 'lib/active_form/mixins/casting.rb', line 50 def casting_for_datetime [self.class.method_to_proc(:string_to_time), self.class.method_to_proc(:time_to_string)] end |
#casting_for_float ⇒ Object
42 43 44 |
# File 'lib/active_form/mixins/casting.rb', line 42 def casting_for_float [lambda { |value| value.is_a?(Float) ? value : value.to_f rescue 0 }, nil] end |
#casting_for_integer ⇒ Object
34 35 36 |
# File 'lib/active_form/mixins/casting.rb', line 34 def casting_for_integer [lambda { |value| value.is_a?(Integer) ? value : value.to_i rescue value ? 1 : 0 }, nil] end |
#casting_for_string ⇒ Object Also known as: casting_for_text
29 30 31 |
# File 'lib/active_form/mixins/casting.rb', line 29 def casting_for_string [lambda { |value| value.is_a?(String) ? value : value.to_s }, nil] end |
#casting_for_time ⇒ Object
55 56 57 |
# File 'lib/active_form/mixins/casting.rb', line 55 def casting_for_time [self.class.method_to_proc(:string_to_dummy_time), self.class.method_to_proc(:time_to_string)] end |
#casting_for_yaml ⇒ Object
38 39 40 |
# File 'lib/active_form/mixins/casting.rb', line 38 def casting_for_yaml [self.class.method_to_proc(:yaml_to_data), self.class.method_to_proc(:data_to_yaml)] end |
#define_casting_filter(prc = nil, &block) ⇒ Object Also known as: casting_filter=
67 68 69 |
# File 'lib/active_form/mixins/casting.rb', line 67 def define_casting_filter(prc = nil, &block) define_singleton_method(:casting_filter, &(block_given? ? block : prc)) end |
#reset_casting_filter ⇒ Object
72 73 74 |
# File 'lib/active_form/mixins/casting.rb', line 72 def reset_casting_filter define_singleton_method(:casting_filter) rescue nil end |
#type_cast=(type = :string) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/active_form/mixins/casting.rb', line 20 def type_cast=(type = :string) if self.respond_to?("casting_for_#{type}") @type_cast = type.to_sym cast, intern = self.send("casting_for_#{type}") define_casting_filter(cast) if cast define_formatting_filter(intern) if intern end end |