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



51
52
53
# File 'lib/proc/composition.rb', line 51

def <<(callable)
  @callables << callable
end

#>>(other) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/proc/composition.rb', line 38

def >>(other)
  composed = dup

  case other
  when Composition
    composed.merge(other)
  when Callable
    composed << other
  end

  composed
end

#call(input = input_omitted = true, **arguments) ⇒ 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)
  callable = self.class.new(
    client: @client,
    input: input_omitted ? @input : input,
    callables: @callables.dup,
    arguments: @arguments.merge(arguments)
  )

  @client.call("proc.exec", nil, proc: callable.serialize)
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)


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

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

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

#serializeObject



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

def serialize
  {
    "{}" => {
      "<<" => serialized_input,
      "[]" => serialized_calls
    }.merge(serialized_arguments)
  }
end

#serialized_argumentsObject



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

def serialized_arguments
  @arguments.each_pair.each_with_object({}) { |(key, value), hash|
    hash[key.to_s] = serialize_value(value)
  }
end

#serialized_callsObject



64
65
66
67
68
# File 'lib/proc/composition.rb', line 64

def serialized_calls
  @callables.map { |callable|
    [callable.proc, callable.serialized_arguments]
  }
end

#serialized_inputObject



70
71
72
# File 'lib/proc/composition.rb', line 70

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