Class: Yes::Core::Commands::Stateless::GroupHandler

Inherits:
Object
  • Object
show all
Includes:
HandlerHelpers
Defined in:
lib/yes/core/commands/stateless/group_handler.rb

Overview

Handles a group of commands

Defined Under Namespace

Classes: CommandsError, CustomHandlerMethodMissingError, InvalidCommandGroupError

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HandlerHelpers

#activated?, #added?, #deactivated?, #disabled?, #enabled?, #event_exists_with_payload?, #event_in_stream?, #item_added?, #item_assigned?, #item_removed?, #item_unassigned?, #no_change?, #partial_payload_field_changed?, #published?, #removed?, #skip?, #unpublished?

Constructor Details

#initialize(cmd, events_cache: {}) ⇒ GroupHandler

Returns a new instance of GroupHandler.

Parameters:

  • cmd (Group)

    the command group to handle

  • events_cache (Hash) (defaults to: {})

    already cached events { stream => { event_name => event_data } }

Raises:



60
61
62
63
64
65
# File 'lib/yes/core/commands/stateless/group_handler.rb', line 60

def initialize(cmd, events_cache: {})
  raise InvalidCommandGroupError.new(cmd.class.name.deconstantize, self.class.name.deconstantize) unless valid_command?(cmd)

  @cmd = cmd
  @events_cache = events_cache
end

Class Attribute Details

.handlersArray<Symbol, Class> (readonly)

Returns List of handlers for the command group.

Returns:

  • (Array<Symbol, Class>)

    List of handlers for the command group



22
23
24
# File 'lib/yes/core/commands/stateless/group_handler.rb', line 22

def handlers
  @handlers
end

Class Method Details

.handler(command_or_handler_method_name, context: to_s.split('::')[0], subject: to_s.split('::')[1]) ⇒ void

This method returns an undefined value.

Adds a handler to the command group

Parameters:

  • command_or_handler_method_name (Symbol, String)

    the name of the command class (String) or custom handler method (Symbol)

  • context (Symbol, String) (defaults to: to_s.split('::')[0])

    the context of the handler’s command, camel or snake case

  • subject (Symbol, String) (defaults to: to_s.split('::')[1])

    the subject of the handler’s command, camel or snake case



34
35
36
37
38
# File 'lib/yes/core/commands/stateless/group_handler.rb', line 34

def handler(command_or_handler_method_name, context: to_s.split('::')[0],
            subject: to_s.split('::')[1])
  @handlers ||= []
  @handlers << build_handler(command_or_handler_method_name, context, subject)
end

.stateless?Boolean

Returns Always returns true for stateless handlers.

Returns:

  • (Boolean)

    Always returns true for stateless handlers



25
26
27
# File 'lib/yes/core/commands/stateless/group_handler.rb', line 25

def stateless?
  true
end

Instance Method Details

#callvoid

This method returns an undefined value.

Executes the command group

Raises:

  • (CommandsError)

    if any handler errors occur during execution



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yes/core/commands/stateless/group_handler.rb', line 70

def call
  errors = []

  PgEventstore.client.multiple do
    errors.push(*run_command_handlers)
    errors.push(*run_custom_handlers)
    raise CommandsError.new(extra: errors), 'Command group failed' if errors.any?

    publish_events
  end
end