Class: Interactify::Async::JobKlass
- Inherits:
-
Object
- Object
- Interactify::Async::JobKlass
- Defined in:
- lib/interactify/async/job_klass.rb
Instance Attribute Summary collapse
-
#container_klass ⇒ Object
readonly
Returns the value of attribute container_klass.
-
#klass_suffix ⇒ Object
readonly
Returns the value of attribute klass_suffix.
Instance Method Summary collapse
- #args(context) ⇒ Object
- #async_job_klass ⇒ Object
- #attach_call(async_job_klass) ⇒ Object
- #attach_call!(async_job_klass) ⇒ Object
-
#initialize(container_klass:, klass_suffix:) ⇒ JobKlass
constructor
A new instance of JobKlass.
- #restrict_to_optional_or_keys_from_contract(args) ⇒ Object
Constructor Details
#initialize(container_klass:, klass_suffix:) ⇒ JobKlass
Returns a new instance of JobKlass.
8 9 10 11 |
# File 'lib/interactify/async/job_klass.rb', line 8 def initialize(container_klass:, klass_suffix:) @container_klass = container_klass @klass_suffix = klass_suffix end |
Instance Attribute Details
#container_klass ⇒ Object (readonly)
Returns the value of attribute container_klass.
6 7 8 |
# File 'lib/interactify/async/job_klass.rb', line 6 def container_klass @container_klass end |
#klass_suffix ⇒ Object (readonly)
Returns the value of attribute klass_suffix.
6 7 8 |
# File 'lib/interactify/async/job_klass.rb', line 6 def klass_suffix @klass_suffix end |
Instance Method Details
#args(context) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/interactify/async/job_klass.rb', line 45 def args(context) args = context.to_h.stringify_keys return args unless container_klass.respond_to?(:expected_keys) restrict_to_optional_or_keys_from_contract(args) end |
#async_job_klass ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/interactify/async/job_klass.rb', line 13 def async_job_klass klass = Class.new do include Interactor include Interactor::Contracts end attach_call(klass) attach_call!(klass) klass end |
#attach_call(async_job_klass) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/interactify/async/job_klass.rb', line 25 def attach_call(async_job_klass) # e.g. SomeInteractor::AsyncWithSuffix.call(foo: 'bar') async_job_klass.send(:define_singleton_method, :call) do |context| call!(context) end end |
#attach_call!(async_job_klass) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/interactify/async/job_klass.rb', line 32 def attach_call!(async_job_klass) this = self # e.g. SomeInteractor::AsyncWithSuffix.call!(foo: 'bar') async_job_klass.send(:define_singleton_method, :call!) do |context| # e.g. SomeInteractor::JobWithSuffix job_klass = this.container_klass.const_get("Job#{this.klass_suffix}") # e.g. SomeInteractor::JobWithSuffix.perform_async({foo: 'bar'}) job_klass.perform_async(this.args(context)) end end |
#restrict_to_optional_or_keys_from_contract(args) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/interactify/async/job_klass.rb', line 53 def restrict_to_optional_or_keys_from_contract(args) keys = container_klass.expected_keys.map(&:to_s) optional = Array(container_klass.optional_attrs).map(&:to_s) keys += optional args.slice(*keys) end |