Class: Ector::Multi
- Inherits:
-
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
-
#append(multi) ⇒ Object
-
#commit ⇒ Object
-
#create(name, model, attributes = {}, &block) ⇒ Object
-
#destroy(name, object = nil, &block) ⇒ Object
-
#destroy_all(name, dataset = nil, &block) ⇒ Object
-
#error(name, value) ⇒ Object
-
#initialize ⇒ Multi
constructor
-
#prepend(multi) ⇒ Object
-
#run(name, &procedure) ⇒ Object
-
#to_list ⇒ Object
-
#update(name, object, new_values, &block) ⇒ Object
-
#update_all(name, dataset, new_values, &block) ⇒ Object
Constructor Details
#initialize ⇒ Multi
Returns a new instance of Multi.
7
8
9
|
# File 'lib/ector-multi/multi.rb', line 7
def initialize
@operations = []
end
|
Instance Attribute Details
#failure ⇒ Object
Returns the value of attribute failure.
5
6
7
|
# File 'lib/ector-multi/multi.rb', line 5
def failure
@failure
end
|
#operations ⇒ Object
Returns the value of attribute operations.
5
6
7
|
# File 'lib/ector-multi/multi.rb', line 5
def operations
@operations
end
|
#results ⇒ Object
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
|
#commit ⇒ Object
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_list ⇒ Object
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
|