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.



167
168
169
# File 'lib/dry/types/constructor/function.rb', line 167

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.



165
166
167
# File 'lib/dry/types/constructor/function.rb', line 165

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)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/dry/types/constructor/function.rb', line 135

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)


152
153
154
155
156
157
158
159
160
161
# File 'lib/dry/types/constructor/function.rb', line 152

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:



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

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:



190
191
192
193
# File 'lib/dry/types/constructor/function.rb', line 190

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)


176
# File 'lib/dry/types/constructor/function.rb', line 176

def arity = 1

#call(input) ⇒ 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)


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

def call(input, &) = @fn.(input, &)

#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:



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

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)


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

def wrapper? = arity.equal?(2)