Class: Performify::Base
Constant Summary
Constants included
from Callbacks
Callbacks::TYPES_OF_CALLBACK
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Callbacks
clean_callbacks, register_callback
Methods included from Validation
extended
Constructor Details
#initialize(current_user = nil, **args) ⇒ Base
Returns a new instance of Base.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/performify/base.rb', line 11
def initialize(current_user = nil, **args)
@current_user = current_user
define_singleton_method(:execute!) do |&block|
return if defined?(@result)
super(&block)
end
return if args.empty?
validate(args)
define_singleton_methods(args)
fail!(with_callbacks: true) if errors?
end
|
Instance Attribute Details
#current_user ⇒ Object
Returns the value of attribute current_user.
9
10
11
|
# File 'lib/performify/base.rb', line 9
def current_user
@current_user
end
|
Instance Method Details
#execute! ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/performify/base.rb', line 27
def execute!
block_result = nil
ActiveRecord::Base.transaction do
begin
block_result = yield
if block_result
success!(with_callbacks: false)
else
fail!(with_callbacks: false)
end
rescue RuntimeError, ActiveRecord::RecordInvalid
fail!
end
raise ActiveRecord::Rollback if fail?
end
execute_callbacks
block_result
end
|
#execute_callbacks ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/performify/base.rb', line 49
def execute_callbacks
if success?
self.class.execute_callbacks(:success, self)
elsif fail?
self.class.execute_callbacks(:fail, self)
end
end
|
#fail!(with_callbacks: true, errors: nil) ⇒ Object
63
64
65
66
67
68
|
# File 'lib/performify/base.rb', line 63
def fail!(with_callbacks: true, errors: nil)
@result = false
errors!(errors) unless errors.nil?
execute_callbacks if with_callbacks
@result
end
|
#fail? ⇒ Boolean
74
75
76
|
# File 'lib/performify/base.rb', line 74
def fail?
@result.blank?
end
|
#success!(with_callbacks: true) ⇒ Object
57
58
59
60
61
|
# File 'lib/performify/base.rb', line 57
def success!(with_callbacks: true)
@result = true
execute_callbacks if with_callbacks
@result
end
|
#success? ⇒ Boolean
70
71
72
|
# File 'lib/performify/base.rb', line 70
def success?
@result.present?
end
|