Module: ActiveForm::Mixins::Casting::ClassMethods
- Defined in:
- lib/active_form/mixins/casting.rb
Instance Method Summary collapse
-
#binary_to_string(value) ⇒ Object
type casting methods.
- #boolean_to_value(boolean) ⇒ Object
- #data_to_yaml(value) ⇒ Object
- #date_to_string(date) ⇒ Object
- #define_casting_filter(prc = nil, &block) ⇒ Object (also: #casting_filter=)
- #reset_casting_filter ⇒ Object
- #string_to_date(string) ⇒ Object
- #string_to_dummy_time(string) ⇒ Object
- #string_to_time(string) ⇒ Object
- #time_to_string(time) ⇒ Object
- #value_to_boolean(value) ⇒ Object
- #yaml_to_data(value) ⇒ Object
Instance Method Details
#binary_to_string(value) ⇒ Object
type casting methods
89 90 91 |
# File 'lib/active_form/mixins/casting.rb', line 89 def binary_to_string(value) value end |
#boolean_to_value(boolean) ⇒ Object
144 145 146 |
# File 'lib/active_form/mixins/casting.rb', line 144 def boolean_to_value(boolean) boolean ? "true" : "false" end |
#data_to_yaml(value) ⇒ Object
93 94 95 |
# File 'lib/active_form/mixins/casting.rb', line 93 def data_to_yaml(value) value.to_yaml end |
#date_to_string(date) ⇒ Object
109 110 111 |
# File 'lib/active_form/mixins/casting.rb', line 109 def date_to_string(date) date.respond_to?(:to_formatted_s) ? date.to_formatted_s(:long) : date.to_s end |
#define_casting_filter(prc = nil, &block) ⇒ Object Also known as: casting_filter=
78 79 80 |
# File 'lib/active_form/mixins/casting.rb', line 78 def define_casting_filter(prc = nil, &block) define_singleton_method(:casting_filter, &(block_given? ? block : prc)) end |
#reset_casting_filter ⇒ Object
83 84 85 |
# File 'lib/active_form/mixins/casting.rb', line 83 def reset_casting_filter define_singleton_method(:casting_filter) rescue nil end |
#string_to_date(string) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/active_form/mixins/casting.rb', line 102 def string_to_date(string) return string unless string.is_a?(String) date_array = ParseDate.parsedate(string) # treat 0000-00-00 as nil Date.new(date_array[0], date_array[1], date_array[2]) rescue nil end |
#string_to_dummy_time(string) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/active_form/mixins/casting.rb', line 128 def string_to_dummy_time(string) return string unless string.is_a?(String) time_array = ParseDate.parsedate(string) # pad the resulting array with dummy date information time_array[0] = 2000; time_array[1] = 1; time_array[2] = 1; Time.send(ActiveForm::Element::Base.default_timezone, *time_array) rescue nil end |
#string_to_time(string) ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/active_form/mixins/casting.rb', line 113 def string_to_time(string) return string unless string.is_a?(String) if string.strip.match(/^(\d{1,2})[:\.](\d{1,2})([:\.](\d{1,2}))?$/) current_time = Time.now string = "#{current_time.year}-#{current_time.month}-#{current_time.day} #{string}" end # treat 0000-00-00 00:00:00 as nil time_array = ParseDate.parsedate(string)[0..5] Time.send(ActiveForm::Element::Base.default_timezone, *time_array) rescue nil end |
#time_to_string(time) ⇒ Object
124 125 126 |
# File 'lib/active_form/mixins/casting.rb', line 124 def time_to_string(time) time.respond_to?(:to_formatted_s) ? time.to_formatted_s(:long) : time.to_s end |
#value_to_boolean(value) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/active_form/mixins/casting.rb', line 136 def value_to_boolean(value) return value if value==true || value==false case value.to_s.downcase when "true", "t", "1" then true else false end end |