Class: NexusCqrs::BaseMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_cqrs/base_message.rb

Overview

All messages passed through the message bus will extend this class. Commands and Queries are both extended types of ‘BaseMessage`. This is mainly used to store metadata for Commands/Queries (such as the user context), as well as helper methods for retrieving the policy method for this Command/Query/Message

Since:

  • 0.1.0

Direct Known Subclasses

BaseCommand, BaseQuery

Instance Method Summary collapse

Instance Method Details

#demodularised_class_nameString

Helper method for retrieving the demodularised name of the class used to define the auth policy for this message

Examples:

Get the demodularised_class_name on a ‘Commands::Mods::DeleteMod` command

command.policy_class #=> "DeleteMod"

Returns:

  • (String)

    Name of the policy class

Since:

  • 0.1.0



50
51
52
# File 'lib/nexus_cqrs/base_message.rb', line 50

def demodularised_class_name
  self.class.name.split('::').last
end

#metadataHash

Getter for retrieving the metadata on this message

Returns:

  • (Hash)

    Any metadata attached to this message

Since:

  • 0.1.0



25
26
27
# File 'lib/nexus_cqrs/base_message.rb', line 25

def 
  @metadata || {}
end

#policy_classString

Deprecated.

This used to be used to authorise policies before they hit the command bus - requiring a new policy for every message. E.g. ‘DeleteModPolicy`, `UpdateModPolicy`, `CreateModPolicy` etc. It is now recommended to simple call `authorise` within the handler as it is far more flexible

Helper method for retrieving the name of the class used to define the auth policy for this message

Examples:

Get the policy_class on a ‘Commands::Mods::DeleteMod` command

command.policy_class #=> "DeleteModPolicy"

Returns:

  • (String)

    Name of the policy class

Since:

  • 0.1.0



39
40
41
# File 'lib/nexus_cqrs/base_message.rb', line 39

def policy_class
  demodularised_class_name + 'Policy'
end

#set_metadata(key, value) ⇒ Object

Sets metadata on this message.

Examples:

Set the current user on a command/query

command.(:current_user, user)

Parameters:

  • key (Symbol)

    Metadata key

  • value (Object)

    Metadata value

Since:

  • 0.1.0



16
17
18
19
# File 'lib/nexus_cqrs/base_message.rb', line 16

def (key, value)
  @metadata = {} unless @metadata
  @metadata[key.to_sym] = value
end