Class: Tainbox::TypeConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/tainbox/type_converter.rb

Class Method Summary collapse

Instance Method Summary collapse

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
  @options = options
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

#convertObject



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