Sha256: 071dd9458c68331831ca0fcf44d3a3dc78cd023b7d49dc995cc2ada4ddace49e

Contents?: true

Size: 837 Bytes

Versions: 4

Compression:

Stored size: 837 Bytes

Contents

# frozen_string_literal: true

require 'set'
require_relative 'array_coercer'

module Grape
  module Validations
    module Types
      # Takes the given array and converts it to a set. Every element of the set
      # is also coerced.
      class SetCoercer < ArrayCoercer
        def initialize(type, strict = false)
          super

          @coercer = nil
        end

        def call(value)
          return InvalidValue.new unless value.is_a?(Array)

          coerce_elements(value)
        end

        protected

        def coerce_elements(collection)
          collection.each_with_object(Set.new) do |elem, memo|
            coerced_elem = elem_coercer.call(elem)

            return coerced_elem if coerced_elem.is_a?(InvalidValue)

            memo.add(coerced_elem)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
grape-2.0.0 lib/grape/validations/types/set_coercer.rb
grape-1.8.0 lib/grape/validations/types/set_coercer.rb
grape-1.7.1 lib/grape/validations/types/set_coercer.rb
grape-1.7.0 lib/grape/validations/types/set_coercer.rb