class Proc::Callable

Constants

IGNORE_MISSING

Attributes

arguments[R]
input[R]
proc[R]

Public Class Methods

new(proc, client:, input: ::Proc.undefined, arguments: {}) click to toggle source
# File lib/proc/callable.rb, line 7
def initialize(proc, client:, input: ::Proc.undefined, arguments: {})
  @proc = proc.to_s
  @client = client
  @input = input
  @arguments = arguments
end

Public Instance Methods

>>(other) click to toggle source
public

Returns a composition built from this callable context and another callable.

# File lib/proc/callable.rb, line 77
def >>(other)
  composed = ::Proc::Composition.new(client: @client, input: @input)
  composed << self
  composed << other
  composed
end
[](proc) { || ... } click to toggle source
public

Returns a callable context for `proc`, nested within this callable context.

# File lib/proc/callable.rb, line 112
def [](proc)
  arguments = if ::Kernel.block_given?
    duped = @arguments.dup
    duped[:proc] = yield
    duped
  else
    @arguments
  end

  ::Proc::Callable.new(
    [@proc, proc].join("."),
    client: @client,
    input: @input,
    arguments: arguments
  )
end
call(input = input_omitted = true, **arguments) { || ... } click to toggle source
public

Dispatches this callable context to proc using the client.

If a block is passed, it will be called to prior to dispatch and its result passed as a nested context.

# File lib/proc/callable.rb, line 23
def call(input = input_omitted = true, **arguments)
  if ::Kernel.block_given?
    arguments[:proc] = yield
  end

  callable = ::Proc::Callable.new(
    @proc,
    client: @client,
    input: input_omitted ? @input : input,
    arguments: @arguments.merge(arguments)
  )

  @client.call(@proc, callable.input, **callable.arguments)
end
compose(*others) click to toggle source
public

Returns a composition built from this callable context and one or more other callables.

# File lib/proc/callable.rb, line 68
def compose(*others)
  composed = ::Proc::Composition.new(client: @client, input: @input)
  composed << self
  others.each { |other| composed << other }
  composed
end
each(input = input_omitted = true, **arguments, &block) click to toggle source
public

Dispatches this callable context to proc using the client, calling the given block once for each value.

# File lib/proc/callable.rb, line 40
def each(input = input_omitted = true, **arguments, &block)
  callable = ::Proc::Callable.new(
    @proc,
    client: @client,
    input: input_omitted ? @input : input,
    arguments: @arguments.merge(arguments)
  )

  @client.call(@proc, callable.input, **callable.arguments, &block)
end
initialize_copy(_) click to toggle source
# File lib/proc/callable.rb, line 14
def initialize_copy(_)
  @input = input.dup
  @arguments = arguments.dup
end
method_missing(name, input = input_omitted = true, **arguments) { || ... } click to toggle source
public

Allows nested callable contexts to be built through method lookups.

Calls superclass method
# File lib/proc/callable.rb, line 133
def method_missing(name, input = input_omitted = true, **arguments)
  if IGNORE_MISSING.include?(name)
    super
  else
    if ::Kernel.block_given?
      arguments[:proc] = yield
    end

    ::Proc::Callable.new(
      [@proc, name].join("."),
      client: @client,
      input: input_omitted ? @input : input,
      arguments: @arguments.merge(arguments)
    )
  end
end
respond_to_missing?(name, *) click to toggle source
Calls superclass method
# File lib/proc/callable.rb, line 150
def respond_to_missing?(name, *)
  if IGNORE_MISSING.include?(name)
    super
  else
    true
  end
end
serialize(unwrapped: false) click to toggle source
# File lib/proc/callable.rb, line 84
def serialize(unwrapped: false)
  serialized = ["()", @proc]

  unless ::Proc.undefined?(@input)
    serialized << [">>", serialized_input]
  end

  serialized.concat(serialized_arguments)

  if unwrapped
    serialized
  else
    ["{}", serialized]
  end
end
serialized_arguments() click to toggle source
# File lib/proc/callable.rb, line 104
def serialized_arguments
  @arguments.map { |key, value|
    ["$$", key.to_s, serialize_value(value)]
  }
end
serialized_input() click to toggle source
# File lib/proc/callable.rb, line 100
def serialized_input
  serialize_value(@input)
end
with(input = input_omitted = true, **arguments) { || ... } click to toggle source
public

Creates a new callable context based on this one, with a new input and/or arguments.

# File lib/proc/callable.rb, line 53
def with(input = input_omitted = true, **arguments)
  if ::Kernel.block_given?
    arguments[:proc] = yield
  end

  ::Proc::Callable.new(
    @proc,
    client: @client,
    input: input_omitted ? @input : input,
    arguments: @arguments.merge(arguments)
  )
end

Private Instance Methods

serialize_value(value) click to toggle source
# File lib/proc/callable.rb, line 158
        def serialize_value(value)
  case value
  when ::Symbol
    ["@@", value.to_s, {}]
  when ::Proc::Argument, ::Proc::Callable, ::Proc::Composition
    value.serialize
  else
    ["%%", value]
  end
end