Sha256: db4aae1db68d3bf95f8bc2fac1c1639f2be601f60d9d39bf2f98e7311fbd4f58

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

module DataModel
	# Test data for hash schemas
	module Fixtures::Hash
		include Fixtures
		extend self

		# hash data conforming to the contact schema
		# @return [Hash{Symbol => String}] the hash
		def example_contact
			{
				first_name: "foo",
				last_name: "bar",
				email: "foo@bar.com"
			}
		end

		# alternate hash syntax for when you want to type keys and values
		# @return [Example] the example
		def dictionary
			Example.new(
				[:hash, [symbol: :string]],
				variants: {
					valid: { foo: "bar" },
					invalid: { foo: 123 }
				},
			)
		end

		# hash contact example
		# @return [Example] the example
		def contact
			Example.new(
				[:hash,
					[:first_name, :string],
					[:last_name, :string, { optional: true }],
					[:email, :string]],
				variants: {
					valid: example_contact,
					missing: nil,
					coercible: example_contact.to_a,
					missing_email: example_contact.tap { |h| h.delete(:email) },
					invalid_field: example_contact.merge(email: 123),
					other_type: []
				},
			)
		end

		# hash contact example that is optional
		# @return [Example] the example
		def optional_contact
			Example.new(
				[:hash, { optional: true },
					[:first_name, :string],
					[:last_name, :string, { optional: true }],
					[:email, :string]],
				variants: {
					valid: example_contact,
					missing: nil
				},
			)
		end

		# hash contact example that is closed to extra keys
		# @return [Example] the example
		def closed_contact
			Example.new(
				[:hash, { open: false },
					[:first_name, :string],
					[:last_name, :string, { optional: true }],
					[:email, :string]],
				variants: {
					valid: example_contact,
					extra_keys: example_contact.merge(extra: "keys")
				},
			)
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
data_model-0.6.1 lib/data_model/fixtures/hash.rb
data_model-0.6.0 lib/data_model/fixtures/hash.rb
data_model-0.5.0 lib/data_model/fixtures/hash.rb