Module: Cooperator

Defined in:
lib/cooperator.rb,
lib/cooperator/context.rb,
lib/cooperator/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: Context

Constant Summary collapse

VERSION =
'0.3.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



118
119
120
# File 'lib/cooperator.rb', line 118

def self.prepended(base)
  base.extend ClassMethods
end

Instance Method Details

#commit(properties = {}) ⇒ Object



112
113
114
115
116
# File 'lib/cooperator.rb', line 112

def commit(properties = {})
  properties.each do |key, value|
    context.send :"#{key}=", value
  end
end

#contextObject



82
83
84
# File 'lib/cooperator.rb', line 82

def context
  @_context
end

#cooperate(*actions) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cooperator.rb', line 94

def cooperate(*actions)
  done = []

  actions.each do |action|
    action.perform context

    break if context.failure?

    done << action
  end

  if context.failure?
    done.reverse.each do |action|
      action.rollback context
    end
  end
end

#initialize(context = Context.new) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/cooperator.rb', line 86

def initialize(context = Context.new)
  @_context = if context.is_a? Context
                context
              else
                Context.new context
              end
end