Class: Performify::Base

Inherits:
Object
  • Object
show all
Extended by:
Callbacks, Validation
Defined in:
lib/performify/base.rb

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.



14
15
16
17
18
19
20
21
# File 'lib/performify/base.rb', line 14

def initialize(current_user = nil, args = {})
  @current_user = current_user
  @args = args

  prepare_instance

  fail!(with_callbacks: true) if errors?
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



10
11
12
# File 'lib/performify/base.rb', line 10

def args
  @args
end

#current_userObject (readonly)

Returns the value of attribute current_user.



9
10
11
# File 'lib/performify/base.rb', line 9

def current_user
  @current_user
end

#exceptionObject (readonly)

Returns the value of attribute exception.



12
13
14
# File 'lib/performify/base.rb', line 12

def exception
  @exception
end

#inputsObject (readonly)

Returns the value of attribute inputs.



11
12
13
# File 'lib/performify/base.rb', line 11

def inputs
  @inputs
end

Instance Method Details

#execute!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/performify/base.rb', line 23

def execute!
  block_result = nil

  ActiveRecord::Base.transaction do
    begin
      block_result = yield

      if @result.nil?
        if block_result
          success!(with_callbacks: false)
        else
          fail!(with_callbacks: false)
        end
      end
    rescue RuntimeError, ActiveRecord::RecordInvalid => e
      fail!(exception: e, with_callbacks: false)
    end

    raise ActiveRecord::Rollback if fail?
  end

  execute_callbacks
  block_result
end

#execute_callbacksObject



48
49
50
51
52
53
54
# File 'lib/performify/base.rb', line 48

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, exception: nil) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/performify/base.rb', line 62

def fail!(with_callbacks: true, errors: nil, exception: nil)
  @result = false
  errors!(errors) unless errors.nil?
  execute_callbacks if with_callbacks
  @exception = exception unless exception.nil?
  @result
end

#fail?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/performify/base.rb', line 74

def fail?
  @result.blank?
end

#success!(with_callbacks: true) ⇒ Object



56
57
58
59
60
# File 'lib/performify/base.rb', line 56

def success!(with_callbacks: true)
  @result = true
  execute_callbacks if with_callbacks
  @result
end

#success?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/performify/base.rb', line 70

def success?
  @result.present?
end