Class: FourthDimensional::CommandHandler
- Inherits:
-
Object
- Object
- FourthDimensional::CommandHandler
- Includes:
- Eventable
- Defined in:
- lib/fourth_dimensional/command_handler.rb
Overview
FourthDimensional::CommandHandler
Command handlers have bindings that wrap a Command to load an aggregate and apply events.
class PostCommandHandler < FourthDimensional::CommandHandler
on AddPost do |command|
with_aggregate(PostAggregate, command) do |post|
post.add(title: command.title, body: command.body)
end
end
# manually load and save aggregate
on UpdateTitle do |command|
post = repository.load_aggregate(PostAggregate, command.aggregate_id)
post.update_title(title: command.title)
save(command, post)
end
on PublishPost do |command|
with_aggregate(PostAggregate, command) do |post|
post.publish
end
end
end
Defined Under Namespace
Classes: CommandAndEvents
Instance Attribute Summary collapse
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
Instance Method Summary collapse
-
#call(command) ⇒ Object
Invokes a callback for an command.
-
#initialize(repository:) ⇒ CommandHandler
constructor
A new instance of CommandHandler.
-
#save(command, aggregate) ⇒ Object
Saves the command and aggregate’s applied events.
-
#with_aggregate(aggregate_class, command) {|aggregate| ... } ⇒ Object
Yields the aggregate and saves the applied events.
Methods included from Eventable
Constructor Details
#initialize(repository:) ⇒ CommandHandler
Returns a new instance of CommandHandler.
45 46 47 |
# File 'lib/fourth_dimensional/command_handler.rb', line 45 def initialize(repository:) @repository = repository end |
Instance Attribute Details
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
30 31 32 |
# File 'lib/fourth_dimensional/command_handler.rb', line 30 def repository @repository end |
Instance Method Details
#call(command) ⇒ Object
Invokes a callback for an command.
50 51 52 53 54 |
# File 'lib/fourth_dimensional/command_handler.rb', line 50 def call(command) callback = self.class.event_bindings[command.class] return if callback.nil? instance_exec(command, &callback) end |
#save(command, aggregate) ⇒ Object
Saves the command and aggregate’s applied events
class PostCommandHandler < FourthDimensional::CommandHandler
on AddPost do |command|
post = repository.load_aggregate(PostAggregate, command.aggregate_id)
post.add(title: command.title)
save(command, post)
end
end
72 73 74 75 76 77 |
# File 'lib/fourth_dimensional/command_handler.rb', line 72 def save(command, aggregate) repository.save_command_and_events(CommandAndEvents.new( command: command, events: aggregate.applied_events )) end |
#with_aggregate(aggregate_class, command) {|aggregate| ... } ⇒ Object
Yields the aggregate and saves the applied events
57 58 59 60 61 |
# File 'lib/fourth_dimensional/command_handler.rb', line 57 def with_aggregate(aggregate_class, command, &block) aggregate = repository.load_aggregate(aggregate_class, command.aggregate_id) yield aggregate save(command, aggregate) end |