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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • primitive (Class)
  • options (Hash)

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)

Returns:



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

def member
  @member
end

#optionsHash (readonly) Originally defined in module Options

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:

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.

Parameters:

  • input (Object)

Returns:



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.

Parameters:

  • input (Object)

Returns:



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
117
118
# File 'lib/dry/types/array/member.rb', line 116

def constructor_type
  ::Dry::Types::Array::Constructor
end

#laxLax

Build a lax type

Returns:



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

Parameters:

Yield Parameters:

  • failure (Failure)

Yield Returns:

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