module Operate::Command

A command-pattern implementation for controller actions, etc.

`register` handlers with on(). `broadcast` results with broadcast(). `transaction` wraps ActiveRecord transactions. `expose` to set a value from the handler block to the caller

Public Class Methods

included(target) click to toggle source
# File lib/operate/command.rb, line 16
def self.included(target)
  target.extend ClassMethods
end

Public Instance Methods

evaluate(&block) click to toggle source
# File lib/operate/command.rb, line 46
def evaluate(&block)
  @caller = eval('self', block.binding)
  instance_eval(&block)
end
expose(presentation_data) click to toggle source

Expose a value within a handler block to the caller. Sets attribute directly if available, or as an instance variable.

RegisterAccount.call(@form) do

on(:ok) { |user| expose(:user => user) }

end

# File lib/operate/command.rb, line 71
def expose(presentation_data)
  presentation_data.each do |attribute, value|
    if @caller.respond_to?("#{attribute}=")
      @caller.public_send("#{attribute}=", value)
    else
      @caller.instance_variable_set("@#{attribute}", value)
    end
  end
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/operate/command.rb, line 51
def method_missing(method_name, *args, &block)
  if @caller.respond_to?(method_name, true)
    @caller.send(method_name, *args, &block)
  else
    super
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
# File lib/operate/command.rb, line 59
def respond_to_missing?(method_name, include_private = false)
  @caller.respond_to?(method_name, include_private)
end
transaction(&block) click to toggle source
# File lib/operate/command.rb, line 36
def transaction(&block)
  return unless block_given?

  if defined?(ActiveRecord)
    ::ActiveRecord::Base.transaction(&block)
  else
    raise Error, 'Transactions are supported only with ActiveRecord'
  end
end