Class: Tainbox::TypeConverter
Class Method Summary collapse
Instance Method Summary collapse
- #convert ⇒ Object
-
#initialize(type, value, options: {}) ⇒ TypeConverter
constructor
A new instance of TypeConverter.
Constructor Details
#initialize(type, value, options: {}) ⇒ TypeConverter
Returns a new instance of TypeConverter.
15 16 17 18 19 |
# File 'lib/tainbox/type_converter.rb', line 15 def initialize(type, value, options: {}) @type = type @value = value = end |
Class Method Details
.conversion_method_name_for(type) ⇒ Object
11 12 13 |
# File 'lib/tainbox/type_converter.rb', line 11 def self.conversion_method_name_for(type) "convert_to_#{type.to_s.downcase}".to_sym end |
.define_converter(type, block) ⇒ Object
5 6 7 8 9 |
# File 'lib/tainbox/type_converter.rb', line 5 def self.define_converter(type, block) method_name = conversion_method_name_for(type) raise "Converter for #{type} already exists" if instance_methods.include?(method_name) define_method(method_name, block) end |
Instance Method Details
#convert ⇒ Object
21 22 23 24 25 |
# File 'lib/tainbox/type_converter.rb', line 21 def convert method_name = self.class.conversion_method_name_for(type) raise "Type not supported: #{type}" unless respond_to?(method_name) send(method_name) end |