Class: Proc::Composition

Inherits:
Object
  • Object
show all
Defined in:
lib/proc/composition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, input:, callables: [], arguments: {}) ⇒ Composition



7
8
9
10
11
12
# File 'lib/proc/composition.rb', line 7

def initialize(client:, input:, callables: [], arguments: {})
  @client = client
  @input = input
  @callables = callables
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

#callablesObject (readonly)

Returns the value of attribute callables.



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

def callables
  @callables
end

#inputObject (readonly)

Returns the value of attribute input.



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

def input
  @input
end

Instance Method Details

#<<(callable) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/proc/composition.rb', line 79

def <<(callable)
  case callable
  when Composition
    merge(callable)
  when Callable
    @callables << callable
  end
end

#>>(other) ⇒ Object

public

Returns a composition built from this composition and another callable.



73
74
75
76
77
# File 'lib/proc/composition.rb', line 73

def >>(other)
  composed = dup
  composed << other
  composed
end

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

public

Dispatches this composition to proc using the client.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object

public

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



65
66
67
68
69
# 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) ⇒ Object

public

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



37
38
39
40
41
42
43
44
45
46
# 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(_) ⇒ Object



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

def initialize_copy(_)
  @callables = @callables.dup
end

#merge(composition) ⇒ Object

Raises:

  • (ArgumentError)


108
109
110
111
112
113
# 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

#serializeObject



88
89
90
91
92
93
94
95
96
# 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_argumentsObject



102
103
104
105
106
# File 'lib/proc/composition.rb', line 102

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

#serialized_inputObject



98
99
100
# File 'lib/proc/composition.rb', line 98

def serialized_input
  serialize_value(@input)
end

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

public

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



50
51
52
53
54
55
56
57
58
59
60
61
# 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