class Proc::Composer::Callable

Constants

IGNORE_MISSING
KERNEL_DELEGATE

Attributes

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

Public Class Methods

new(proc, input: ::Proc::Composer.undefined, arguments: {}) click to toggle source
# File lib/proc/composer/callable.rb, line 13
def initialize(proc, input: ::Proc::Composer.undefined, arguments: {})
  @proc = proc.to_s
  @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/composer/callable.rb, line 45
def >>(other)
  composed = build_composition(input: @input)
  composed << self
  composed << other
  composed
end
Also aliased as: |
[](proc) { || ... } click to toggle source
public

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

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

  build_callable(proc: [@proc, proc].join("."), input: @input, arguments: 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/composer/callable.rb, line 36
def compose(*others)
  composed = build_composition(input: @input)
  composed << self
  others.each { |other| composed << other }
  composed
end
initialize_copy(_) click to toggle source
# File lib/proc/composer/callable.rb, line 19
def initialize_copy(_)
  @input = input.dup
  @arguments = arguments.dup
end
method_missing(name, input = input_omitted = true, *parameters, **arguments) { || ... } click to toggle source
public

Allows nested callable contexts to be built through method lookups.

Calls superclass method
# File lib/proc/composer/callable.rb, line 109
def method_missing(name, input = input_omitted = true, *parameters, **arguments, &block)
  if IGNORE_MISSING.include?(name)
    super
  elsif KERNEL_DELEGATE.include?(name)
    if input_omitted
      ::Kernel.instance_method(name).bind_call(self, *parameters, **arguments, &block)
    else
      ::Kernel.instance_method(name).bind_call(self, input, *parameters, **arguments, &block)
    end
  else
    if block
      arguments[:proc] = yield
    end

    build_callable(
      proc: [@proc, name].join("."),
      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/composer/callable.rb, line 131
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/composer/callable.rb, line 53
def serialize(unwrapped: false)
  serialized = ["()", @proc]

  unless ::Proc::Composer.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/composer/callable.rb, line 73
def serialized_arguments
  @arguments.map { |key, value|
    ["$$", key.to_s, serialize_value(value)]
  }
end
serialized_input() click to toggle source
# File lib/proc/composer/callable.rb, line 69
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/composer/callable.rb, line 26
def with(input = input_omitted = true, **arguments)
  if ::Kernel.block_given?
    arguments[:proc] = yield
  end

  build_callable(input: input_omitted ? @input : input, arguments: @arguments.merge(arguments))
end
|(other)
Alias for: >>

Private Instance Methods

build_callable(input:, arguments:, proc: @proc) click to toggle source
# File lib/proc/composer/callable.rb, line 152
        def build_callable(input:, arguments:, proc: @proc)
  ::Proc::Composer::Callable.new(proc, input: input, arguments: arguments)
end
build_composition(input:) click to toggle source
# File lib/proc/composer/callable.rb, line 156
        def build_composition(input:)
  ::Proc::Composer::Composition.new(input: input)
end
serialize_value(value) click to toggle source
# File lib/proc/composer/callable.rb, line 139
        def serialize_value(value)
  case value
  when ::Symbol
    ["@@", value.to_s, {}]
  else
    if value.respond_to?(:serialize)
      value.serialize
    else
      ["%%", value]
    end
  end
end