Module: Sequent::Core::Helpers::TypeConversionSupport

Included in:
BaseCommand, ValueObject
Defined in:
lib/sequent/core/helpers/type_conversion_support.rb

Overview

Will parse all values to the correct types. The raw values are typically posted from the web and are therefor mostly strings. To parse a raw value your class must have a parse_from_string method that returns the parsed values. See sequent/core/ext for currently supported classes.

Instance Method Summary collapse

Instance Method Details

#parse_attrs_to_correct_typesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sequent/core/helpers/type_conversion_support.rb', line 15

def parse_attrs_to_correct_types
  the_copy = dup
  the_copy.class.types.each do |name, type|
    raw_value = the_copy.send(name.to_s)
    next if raw_value.nil?

    if raw_value.respond_to?(:parse_attrs_to_correct_types)
      the_copy.send("#{name}=", raw_value.parse_attrs_to_correct_types)
    else
      parsed_value = Sequent::Core::Helpers::StringToValueParsers.for(type).parse_from_string(raw_value)
      the_copy.send("#{name}=", parsed_value)
    end
  end
  the_copy
rescue StandardError => e
  raise TypeConversionError, e.message
end