Class: ActionMan::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/action_man/base.rb

Constant Summary collapse

RESULT_TYPES =
%i[success failure].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Base

Returns a new instance of Base.



80
81
82
83
# File 'lib/action_man/base.rb', line 80

def initialize(model)
  @model = model
  @params = {}
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



17
18
19
# File 'lib/action_man/base.rb', line 17

def args
  @args
end

#modelObject (readonly)

Returns the value of attribute model.



17
18
19
# File 'lib/action_man/base.rb', line 17

def model
  @model
end

#paramsObject (readonly)

Returns the value of attribute params.



17
18
19
# File 'lib/action_man/base.rb', line 17

def params
  @params
end

#resultObject (readonly)

Returns the value of attribute result.



17
18
19
# File 'lib/action_man/base.rb', line 17

def result
  @result
end

Class Method Details

.after(*filters) ⇒ Object



26
27
28
29
# File 'lib/action_man/base.rb', line 26

def after(*filters, &)
  set_options_for_callbacks!(filters)
  set_callback(:execute, :after, *filters, &)
end

.after_failure(*filters) ⇒ Object



36
37
38
39
# File 'lib/action_man/base.rb', line 36

def after_failure(*filters, &)
  set_options_for_callbacks!(filters, on: :failure)
  set_callback(:execute, :after, *filters, &)
end

.after_success(*filters) ⇒ Object



31
32
33
34
# File 'lib/action_man/base.rb', line 31

def after_success(*filters, &)
  set_options_for_callbacks!(filters, on: :success)
  set_callback(:execute, :after, *filters, &)
end

.beforeObject



22
23
24
# File 'lib/action_man/base.rb', line 22

def before(...)
  set_callback(:execute, :before, ...)
end

.model(name) ⇒ Object



51
52
53
54
55
# File 'lib/action_man/base.rb', line 51

def model(name)
  define_method(name) do
    model
  end
end

.param(name) ⇒ Object



41
42
43
44
45
# File 'lib/action_man/base.rb', line 41

def param(name)
  define_method(name) do
    params[name]
  end
end

.params(*names) ⇒ Object



47
48
49
# File 'lib/action_man/base.rb', line 47

def params(*names)
  names.each { |name| param(name) }
end

Instance Method Details

#executable?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/action_man/base.rb', line 85

def executable?
  true
end

#run(params = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/action_man/base.rb', line 89

def run(params={})
  @params = params

  if executable?
    run_execute
  elsif exception_if_not_executable
    raise NotExecutable
  else
    Result.failure
  end
end