Class: Proc::Callable

Inherits:
BasicObject
Defined in:
lib/proc/callable.rb

Constant Summary collapse

IGNORE_MISSING =
i[to_hash].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proc, client:, input: ::Proc.undefined, arguments: {}) ⇒ Callable

Returns a new instance of Callable.



7
8
9
10
11
12
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, input = input_omitted = true, **arguments) ⇒ Object

public

Allows nested callable contexts to be built through method lookups.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



5
6
7
# File 'lib/proc/callable.rb', line 5

def arguments
  @arguments
end

#inputObject (readonly)

Returns the value of attribute input.



5
6
7
# File 'lib/proc/callable.rb', line 5

def input
  @input
end

#procObject (readonly)

Returns the value of attribute proc.



5
6
7
# File 'lib/proc/callable.rb', line 5

def proc
  @proc
end

Instance Method Details

#>>(other) ⇒ Object

public

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



77
78
79
80
81
82
# File 'lib/proc/callable.rb', line 77

def >>(other)
  composed = ::Proc::Composition.new(client: @client, input: @input)
  composed << self
  composed << other
  composed
end

#[](proc) ⇒ Object

public

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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) ⇒ Object

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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object

public

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



68
69
70
71
72
73
# 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) ⇒ Object

public

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



40
41
42
43
44
45
46
47
48
49
# 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(_) ⇒ Object



14
15
16
17
# File 'lib/proc/callable.rb', line 14

def initialize_copy(_)
  @input = input.dup
  @arguments = arguments.dup
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
# 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) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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_argumentsObject



104
105
106
107
108
# File 'lib/proc/callable.rb', line 104

def serialized_arguments
  @arguments.map { |key, value|
    ["$$", key.to_s, serialize_value(value)]
  }
end

#serialized_inputObject



100
101
102
# File 'lib/proc/callable.rb', line 100

def serialized_input
  serialize_value(@input)
end

#with(input = input_omitted = true, **arguments) ⇒ Object

public

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



53
54
55
56
57
58
59
60
61
62
63
64
# 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