Class: ActionAction::Base

Inherits:
Object
  • Object
show all
Extended by:
Callbacks
Includes:
Statuses, ActiveSupport::Callbacks
Defined in:
lib/action_action/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

after_perform, around_perform, before_perform

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
# File 'lib/action_action/base.rb', line 11

def initialize(params = {})
  @params = params
  @params.map do |parameter, value|
    instance_variable_set(:"@#{parameter}", value)
  end
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Class Method Details

.perform(*args) ⇒ Object



33
34
35
# File 'lib/action_action/base.rb', line 33

def perform(*args)
  new.perform_with_callbacks(*args)
end

.perform!(*args) ⇒ Object



37
38
39
40
41
42
# File 'lib/action_action/base.rb', line 37

def perform!(*args)
  if (context = perform(*args)).success?
    return context
  end
  raise ActionAction::Error.new(context.message)
end

.require(params = {}) ⇒ Object Also known as: set!, with!, require!



21
22
23
# File 'lib/action_action/base.rb', line 21

def require(params = {})
  Parameters.new(self).require(params)
end

.set(params = {}) ⇒ Object Also known as: with



28
29
30
# File 'lib/action_action/base.rb', line 28

def set(params = {})
  Parameters.new(self, params).set(params)
end

Instance Method Details

#perform(*args) ⇒ Object



45
46
47
# File 'lib/action_action/base.rb', line 45

def perform(*args)
  perform_with_callbacks(*args)
end

#perform_with_callbacks(*args) ⇒ Object



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

def perform_with_callbacks(*args)
  run_callbacks :perform do
    perform_without_callbacks(*args)
  end
  self
end

#perform_without_callbacks(*args) ⇒ Object



49
50
51
52
53
# File 'lib/action_action/base.rb', line 49

def perform_without_callbacks(*args)
  @result = perform(*args)
  success! if @status.nil?
  @result
end