Class: Farce::Internal::DryTypes::MapType

Inherits:
CollectionType show all
Defined in:
lib/farce/integrations/dry_types.rb

Overview

A Map type that supports dry-types homogeneous maps and schemas.

Instance Attribute Summary

Attributes inherited from CollectionType

#source

Dry Types Integration collapse

Methods inherited from CollectionType

#call, #constrained?, #default?, #inspect, #meta, #name, #optional?, #primitive?, #pristine, #to_ast, #to_proc, #try, #valid?, #with

Instance Method Details

#map(key_type, value_type) ⇒ MapType

Apply dry types to every key and value before Farce construction.

Parameters:

  • key_type (Dry::Types::Type) —

    The key type.

  • value_type (Dry::Types::Type) —

    The value type.

Returns:

  • (MapType) —

    The homogeneous map type.



340
341
342
343
344
# File 'lib/farce/integrations/dry_types.rb', line 340

def map(key_type, value_type)
  typed = DryTypes.collection_source(source).map(key_type, value_type)
  typed = DryTypes.restore_optional_source(source, typed)
  self.class.new(typed, kind:, configuration:)
end

#schema(keys_or_map, meta = {}) ⇒ MapType #schema(keys_or_map) ⇒ MapType

Define or extend fixed keys and their dry types before Farce construction.

Overloads:

  • #schema(keys_or_map, meta = {}) ⇒ MapType

    Create a schema from the imported Hash source.

    Parameters:

    • keys_or_map (Hash, Array<Dry::Types::Schema::Key>) —

      The schema definition.

    • meta (Hash) (defaults to: {}) —

      Metadata applied while creating the schema.

    Returns:

    • (MapType) —

      The schema-backed map type.

  • #schema(keys_or_map) ⇒ MapType

    Extend an existing schema.

    Parameters:

    • keys_or_map (Hash, Array<Dry::Types::Schema::Key>) —

      Additional schema keys.

    Returns:

    • (MapType) —

      The schema-backed map type.



356
357
358
359
360
# File 'lib/farce/integrations/dry_types.rb', line 356

def schema(...)
  typed = DryTypes.collection_source(source).schema(...)
  typed = DryTypes.restore_optional_source(source, typed)
  self.class.new(typed, kind:, configuration:)
end

#strict(strict = true) ⇒ MapType

Return a schema that rejects unknown keys.

Parameters:

  • strict (Boolean) (defaults to: true) —

    Whether unknown keys are rejected.

Returns:

  • (MapType) —

    The configured map type.



365
# File 'lib/farce/integrations/dry_types.rb', line 365

def strict(strict = true) = rebuild_map_source(:strict, strict) # rubocop:disable Style/OptionalBooleanParameter

#with_key_transform(transform = nil) {|key| ... } ⇒ MapType

Transform input keys before schema lookup.

Parameters:

  • transform (#call, nil) (defaults to: nil) —

    The key transform.

Yields:

  • (key) —

    Transform an input key when no callable argument is given.

Yield Parameters:

  • key (Object) —

    An input key.

Yield Returns:

  • (Object) —

    The transformed key.

Returns:

  • (MapType) —

    The configured map type.



373
# File 'lib/farce/integrations/dry_types.rb', line 373

def with_key_transform(transform = nil, &) = rebuild_map_source(:with_key_transform, transform, &)

#with_type_transform(transform = nil) {|type| ... } ⇒ MapType

Transform schema key types when defining a schema.

Parameters:

  • transform (#call, nil) (defaults to: nil) —

    The type transform.

Yields:

  • (type) —

    Transform a schema key type when no callable argument is given.

Yield Parameters:

  • type (Dry::Types::Schema::Key) —

    A schema key type.

Yield Returns:

  • (Dry::Types::Schema::Key) —

    The transformed key type.

Returns:

  • (MapType) —

    The configured map type.



381
# File 'lib/farce/integrations/dry_types.rb', line 381

def with_type_transform(transform = nil, &) = rebuild_map_source(:with_type_transform, transform, &)