Class: CommandProposal::Services::CommandInterpreter
- Inherits:
-
Object
- Object
- CommandProposal::Services::CommandInterpreter
show all
- Includes:
- PermissionsHelper
- Defined in:
- lib/command_proposal/services/command_interpreter.rb
Defined Under Namespace
Classes: Error
Class Method Summary
collapse
Instance Method Summary
collapse
#approval_required?, #can_approve?, #can_command?, #cmd_config, #command_user, #current_is_author?, #has_approval?, #permitted_to_use?
Constructor Details
#initialize(iteration, command, user, params = {}) ⇒ CommandInterpreter
Returns a new instance of CommandInterpreter.
13
14
15
16
17
18
19
20
|
# File 'lib/command_proposal/services/command_interpreter.rb', line 13
def initialize(iteration, command, user, params={})
@iteration = iteration
@task = iteration.task
@command = command.to_s.to_sym
@user = user
@params = params
command_user(@user) if @user.present?
end
|
Class Method Details
.command(iteration, command, user, params = {}) ⇒ Object
9
10
11
|
# File 'lib/command_proposal/services/command_interpreter.rb', line 9
def self.command(iteration, command, user, params={})
new(iteration, command, user, params).command
end
|
Instance Method Details
#check_can_approve? ⇒ Boolean
106
107
108
109
110
|
# File 'lib/command_proposal/services/command_interpreter.rb', line 106
def check_can_approve?
return true if can_approve?(@iteration)
error!("You cannot approve your own command.")
end
|
#check_can_command? ⇒ Boolean
100
101
102
103
104
|
# File 'lib/command_proposal/services/command_interpreter.rb', line 100
def check_can_command?
return true if can_command?
error!("Sorry, you do not have permission to do this.")
end
|
#command ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/command_proposal/services/command_interpreter.rb', line 22
def command
case @command
when :request then command_request
when :approve then command_approve
when :run then command_run
when :cancel then command_cancel
when :close then command_close
end
@iteration
end
|
#command_approve ⇒ Object
54
55
56
57
58
59
60
61
|
# File 'lib/command_proposal/services/command_interpreter.rb', line 54
def command_approve
error!("Command is not ready for approval.") unless @iteration.pending?
check_can_command? && check_can_approve?
@iteration.update(status: :approved, approver: @user, approved_at: Time.current)
proposal = ::CommandProposal::Service::ProposalPresenter.new(@iteration)
::CommandProposal.configuration.approval_callback&.call(proposal)
end
|
#command_cancel ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/command_proposal/services/command_interpreter.rb', line 77
def command_cancel
check_can_command?
return if @iteration.complete?
@iteration.update(status: :cancelling)
return if ::CommandProposal.sessions.key?("task:#{@task.id}")
::CommandProposal::Services::ShutDown.terminate(@iteration)
end
|
#command_close ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/command_proposal/services/command_interpreter.rb', line 87
def command_close
check_can_command?
return unless @iteration.task.console?
if ::CommandProposal.sessions.key?("task:#{@task.id}")
@task.first_iteration.update(status: :success, completed_at: Time.current)
else
ended_at = @task.iterations.last&.end_time || Time.current
@task.first_iteration.update(status: :terminated, completed_at: ended_at)
end
::CommandProposal.sessions.delete("task:#{@task.id}")
end
|
#command_request ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/command_proposal/services/command_interpreter.rb', line 34
def command_request
check_can_command?
if @iteration.complete? && (@task.task? || @task.function?)
previous_iteration = @iteration
@task.user = @user @task.update(code: @iteration.code)
@iteration = @task.current_iteration
if @task.function? && previous_iteration.approved_at?
@params.merge!(previous_iteration.attributes.slice("approved_at", "approver_id"))
@params.merge!(status: :approved)
return end
end
proposal = ::CommandProposal::Service::ProposalPresenter.new(@iteration)
::CommandProposal.configuration.proposal_callback&.call(proposal)
end
|
#command_run ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/command_proposal/services/command_interpreter.rb', line 63
def command_run
check_can_command?
ActiveRecord::Base.transaction do
command_request if @task.function? && @iteration.approved_at? && @iteration.complete?
@iteration.update(@params.merge(requester: @user))
error!("Cannot run without approval.") unless has_approval?(@task)
end
::CommandProposal::CommandRunnerJob.perform_later(@iteration.id)
end
|