Class: Dry::Types::Constructor::Function Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/types/constructor/function.rb

Overview

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

Function is used internally by Constructor types

Direct Known Subclasses

MethodCall, Safe, Wrapper

Defined Under Namespace

Classes: MethodCall, Safe, Wrapper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn) ⇒ Function

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 Function.



173
174
175
# File 'lib/dry/types/constructor/function.rb', line 173

def initialize(fn)
  @fn = fn
end

Instance Attribute Details

#fnObject (readonly)

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.



171
172
173
# File 'lib/dry/types/constructor/function.rb', line 171

def fn
  @fn
end

Class Method Details

.[](fn) ⇒ Function

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.

Choose or build specialized invokation code for a callable

Parameters:

Returns:

Raises:

  • (::ArgumentError)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dry/types/constructor/function.rb', line 141

def self.[](fn)
  raise ::ArgumentError, "Missing constructor block" if fn.nil?

  if fn.is_a?(Function)
    fn
  elsif fn.respond_to?(:arity) && fn.arity.equal?(2)
    Wrapper.new(fn)
  elsif fn.is_a?(::Method)
    MethodCall[fn, yields_block?(fn)]
  elsif yields_block?(fn)
    new(fn)
  else
    Safe.new(fn)
  end
end

.yields_block?(fn) ⇒ Boolean

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:

  • (Boolean)


158
159
160
161
162
163
164
165
166
167
# File 'lib/dry/types/constructor/function.rb', line 158

def self.yields_block?(fn)
  *, (last_arg,) =
    if fn.respond_to?(:parameters)
      fn.parameters
    else
      fn.method(:call).parameters
    end

  last_arg.equal?(:block)
end

Instance Method Details

#<<(other) ⇒ Function

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:



208
209
210
211
# File 'lib/dry/types/constructor/function.rb', line 208

def <<(other)
  f = Function[other]
  Function[-> x, &b { self.(f.(x, &b), &b) }]
end

#>>(other) ⇒ Function

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:



202
203
204
205
# File 'lib/dry/types/constructor/function.rb', line 202

def >>(other)
  f = Function[other]
  Function[-> x, &b { f.(self.(x, &b), &b) }]
end

#arityInteger

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:

  • (Integer)


184
185
186
# File 'lib/dry/types/constructor/function.rb', line 184

def arity
  1
end

#call(input, &block) ⇒ Object Also known as: []

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:

  • (Object)


178
179
180
# File 'lib/dry/types/constructor/function.rb', line 178

def call(input, &block)
  @fn.(input, &block)
end

#to_astArray

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:



193
194
195
196
197
198
199
# File 'lib/dry/types/constructor/function.rb', line 193

def to_ast
  if fn.is_a?(::Proc)
    [:id, FnContainer.register(fn)]
  else
    [:callable, fn]
  end
end

#wrapper?Boolean

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:

  • (Boolean)


188
189
190
# File 'lib/dry/types/constructor/function.rb', line 188

def wrapper?
  arity.equal?(2)
end