Class: Proc::Callable

Inherits:
Object
  • Object
show all
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



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/proc/callable.rb', line 79

def method_missing(name, input = input_omitted = true, **arguments)
  if IGNORE_MISSING.include?(name)
    super
  else
    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



39
40
41
42
43
44
# File 'lib/proc/callable.rb', line 39

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

#[](proc) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/proc/callable.rb', line 68

def [](proc)
  Callable.new(
    [@proc, proc].join("."),
    client: @client,
    input: @input,
    arguments: @arguments
  )
end

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



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

def call(input = input_omitted = true, **arguments)
  callable = self.class.new(
    @proc,
    client: @client,
    input: input_omitted ? @input : input,
    arguments: @arguments.merge(arguments)
  )

  @client.call(@proc, callable.input, **callable.arguments)
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)


92
93
94
95
96
97
98
# File 'lib/proc/callable.rb', line 92

def respond_to_missing?(name, *)
  if IGNORE_MISSING.include?(name)
    super
  else
    true
  end
end

#serializeObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/proc/callable.rb', line 46

def serialize
  serialized = ["()", @proc]

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

  serialized.concat(serialized_arguments)

  ["{}", serialized]
end

#serialized_argumentsObject



62
63
64
65
66
# File 'lib/proc/callable.rb', line 62

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

#serialized_inputObject



58
59
60
# File 'lib/proc/callable.rb', line 58

def serialized_input
  serialize_value(@input)
end

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



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

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