Class: Proc::Composition
- Inherits:
-
Object
- Object
- Proc::Composition
- Defined in:
- lib/proc/composition.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#callables ⇒ Object
readonly
Returns the value of attribute callables.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
Instance Method Summary collapse
- #<<(callable) ⇒ Object
- #>>(other) ⇒ Object
- #call(input = input_omitted = true, **arguments, &block) ⇒ Object
-
#initialize(client:, input:, callables: [], arguments: {}) ⇒ Composition
constructor
A new instance of Composition.
- #initialize_copy(_) ⇒ Object
- #merge(composition) ⇒ Object
- #serialize ⇒ Object
- #serialized_arguments ⇒ Object
- #serialized_input ⇒ Object
- #with(input = input_omitted = true, **arguments) ⇒ Object
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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
5 6 7 |
# File 'lib/proc/composition.rb', line 5 def arguments @arguments end |
#callables ⇒ Object (readonly)
Returns the value of attribute callables.
5 6 7 |
# File 'lib/proc/composition.rb', line 5 def callables @callables end |
#input ⇒ Object (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
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 |
#serialize ⇒ Object
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_arguments ⇒ Object
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_input ⇒ Object
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 |