Sha256: 0a42f8773a233c511acdddd69897cb71ee3009eab05a17cfceda8a2aeb12c9ed

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

module Rasti
  module Types
    class Hash

      include Castable

      attr_reader :key_type, :value_type

      def self.[](key_type, value_type)
        new key_type, value_type
      end

      def to_s
        "#{self.class}[#{key_type}, #{value_type}]"
      end
      alias_method :inspect, :to_s

      private

      def initialize(key_type, value_type)
        @key_type = key_type
        @value_type = value_type
      end

      def valid?(value)
        value.is_a? ::Hash
      end

      def transform(value)
        MultiCaster.cast!(self, value) do |multi_caster|
          value.each_with_object({}) do |(k,v), hash|
            casted_key = multi_caster.cast type: key_type,
                                           value: k,
                                           error_key: k

            casted_value = multi_caster.cast type: value_type,
                                             value: v,
                                             error_key: k

            hash[casted_key] = casted_value
          end
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rasti-types-2.0.1 lib/rasti/types/hash.rb
rasti-types-2.0.0 lib/rasti/types/hash.rb