class Proc::Composition

Attributes

arguments[R]
callables[R]
input[R]

Public Class Methods

new(client:, input:, callables: [], arguments: {}) click to toggle source
# File lib/proc/composition.rb, line 7
def initialize(client:, input:, callables: [], arguments: {})
  @client = client
  @input = input
  @callables = callables
  @arguments = arguments
end

Public Instance Methods

<<(callable) click to toggle source
# File lib/proc/composition.rb, line 79
def <<(callable)
  case callable
  when Composition
    merge(callable)
  when Callable
    @callables << callable
  end
end
>>(other) click to toggle source
public

Returns a composition built from this composition and another callable.

# File lib/proc/composition.rb, line 73
def >>(other)
  composed = dup
  composed << other
  composed
end
call(input = input_omitted = true, **arguments) { || ... } click to toggle source
public

Dispatches this composition to proc using the client.

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

  callable = self.class.new(
    client: @client,
    input: input_omitted ? @input : input,
    callables: @callables.dup,
    arguments: @arguments.merge(arguments)
  )

  @client.call("core.exec", Proc.undefined, proc: callable)
end
compose(*others) click to toggle source
public

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

# File lib/proc/composition.rb, line 65
def compose(*others)
  composed = dup
  others.each { |other| composed << other }
  composed
end
each(input = input_omitted = true, **arguments, &block) click to toggle source
public

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

# File lib/proc/composition.rb, line 37
def each(input = input_omitted = true, **arguments, &block)
  callable = self.class.new(
    client: @client,
    input: input_omitted ? @input : input,
    callables: @callables.dup,
    arguments: @arguments.merge(arguments)
  )

  @client.call("core.exec", Proc.undefined, proc: callable, &block)
end
initialize_copy(_) click to toggle source
# File lib/proc/composition.rb, line 14
def initialize_copy(_)
  @callables = @callables.dup
end
merge(composition) click to toggle source
# File lib/proc/composition.rb, line 108
def merge(composition)
  raise ArgumentError, "expected a composition" unless composition.is_a?(self.class)

  @callables.concat(composition.callables)
  @arguments.merge!(composition.arguments)
end
serialize() click to toggle source
# File lib/proc/composition.rb, line 88
def serialize
  serialized = ["{}"]

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

  serialized + serialized_arguments + @callables.map { |callable| callable.serialize(unwrapped: true) }
end
serialized_arguments() click to toggle source
# File lib/proc/composition.rb, line 102
def serialized_arguments
  @arguments.map { |key, value|
    ["$$", key.to_s, serialize_value(value)]
  }
end
serialized_input() click to toggle source
# File lib/proc/composition.rb, line 98
def serialized_input
  serialize_value(@input)
end
with(input = input_omitted = true, **arguments) { || ... } click to toggle source
public

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

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

  self.class.new(
    client: @client,
    input: input_omitted ? @input : input,
    callables: @callables.dup,
    arguments: @arguments.merge(arguments)
  )
end

Private Instance Methods

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