lib/data_model/fixtures/hash.rb in data_model-0.4.0 vs lib/data_model/fixtures/hash.rb in data_model-0.5.0

- old
+ new

@@ -1,43 +1,54 @@ -# typed: strict - module DataModel + # Test data for hash schemas module Fixtures::Hash include Fixtures - extend T::Sig extend self - TContact = T.type_alias { T::Hash[Symbol, T.untyped] } - - sig { returns(TContact) } + # 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 - sig { returns(Example) } + # 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| T.cast(h, TContact).delete(:email) }, + missing_email: example_contact.tap { |h| h.delete(:email) }, invalid_field: example_contact.merge(email: 123), other_type: [] }, ) end - sig { returns(Example) } + # 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 }], @@ -47,10 +58,11 @@ missing: nil }, ) end - sig { returns(Example) } + # 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 }],