Class: Kobako::Payload::Arguments

Inherits:
Data
  • Object
show all
Defined in:
lib/kobako/payload/arguments.rb,
sig/kobako/payload/arguments.rbs

Overview

The invocation arguments a Call or a Run carries: a 2-element msgpack array, args then kwargs. Both elements are always present, so field positions stay stable when either is empty.

The positional-versus-keyword split lives here rather than in the core envelope because it is Ruby's call semantics, not the wire's. SPEC pins kwargs keys to Symbols; the invariant is enforced at construction so the value object is the single source of truth.

Built on the class X < Data.define(...) subclass form so the class body is fully Steep-visible; see .rubocop.yml for the rationale.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args: [], kwargs: {}) ⇒ Arguments

Returns a new instance of Arguments.

Parameters:

  • args: (Array[Kobako::Codec::wire_value]) (defaults to: [])
  • kwargs: (Hash[Symbol, Kobako::Codec::wire_value]) (defaults to: {})

Raises:

  • (ArgumentError)


20
21
22
23
24
25
# File 'lib/kobako/payload/arguments.rb', line 20

def initialize(args: [], kwargs: {})
  raise ArgumentError, "payload args must be Array" unless args.is_a?(Array)

  validate_kwargs!(kwargs)
  super
end

Instance Attribute Details

#argsArray[Kobako::Codec::wire_value] (readonly)

Returns the value of attribute args.

Returns:

  • (Array[Kobako::Codec::wire_value])


4
5
6
# File 'sig/kobako/payload/arguments.rbs', line 4

def args
  @args
end

#kwargsHash[Symbol, Kobako::Codec::wire_value] (readonly)

Returns the value of attribute kwargs.

Returns:

  • (Hash[Symbol, Kobako::Codec::wire_value])


5
6
7
# File 'sig/kobako/payload/arguments.rbs', line 5

def kwargs
  @kwargs
end

Class Method Details

.decode(bytes) ⇒ Arguments

Decode bytes into an Arguments. Raises Codec::InvalidTypeError when the payload is not the expected 2-element msgpack array, or when the construction invariants reject the decoded fields.

Parameters:

  • bytes (String)

Returns:



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kobako/payload/arguments.rb', line 36

def self.decode(bytes)
  Codec::Decoder.decode(bytes) do |frame|
    unless frame.is_a?(Array) && frame.length == 2
      raise Codec::InvalidTypeError,
            "an invocation payload is malformed (expected a 2-element array)"
    end

    args, kwargs = frame
    new(args: args, kwargs: kwargs)
  end
end

.newArguments

Parameters:

  • args: (Array[Kobako::Codec::wire_value])
  • kwargs: (Hash[Symbol, Kobako::Codec::wire_value])

Returns:



7
# File 'sig/kobako/payload/arguments.rbs', line 7

def self.new: (?args: Array[Kobako::Codec::wire_value], ?kwargs: Hash[Symbol, Kobako::Codec::wire_value]) -> Arguments

Instance Method Details

#==Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


17
# File 'sig/kobako/payload/arguments.rbs', line 17

def ==: (untyped other) -> bool

#encodeString

Encode to the [args, kwargs] msgpack bytes. The value object's own invariants are the contract; this does not re-check the shape.

Returns:

  • (String)


29
30
31
# File 'lib/kobako/payload/arguments.rb', line 29

def encode
  Codec::Encoder.encode([args, kwargs])
end

#hashInteger

Returns:

  • (Integer)


19
# File 'sig/kobako/payload/arguments.rbs', line 19

def hash: () -> Integer

#validate_kwargs!(kwargs) ⇒ void

This method returns an undefined value.

Parameters:

  • kwargs (Hash[untyped, untyped])

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
# File 'lib/kobako/payload/arguments.rb', line 50

def validate_kwargs!(kwargs)
  raise ArgumentError, "payload kwargs must be Hash" unless kwargs.is_a?(Hash)

  kwargs.each_key do |key|
    raise ArgumentError, "payload kwargs keys must be Symbol, got #{key.class}" unless key.is_a?(Symbol)
  end
end

#withArguments

Parameters:

  • args: (Array[Kobako::Codec::wire_value])
  • kwargs: (Hash[Symbol, Kobako::Codec::wire_value])

Returns:



15
# File 'sig/kobako/payload/arguments.rbs', line 15

def with: (?args: Array[Kobako::Codec::wire_value], ?kwargs: Hash[Symbol, Kobako::Codec::wire_value]) -> Arguments