Sha256: d42b307c8240784ddd9d8460616d495e40eaf5b353cd63ef1b66467f642debd6

Contents?: true

Size: 1.09 KB

Versions: 11

Compression:

Stored size: 1.09 KB

Contents

module Restspec::Schema::Types
  # It will return an element of a set of elements while making a
  # example and checks against that same set of element while
  # validating. The following options are needed:
  #
  # elements: An array of the element to use
  #
  # @example:
  #   attribute :pet_type, one_of(elements: %w{dog cat})
  #
  class OneOfType < BasicType
    # @param attribute [Restspec::Schema::Attribute] the atribute of the schema.
    #
    # @return [Object] A sample of the elements array.
    # @raise KeyError if the elements option was not initialized.
    def example_for(attribute)
      elements.sample
    end

    # @param attribute [Restspec::Schema::Attribute] the atribute of the schema.
    # @param value [Object] the value of the attribute.
    #
    # @return [true, false] If the value is included in the elements.
    # @raise KeyError if the elements option was not initialized.
    def valid?(attribute, value)
      elements.include?(value)
    end

    def to_s
      "OneOfType(#{elements})"
    end

    private

    def elements
      options.fetch(:elements)
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
restspec-0.3.2 lib/restspec/schema/types/one_of_type.rb
restspec-0.3.1 lib/restspec/schema/types/one_of_type.rb
restspec-0.3.0 lib/restspec/schema/types/one_of_type.rb
restspec-0.2.6 lib/restspec/schema/types/one_of_type.rb
restspec-0.2.5 lib/restspec/schema/types/one_of_type.rb
restspec-0.2.4 lib/restspec/schema/types/one_of_type.rb
restspec-0.2.3 lib/restspec/schema/types/one_of_type.rb
restspec-0.2.2 lib/restspec/schema/types/one_of_type.rb
restspec-0.2.1 lib/restspec/schema/types/one_of_type.rb
restspec-0.2 lib/restspec/schema/types/one_of_type.rb
restspec-0.1 lib/restspec/schema/types/one_of_type.rb