Class: Philosophal::Convertor
- Inherits:
-
Object
- Object
- Philosophal::Convertor
- Defined in:
- lib/philosophal/convertor.rb
Constant Summary collapse
- METHOD_TYPE_MAP =
{ Symbol => :convert_to_symbol, String => :convert_to_string, Integer => :convert_to_integer, Float => :convert_to_float, Array => :convert_to_array, Hash => :convert_to_hash, Time => :convert_to_time, Date => :convert_to_date, DateTime => :convert_to_date_time, Philosophal::Types::AnyType::Instance => :convert_to_any, Philosophal::Types::BooleanType::Instance => :convert_to_boolean, Philosophal::Types::ArrayOfType => :convert_to_array_of, Philosophal::Types::HashOfType => :convert_to_hash_of, Pathname => :convert_to_pathname }.freeze
Class Method Summary collapse
- .convert_method_for(type) ⇒ Object
- .convert_to_any(obj) ⇒ Object
- .convert_to_array(obj) ⇒ Object
- .convert_to_array_of(obj, subtype) ⇒ Object
- .convert_to_boolean(obj) ⇒ Object
- .convert_to_date(obj) ⇒ Object
- .convert_to_date_time(obj) ⇒ Object
- .convert_to_float(obj) ⇒ Object
- .convert_to_hash(obj) ⇒ Object
- .convert_to_hash_of(obj, subtype) ⇒ Object
- .convert_to_integer(obj) ⇒ Object
- .convert_to_pathname(obj) ⇒ Object
- .convert_to_string(obj) ⇒ Object
- .convert_to_symbol(obj) ⇒ Object
- .convert_to_time(obj) ⇒ Object
Class Method Details
.convert_method_for(type) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/philosophal/convertor.rb', line 23 def convert_method_for(type) if type.respond_to?(:subtype) [METHOD_TYPE_MAP[type.class], type.subtype] else METHOD_TYPE_MAP[type] end end |
.convert_to_any(obj) ⇒ Object
95 96 97 |
# File 'lib/philosophal/convertor.rb', line 95 def convert_to_any(obj) obj end |
.convert_to_array(obj) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/philosophal/convertor.rb', line 55 def convert_to_array(obj) if obj.respond_to?(:to_a) obj.to_a else [obj] end end |
.convert_to_array_of(obj, subtype) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/philosophal/convertor.rb', line 119 def convert_to_array_of(obj, subtype) subtype_convert_method = convert_method_for(subtype) raise Philosophal::TypeError unless subtype_convert_method convert_to_array(obj).tap do |new_obj| new_obj.map! do |item| send(subtype_convert_method, item) end end end |
.convert_to_boolean(obj) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/philosophal/convertor.rb', line 99 def convert_to_boolean(obj) if obj.is_a?(Numeric) return true if obj == 1 return false if obj.zero? elsif obj.is_a?(String) obj_downcase = obj.downcase return true if obj_downcase == 'true' return false if obj_downcase == 'false' end raise Philosophal::TypeError end |
.convert_to_date(obj) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/philosophal/convertor.rb', line 77 def convert_to_date(obj) return obj.to_date if obj.respond_to?(:to_date) return Date.new(*obj) if obj.is_a?(Array) return Date.parse(obj) if obj.is_a?(String) return Time.at(obj).to_date if obj.is_a?(Numeric) raise Philosophal::TypeError end |
.convert_to_date_time(obj) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/philosophal/convertor.rb', line 86 def convert_to_date_time(obj) return obj.to_datetime if obj.respond_to?(:to_datetime) return DateTime.new(*obj) if obj.is_a?(Array) return DateTime.parse(obj) if obj.is_a?(String) return Time.zone.at(obj).to_datetime if obj.is_a?(Numeric) raise Philosophal::TypeError end |
.convert_to_float(obj) ⇒ Object
49 50 51 52 53 |
# File 'lib/philosophal/convertor.rb', line 49 def convert_to_float(obj) raise Philosophal::TypeError unless obj.respond_to?(:to_f) obj.to_f end |
.convert_to_hash(obj) ⇒ Object
63 64 65 66 67 |
# File 'lib/philosophal/convertor.rb', line 63 def convert_to_hash(obj) raise Philosophal::TypeError unless obj.respond_to?(:to_h) obj.to_h end |
.convert_to_hash_of(obj, subtype) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/philosophal/convertor.rb', line 130 def convert_to_hash_of(obj, subtype) key_convert_method = convert_method_for(subtype[:key_type]) raise Philosophal::TypeError unless key_convert_method value_convert_method = convert_method_for(subtype[:value_type]) raise Philosophal::TypeError unless value_convert_method convert_to_hash(obj).tap do |new_obj| new_obj.transform_keys! { |key| send(key_convert_method, key) } new_obj.transform_values! { |value| send(value_convert_method, value) } end end |
.convert_to_integer(obj) ⇒ Object
43 44 45 46 47 |
# File 'lib/philosophal/convertor.rb', line 43 def convert_to_integer(obj) raise Philosophal::TypeError unless obj.respond_to?(:to_i) obj.to_i end |
.convert_to_pathname(obj) ⇒ Object
113 114 115 116 117 |
# File 'lib/philosophal/convertor.rb', line 113 def convert_to_pathname(obj) raise Philosophal::TypeError unless obj.respond_to?(:to_s) Pathname.new(obj) end |
.convert_to_string(obj) ⇒ Object
37 38 39 40 41 |
# File 'lib/philosophal/convertor.rb', line 37 def convert_to_string(obj) raise Philosophal::TypeError unless obj.respond_to?(:to_s) obj.to_s end |
.convert_to_symbol(obj) ⇒ Object
31 32 33 34 35 |
# File 'lib/philosophal/convertor.rb', line 31 def convert_to_symbol(obj) raise Philosophal::TypeError unless obj.respond_to?(:to_sym) obj.to_sym end |
.convert_to_time(obj) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/philosophal/convertor.rb', line 69 def convert_to_time(obj) return Time.local(*obj) if obj.is_a?(Array) return Time.parse(obj) if obj.is_a?(String) return Time.at(obj) if obj.is_a?(Numeric) raise Philosophal::TypeError end |