Class: Dry::Types::Array::Member

Inherits:
Dry::Types::Array show all
Defined in:
lib/dry/types/array/member.rb

Overview

Member arrays define their member type that is applied to each element

Constant Summary

Constants inherited from Nominal

Nominal::ALWAYS

Instance Attribute Summary collapse

Attributes inherited from Nominal

#primitive

Attributes included from Options

#options

Instance Method Summary collapse

Methods inherited from Dry::Types::Array

#of

Methods inherited from Nominal

[], #coerce, #constrained?, #default?, #failure, #name, #optional?, #primitive?, #success, #to_proc, #try_coerce

Methods included from Printable

#to_s

Methods included from Builder

#&, #>, #constrained, #constrained_type, #constructor, #default, #enum, #fallback, #maybe, #optional, #|

Methods included from Meta

#meta, #pristine, #with

Methods included from Options

#with

Methods included from Type

#call, #valid?

Constructor Details

#initialize(primitive, **options) ⇒ Member

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Member.

Options Hash (**options):



19
20
21
22
# File 'lib/dry/types/array/member.rb', line 19

def initialize(primitive, **options)
  @member = options.fetch(:member)
  super
end

Instance Attribute Details

#memberType (readonly)



11
12
13
# File 'lib/dry/types/array/member.rb', line 11

def member
  @member
end

Instance Method Details

#call_safe(input) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dry/types/array/member.rb', line 45

def call_safe(input)
  if primitive?(input)
    failed = false

    result = input.each_with_object([]) do |el, output|
      coerced = member.call_safe(el) { |out = el|
        failed = true
        out
      }

      output << coerced unless Undefined.equal?(coerced)
    end

    failed ? yield(result) : result
  else
    yield
  end
end

#call_unsafe(input) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dry/types/array/member.rb', line 29

def call_unsafe(input)
  if primitive?(input)
    input.each_with_object([]) do |el, output|
      coerced = member.call_unsafe(el)

      output << coerced unless Undefined.equal?(coerced)
    end
  else
    super
  end
end

#constructor_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



116
# File 'lib/dry/types/array/member.rb', line 116

def constructor_type = ::Dry::Types::Array::Constructor

#laxLax

Build a lax type



100
101
102
# File 'lib/dry/types/array/member.rb', line 100

def lax
  Lax.new(Member.new(primitive, **options, member: member.lax, meta: meta))
end

#to_ast(meta: true) ⇒ Object

See Also:



107
108
109
110
111
112
113
# File 'lib/dry/types/array/member.rb', line 107

def to_ast(meta: true)
  if member.respond_to?(:to_ast)
    [:array, [member.to_ast(meta: meta), meta ? self.meta : EMPTY_HASH]]
  else
    [:array, [member, meta ? self.meta : EMPTY_HASH]]
  end
end

#try(input, &block) {|failure| ... } ⇒ Result, Logic::Result

Yield Parameters:

  • failure (Failure)

Yield Returns:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dry/types/array/member.rb', line 73

def try(input, &block) # rubocop:disable Metrics/PerceivedComplexity
  if primitive?(input)
    output = []

    result = input.map { |el| member.try(el) }
    result.each do |r|
      output << r.input unless Undefined.equal?(r.input)
    end

    if result.all?(&:success?)
      success(output)
    else
      error = result.find(&:failure?).error
      failure = failure(output, error)
      block ? yield(failure) : failure
    end
  else
    failure = failure(input, CoercionError.new("#{input} is not an array"))
    block ? yield(failure) : failure
  end
end