Sha256: 834a69a262459200e33d0c82b79a5ae20d9c18e3f5a3d57632ac28b1ebde8b6d

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module Schemacop::V2
  class Caster
    DEFAULT_CASTERS = {
      String  => {
        Integer => proc { |s| s.blank? ? nil : Integer(s, 10) },
        Float   => proc { |s| s.blank? ? nil : Float(s) }
      },
      Float   => {
        Integer => proc { |f| Integer(f) }
      },
      Integer => {
        Float => proc { |f| Float(f) }
      }
    }.freeze

    def initialize(casts, data, target_type)
      @casts = casts
      @data = data
      @target_type = target_type
      @caster = nil

      if casts.is_a?(Array)
        from_types = casts
      elsif casts.is_a?(Hash)
        from_types = casts.keys
      else
        fail Exceptions::InvalidSchemaError, 'Option `cast` must be either an array or a hash.'
      end

      return unless from_types.include?(data.class)

      if (casts.is_a?(Array) && casts.include?(data.class)) || casts[data.class] == :default
        @caster = DEFAULT_CASTERS[data.class][target_type]
      else
        @caster = casts[data.class]
      end
    end

    def castable?
      !@caster.nil?
    end

    def cast
      fail 'Not castable.' unless castable?

      return @caster.call(@data)
    rescue StandardError => e
      fail Exceptions::InvalidSchemaError,
           "Could not cast value #{@value.inspect} to #{@target_type}: #{e.message}."
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
schemacop-3.0.0.rc3 lib/schemacop/v2/caster.rb
schemacop-3.0.0.rc2 lib/schemacop/v2/caster.rb
schemacop-3.0.0.rc1 lib/schemacop/v2/caster.rb
schemacop-3.0.0.rc0 lib/schemacop/v2/caster.rb