Class: Yes::Core::Aggregate::Dsl::MethodDefiners::Command::CanCommand

Inherits:
Base
  • Object
show all
Defined in:
lib/yes/core/aggregate/dsl/method_definers/command/can_command.rb

Overview

Defines a can_<command_name>? method on the aggregate class

Since:

  • 0.1.0

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Yes::Core::Aggregate::Dsl::MethodDefiners::Command::Base

Instance Method Details

#callvoid

This method returns an undefined value.

Since:

  • 0.1.0



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yes/core/aggregate/dsl/method_definers/command/can_command.rb', line 12

def call
  can_change_method = :"can_#{name}?"
  error_method = :"#{name}_error"

  aggregate_class.attr_accessor error_method
  command_name = @name

  aggregate_class.define_method(can_change_method) do |payload = {}|
    payload = command_utilities.prepare_default_payload(command_name, payload, self.class)
    payload = command_utilities.prepare_command_payload(command_name, payload, self.class)
    payload = command_utilities.prepare_assign_command_payload(command_name, payload)
    cmd = command_utilities.build_command(command_name, payload)
    guard_evaluator_class = command_utilities.fetch_guard_evaluator_class(command_name)

    Yes::Core::CommandHandling::GuardRunner.new(self).call(
      cmd, command_name, guard_evaluator_class, skip_guards: false
    ).present?
  rescue Yes::Core::CommandHandling::GuardEvaluator::InvalidTransition,
         Yes::Core::CommandHandling::GuardEvaluator::NoChangeTransition,
         Yes::Core::Command::Invalid
    false
  end
end