Class: SolidObjects::OperationDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_objects/operation_dispatcher.rb,
sig/generated/lib/solid_objects/operation_dispatcher.rbs

Constant Summary collapse

RESERVED_REFERENCE_METHOD_NAMES =

Returns:

  • (Object)
%i[actor_type actor_id async sync destroy snapshot].freeze
RESERVED_OPERATION_NAMES =

Returns:

  • (Object)
(
  public_instance_methods(true) +
  RESERVED_REFERENCE_METHOD_NAMES
).map(&:to_sym).uniq.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor_type:, handlers:) {|arg0, arg1| ... } ⇒ OperationDispatcher

Returns a new instance of OperationDispatcher.

RBS:

  • (actor_type: String, handlers: Hash[Symbol, ActorDefinition::Handler]) { (Symbol, Hash[Symbol, untyped]) -> untyped } -> void

Parameters:

Yields:

Yield Parameters:

  • arg0 (Symbol)
  • arg1 (Hash[Symbol, untyped])

Yield Returns:

  • (Object)


10
11
12
13
14
# File 'lib/solid_objects/operation_dispatcher.rb', line 10

def initialize(actor_type:, handlers:, &dispatch)
  @actor_type = actor_type
  @handlers = handlers.dup.freeze
  @dispatch = dispatch
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, **keywords, &block) ⇒ Object

RBS:

  • (Symbol, *untyped, **untyped) -> untyped

Parameters:

  • (Symbol)
  • (Object)
  • (Object)

Returns:

  • (Object)


17
18
19
20
21
22
23
24
# File 'lib/solid_objects/operation_dispatcher.rb', line 17

def method_missing(name, *arguments, **keywords, &block)
  raise UnknownMessage, "unknown message #{name.inspect} for #{actor_type}" unless handlers.key?(name)
  if arguments.any? || block
    raise ArgumentError, "actor operations accept keyword arguments only"
  end

  dispatch.call(name, keywords)
end

Instance Attribute Details

#actor_typeObject (readonly)

Returns the value of attribute actor_type.

Returns:

  • (Object)


33
34
35
# File 'lib/solid_objects/operation_dispatcher.rb', line 33

def actor_type
  @actor_type
end

#dispatchObject (readonly)

Returns the value of attribute dispatch.

Returns:

  • (Object)


33
34
35
# File 'lib/solid_objects/operation_dispatcher.rb', line 33

def dispatch
  @dispatch
end

#handlersObject (readonly)

Returns the value of attribute handlers.

Returns:

  • (Object)


33
34
35
# File 'lib/solid_objects/operation_dispatcher.rb', line 33

def handlers
  @handlers
end

Class Method Details

.validate_operation_name!(name) ⇒ void

This method returns an undefined value.

RBS:

  • (Symbol | String) -> void

Parameters:

  • (Symbol, String)


43
44
45
46
47
48
# File 'lib/solid_objects/operation_dispatcher.rb', line 43

def validate_operation_name!(name)
  operation_name = name.to_sym
  return unless RESERVED_OPERATION_NAMES.include?(operation_name)

  raise InvalidActor, "#{operation_name.inspect} conflicts with actor dispatch"
end

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

RBS:

  • (Symbol, bool) -> bool

Parameters:

  • (Symbol)
  • (Boolean)

Returns:

  • (Boolean)


27
28
29
# File 'lib/solid_objects/operation_dispatcher.rb', line 27

def respond_to_missing?(name, include_private = false)
  handlers.key?(name) || super
end