Class: ApiMaker::CommandResponse
- Inherits:
-
Object
- Object
- ApiMaker::CommandResponse
- Defined in:
- app/services/api_maker/command_response.rb
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #error_for_command(id, data) ⇒ Object
- #fail_for_command(id, data) ⇒ Object
-
#initialize(controller:) ⇒ CommandResponse
constructor
A new instance of CommandResponse.
- #join_threads ⇒ Object
- #respond_to_command(id, data, type) ⇒ Object
- #result_for_command(id, data) ⇒ Object
- #threadding? ⇒ Boolean
- #with_thread(&blk) ⇒ Object
Constructor Details
#initialize(controller:) ⇒ CommandResponse
Returns a new instance of CommandResponse.
4 5 6 7 8 9 10 |
# File 'app/services/api_maker/command_response.rb', line 4 def initialize(controller:) @controller = controller @locale = I18n.locale @mutex = Mutex.new @result = {} @threads = [] end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
2 3 4 |
# File 'app/services/api_maker/command_response.rb', line 2 def controller @controller end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
2 3 4 |
# File 'app/services/api_maker/command_response.rb', line 2 def locale @locale end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
2 3 4 |
# File 'app/services/api_maker/command_response.rb', line 2 def result @result end |
Instance Method Details
#error_for_command(id, data) ⇒ Object
12 13 14 |
# File 'app/services/api_maker/command_response.rb', line 12 def error_for_command(id, data) respond_to_command(id, data, :error) end |
#fail_for_command(id, data) ⇒ Object
16 17 18 |
# File 'app/services/api_maker/command_response.rb', line 16 def fail_for_command(id, data) respond_to_command(id, data, :failed) end |
#join_threads ⇒ Object
24 25 26 27 28 |
# File 'app/services/api_maker/command_response.rb', line 24 def join_threads ActiveSupport::Dependencies.interlock.permit_concurrent_loads do @threads.each(&:join) end end |
#respond_to_command(id, data, type) ⇒ Object
30 31 32 33 34 |
# File 'app/services/api_maker/command_response.rb', line 30 def respond_to_command(id, data, type) @mutex.synchronize do @result[id] = {type: type, data: data} end end |
#result_for_command(id, data) ⇒ Object
20 21 22 |
# File 'app/services/api_maker/command_response.rb', line 20 def result_for_command(id, data) respond_to_command(id, data, :success) end |
#threadding? ⇒ Boolean
36 37 38 |
# File 'app/services/api_maker/command_response.rb', line 36 def threadding? ApiMaker::Configuration.current.threadding end |
#with_thread(&blk) ⇒ Object
40 41 42 43 44 45 46 |
# File 'app/services/api_maker/command_response.rb', line 40 def with_thread(&blk) if Rails.env.test? || !threadding? yield else spawn_thread(&blk) end end |