Method: CommandModel::Model.execute
- Defined in:
- lib/command_model/model.rb
.execute(attributes_or_command, dependencies = {}, &block) ⇒ Object
Executes a block of code if the command model is valid.
Accepts either a command model or a hash of attributes with which to create a new command model.
Examples
RenameUserCommand.execute(login: "john") do |command|
if allowed_to_rename_user?
self.login = command.login
else
command.errors.add :base, "not allowed to rename"
end
end
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/command_model/model.rb', line 137 def self.execute(attributes_or_command, dependencies={}, &block) command = if attributes_or_command.kind_of? self raise ArgumentError, "cannot pass dependencies with already initialized command" if dependencies.present? attributes_or_command else new(attributes_or_command, dependencies) end command.call &block end |