Sha256: c90be95681c3bb5e9a939b94abf37f64de9f5e03fcb0f90de635f7da61206e8d

Contents?: true

Size: 722 Bytes

Versions: 3

Compression:

Stored size: 722 Bytes

Contents

module RiceBubble
  class Attributes
    class Any < Base
      attr_reader :members

      def initialize(*members, &)
        super(&)
        @members = members.flatten
      end

      def valid?(value)
        !which(value).nil?
      end

      def validate!(value, path:, **)
        member = which(value)
        return unless member.nil?

        expected = "one of [#{members.map(&:description).join(", ")}]"
        raise ValidationError,
              "#{path} expected #{expected} but received #{value.inspect}"
      end

      def coerce(value)
        which(value)&.coerce(value) || value
      end

      def which(value)
        members.find { |member| member.valid?(value) }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rice_bubble-0.1.2 lib/rice_bubble/attributes/any.rb
rice_bubble-0.1.1 lib/rice_bubble/attributes/any.rb
rice_bubble-0.1.0 lib/rice_bubble/attributes/any.rb