class Lookbook::StringValueCaster
def active_model_cast
def active_model_cast type_class = "ActiveModel::Type::#{@type.camelize}".constantize type_class.new.cast(@value) end
def call
def call return @nil if @value.empty? public_send(@cast_method) rescue => exception Lookbook.logger.debug "Failed to parse '#{@value}' into a '#{@type}' [#{exception}]" raise exception end
def cast_to_array
def cast_to_array result = YamlParser.call(@value) unless result.is_a?(Array) raise ParserError.new "'#{@value}' is not a YAML Array" end result end
def cast_to_datetime
def cast_to_datetime DateTime.parse(@value) end
def cast_to_hash
def cast_to_hash result = YamlParser.call(@value) unless result.is_a?(Hash) raise ParserError.new "'#{@value}' is not a YAML Hash" end result end
def cast_to_string
def cast_to_string @value end
def cast_to_symbol
def cast_to_symbol @value.delete_prefix(":").to_sym if @value.present? end
def initialize(value, type = "string")
def initialize(value, type = "string") @value = value.to_s @type = type.to_s.downcase @cast_method = :"cast_to_#{@type}" unless respond_to?(@cast_method) raise ArgumentError.new "'#{@type}' is not a valid value type to cast to." end end