From 7220ae30340f42a7105f465ee94bf2bafa50a15e Mon Sep 17 00:00:00 2001 From: shubh Date: Wed, 25 Oct 2023 16:55:24 +0400 Subject: [PATCH 1/3] context_base_class --- lib/servitium/service.rb | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/servitium/service.rb b/lib/servitium/service.rb index d88d236..f3bb7dc 100644 --- a/lib/servitium/service.rb +++ b/lib/servitium/service.rb @@ -268,39 +268,28 @@ def context_class_name name.gsub('Service', 'Context') end - def context_class! + def context_class!(*args) return context_class if context_class - context_class_parts = context_class_name.split('::') context_class_name_part = context_class_parts.pop context_module_name = context_class_parts.join('::') context_module = context_module_name.present? ? context_module_name.constantize : Object - + context_base_class_name = args.first[:base_class] if args.first&.key?(:base_class) + context_base_class_name ||= self.superclass.to_s.gsub('Service', 'Context') + context_base_class_name ||= 'Servitium::Context' context_module.const_set(context_class_name_part, Class.new(context_base_class_name.constantize)) context_class end - # Get the base class for new contexts defined using context blocks - # Defaults to Servitium::Context - def context_base_class_name - @@_context_base_class_name ||= 'Servitium::Context' - end - - # Override the base class for contexts defined using context blocks, you can use this to - # change the base class to your own ApplicationContext - def context_base_class_name=(base_class) - @@_context_base_class_name = base_class - end - def context(*args, &block) return initialized_context(*args) unless block_given? begin - context_class!.new + context_class!(*args).new rescue StandardError nil end - context_class!.class_eval(&block) + context_class!(*args).class_eval(&block) end def initialized_context(*args) From 5314f9e3ffe4763ae4421544c042e5556efd668f Mon Sep 17 00:00:00 2001 From: shubh Date: Wed, 25 Oct 2023 17:00:18 +0400 Subject: [PATCH 2/3] context_base_class --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index a8cdd20..aa6db8b 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,11 @@ class ExampleService < ApplicationService end end ``` +You can also override context base class like this +```ruby +context base_class: 'ApplicationContext' do +end +``` Next you use it as follows: ```ruby From 38a66e25aa478461c265a71d3a9b343fc68e7499 Mon Sep 17 00:00:00 2001 From: shubh Date: Mon, 19 Aug 2024 10:33:47 +0400 Subject: [PATCH 3/3] merge main --- lib/servitium/service.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/servitium/service.rb b/lib/servitium/service.rb index bee619d..1913411 100644 --- a/lib/servitium/service.rb +++ b/lib/servitium/service.rb @@ -268,7 +268,7 @@ def context_class_name name.gsub("Service", "Context") end - def context_class!(*) + def context_class!(*args) return context_class if context_class context_class_parts = context_class_name.split("::") @@ -290,7 +290,7 @@ def context(*, &) rescue nil end - context_class!(*).class_eval(&block) + context_class!(*).class_eval(&) end def initialized_context(*)