Class: MR::TypeConverter
- Inherits:
-
Object
- Object
- MR::TypeConverter
- Defined in:
- lib/mr/type_converter.rb
Constant Summary collapse
- TYPES =
[ :binary, :boolean, :date, :datetime, :timestamp, :decimal, :float, :integer, :primary_key, :string, :text, :slug, :hex, :file_name, :dir_path, :file_path, :path, :url, :email, :time ].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #binary(value) ⇒ Object
- #boolean(value) ⇒ Object
- #date(value) ⇒ Object
- #datetime(value) ⇒ Object (also: #timestamp)
- #decimal(value) ⇒ Object
- #float(value) ⇒ Object
-
#initialize(ar_column_class = nil) ⇒ TypeConverter
constructor
A new instance of TypeConverter.
- #integer(value) ⇒ Object (also: #primary_key)
- #string(value) ⇒ Object (also: #text, #slug, #hex, #file_name, #dir_path, #file_path, #path, #url, #email)
- #time(value) ⇒ Object
Constructor Details
#initialize(ar_column_class = nil) ⇒ TypeConverter
Returns a new instance of TypeConverter.
23 24 25 |
# File 'lib/mr/type_converter.rb', line 23 def initialize(ar_column_class = nil) @ar_column_class = ar_column_class || ActiveRecord::ConnectionAdapters::Column end |
Class Method Details
.valid?(type) ⇒ Boolean
19 20 21 |
# File 'lib/mr/type_converter.rb', line 19 def self.valid?(type) TYPES.include?(type.to_sym) end |
Instance Method Details
#binary(value) ⇒ Object
27 28 29 30 |
# File 'lib/mr/type_converter.rb', line 27 def binary(value) return if value.nil? @ar_column_class.binary_to_string(value) end |
#boolean(value) ⇒ Object
32 33 34 35 |
# File 'lib/mr/type_converter.rb', line 32 def boolean(value) return if value.nil? @ar_column_class.value_to_boolean(value) end |
#date(value) ⇒ Object
42 43 44 45 |
# File 'lib/mr/type_converter.rb', line 42 def date(value) return if value.nil? @ar_column_class.string_to_date(value) end |
#datetime(value) ⇒ Object Also known as: timestamp
47 48 49 50 |
# File 'lib/mr/type_converter.rb', line 47 def datetime(value) return if value.nil? @ar_column_class.string_to_time(value) end |
#decimal(value) ⇒ Object
53 54 55 56 |
# File 'lib/mr/type_converter.rb', line 53 def decimal(value) return if value.nil? @ar_column_class.value_to_decimal(value) end |
#float(value) ⇒ Object
37 38 39 40 |
# File 'lib/mr/type_converter.rb', line 37 def float(value) return if value.nil? value.to_f end |
#integer(value) ⇒ Object Also known as: primary_key
58 59 60 61 |
# File 'lib/mr/type_converter.rb', line 58 def integer(value) return if value.nil? @ar_column_class.value_to_integer(value) end |
#string(value) ⇒ Object Also known as: text, slug, hex, file_name, dir_path, file_path, path, url, email
64 65 66 67 |
# File 'lib/mr/type_converter.rb', line 64 def string(value) return if value.nil? value.to_s end |
#time(value) ⇒ Object
78 79 80 81 |
# File 'lib/mr/type_converter.rb', line 78 def time(value) return if value.nil? @ar_column_class.string_to_dummy_time(value) end |