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

Returns a new instance of 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



44
45
46
47
48
49
50
51
# File 'lib/proc/composition.rb', line 44

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

#>>(other) ⇒ Object



38
39
40
41
42
# File 'lib/proc/composition.rb', line 38

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

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



18
19
20
21
22
23
24
25
26
27
# File 'lib/proc/composition.rb', line 18

def call(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("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)


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

def merge(composition)
  raise ArgumentError, "expected a composition" unless composition.is_a?(self.class)

  @callables.concat(composition.callables)
  @arguments.merge!(composition.arguments)
end

#serializeObject



53
54
55
56
57
58
59
60
61
# File 'lib/proc/composition.rb', line 53

def serialize
  serialized = ["{}"]

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

  serialized + serialized_arguments + @callables.map { |callable| callable.serialize(unwrapped: true) }
end

#serialized_argumentsObject



67
68
69
70
71
# File 'lib/proc/composition.rb', line 67

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

#serialized_inputObject



63
64
65
# File 'lib/proc/composition.rb', line 63

def serialized_input
  serialize_value(@input)
end

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



29
30
31
32
33
34
35
36
# File 'lib/proc/composition.rb', line 29

def with(input = input_omitted = true, **arguments)
  self.class.new(
    client: @client,
    input: input_omitted ? @input : input,
    callables: @callables.dup,
    arguments: @arguments.merge(arguments)
  )
end