module Cooperator::ClassMethods

Public Instance Methods

accepted() click to toggle source
# File lib/cooperator.rb, line 10
def accepted
  @_accepted ||= []
end
accepts(property, default: nil) click to toggle source
# File lib/cooperator.rb, line 30
def accepts(property, default: nil)
  define_method property do
    if context.include? property
      value = context[property]
      value.is_a?(Proc) ? value.call : value
    else
      nil
    end
  end

  accepted << property

  defaults[property] = default if default
end
commits(property) click to toggle source
# File lib/cooperator.rb, line 45
def commits(property)
  committed << property
end
committed() click to toggle source
# File lib/cooperator.rb, line 14
def committed
  @_committed ||= []
end
defaults() click to toggle source
# File lib/cooperator.rb, line 18
def defaults
  @_defaults ||= {}
end
expected() click to toggle source
# File lib/cooperator.rb, line 6
def expected
  @_expected ||= []
end
expects(property) click to toggle source
# File lib/cooperator.rb, line 22
def expects(property)
  define_method property do
    context[property]
  end

  expected << property
end
perform(context = {}) click to toggle source
# File lib/cooperator.rb, line 49
def perform(context = {})
  expected.each do |property|
    raise Exception, "missing expected property: #{property}" unless context.include? property
  end

  defaults.each do |property, value|
    context[property] = value unless context.include? property
  end

  action = new context

  catch :_finish do
    action.perform
  end

  unless action.context.failure?
    committed.each do |property|
      raise Exception, "missing committed property: #{property}" unless action.context.include? property
    end
  end

  action.context
end
rollback(context = {}) click to toggle source
# File lib/cooperator.rb, line 73
def rollback(context = {})
  action = new context

  action.rollback if action.respond_to? :rollback

  action.context
end