Class: Sourced::CommandContext

Inherits:
Object
  • Object
show all
Defined in:
lib/sourced/command_context.rb

Overview

A command factory to instantiate commands from Hash attributes including extra metadata. # params should be a Hash with { type: String, payload: Hash | nil }

cmd = ctx.build(params[:command])
cmd.stream_id # String
cmd.[:user_id] # == session[:user_id]

Examples:


ctx = Sourced::CommandContext.new(
  stream_id: params[:stream_id],
  metadata: {
    user_id: session[:user_id]
  }
)

Instance Method Summary collapse

Constructor Details

#initialize(stream_id: nil, metadata: Plumb::BLANK_HASH, scope: Sourced::Command) ⇒ CommandContext

Returns a new instance of CommandContext.

Parameters:

  • stream_id (Hash) (defaults to: nil)

    a customizable set of options

  • metadata (Hash) (defaults to: Plumb::BLANK_HASH)

    a customizable set of options

  • scope (Hash) (defaults to: Sourced::Command)

    a customizable set of options

Options Hash (stream_id:):

  • (String)

Options Hash (metadata:):

  • metadata (Hash)

    to add to commands built by this context

Options Hash (scope:):



27
28
29
30
31
32
33
# File 'lib/sourced/command_context.rb', line 27

def initialize(stream_id: nil, metadata: Plumb::BLANK_HASH, scope: Sourced::Command)
  @defaults = {
    stream_id:,
    metadata:
  }.freeze
  @scope = scope
end

Instance Method Details

#build(attrs) ⇒ Sourced::Message

Parameters:

  • attrs (Hash)

    attributes to lookup and buils a scope from.

Returns:



37
38
39
40
# File 'lib/sourced/command_context.rb', line 37

def build(attrs)
  attrs = defaults.merge(Types::SymbolizedHash.parse(attrs))
  scope.from(attrs)
end