Class: Ector::Multi

Inherits:
Object
  • Object
show all
Defined in:
lib/ector-multi/multi.rb,
lib/ector-multi/errors.rb,
lib/ector-multi/result.rb,
lib/ector-multi/operations.rb

Defined Under Namespace

Modules: Operation Classes: ControlledFailure, OperationFailure, Result, UniqueOperationError

Constant Summary collapse

Error =

Parent class for all Multi errors

Class.new(StandardError)
Rollback =

Parent class for all “Rollback-able” errors

Class.new(Error)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMulti

Returns a new instance of Multi.



7
8
9
# File 'lib/ector-multi/multi.rb', line 7

def initialize
  @operations = []
end

Instance Attribute Details

#failureObject (readonly)

Returns the value of attribute failure.



5
6
7
# File 'lib/ector-multi/multi.rb', line 5

def failure
  @failure
end

#operationsObject (readonly)

Returns the value of attribute operations.



5
6
7
# File 'lib/ector-multi/multi.rb', line 5

def operations
  @operations
end

#resultsObject (readonly)

Returns the value of attribute results.



5
6
7
# File 'lib/ector-multi/multi.rb', line 5

def results
  @results
end

Instance Method Details

#append(multi) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ector-multi/multi.rb', line 11

def append(multi)
  check_operation_uniqueness!(multi.to_list)

  @operations.push *multi.operations

  self
end

#commitObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ector-multi/multi.rb', line 55

def commit
  results = {}

  fail_fast = operations.find(&:fail_fast?)
  fail_fast.run(results) if fail_fast

  ::ActiveRecord::Base.transaction(joinable: false, requires_new: true) do
    operations.each do |operation|
      ::ActiveRecord::Base.transaction(joinable: false, requires_new: true) do
        results[operation.name] = operation.run(::OpenStruct.new(results))
      end
    end
  end

  Ector::Multi::Result.new(results)
rescue Ector::Multi::Rollback => error
  Ector::Multi::Result.new(results, error)
end

#create(name, model, attributes = {}, &block) ⇒ Object



19
20
21
# File 'lib/ector-multi/multi.rb', line 19

def create(name, model, attributes = {}, &block)
  add_operation Operation::Create.new(name, model, operation_block(attributes, block))
end

#destroy(name, object = nil, &block) ⇒ Object



23
24
25
# File 'lib/ector-multi/multi.rb', line 23

def destroy(name, object = nil, &block)
  add_operation Operation::Destroy.new(name, operation_block(object, block))
end

#destroy_all(name, dataset = nil, &block) ⇒ Object



27
28
29
# File 'lib/ector-multi/multi.rb', line 27

def destroy_all(name, dataset = nil, &block)
  add_operation Operation::DestroyAll.new(name, operation_block(dataset, block))
end

#error(name, value) ⇒ Object



31
32
33
# File 'lib/ector-multi/multi.rb', line 31

def error(name, value)
  add_operation Operation::Error.new(name, operation_block(value, nil))
end

#prepend(multi) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/ector-multi/multi.rb', line 35

def prepend(multi)
  check_operation_uniqueness!(multi.to_list)

  @operations.prepend *multi.operations

  self
end

#run(name, &procedure) ⇒ Object



43
44
45
# File 'lib/ector-multi/multi.rb', line 43

def run(name, &procedure)
  add_operation Operation::Lambda.new(name, procedure)
end

#to_listObject



74
75
76
# File 'lib/ector-multi/multi.rb', line 74

def to_list
  operations.map(&:name)
end

#update(name, object, new_values, &block) ⇒ Object



47
48
49
# File 'lib/ector-multi/multi.rb', line 47

def update(name, object, new_values, &block)
  add_operation Operation::Update.new(name, object, operation_block(new_values, block))
end

#update_all(name, dataset, new_values, &block) ⇒ Object



51
52
53
# File 'lib/ector-multi/multi.rb', line 51

def update_all(name, dataset, new_values, &block)
  add_operation Operation::UpdateAll.new(name, dataset, operation_block(new_values, block))
end