Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在ruby中组织模块、服务及其共享方法_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 在ruby中组织模块、服务及其共享方法

Ruby on rails 在ruby中组织模块、服务及其共享方法,ruby-on-rails,ruby,Ruby On Rails,Ruby,我试图在两个ruby服务之间共享一个方法,这些服务有很多共同点,它们位于同一个模块中 TranslatorManager是提供服务的模块:Cache橡皮擦和Translator 共享方法是key_cache,我需要从cache橡皮擦和Translator服务调用它,仅对它们而言,它仅与TranslatorManager相关,因此我建议此代码必须位于模块文件Translator_manager.rb中 每个服务都有自己的文件,所有这些文件都位于app/services/site/translato

我试图在两个ruby服务之间共享一个方法,这些服务有很多共同点,它们位于同一个模块中

TranslatorManager是提供服务的模块:Cache橡皮擦和Translator

共享方法是key_cache,我需要从cache橡皮擦和Translator服务调用它,仅对它们而言,它仅与TranslatorManager相关,因此我建议此代码必须位于模块文件Translator_manager.rb中

每个服务都有自己的文件,所有这些文件都位于app/services/site/translator\u manager/

对我来说,这是正确的文件组织,不是吗

如何从call services方法调用key_缓存?这不管用

我不能从嵌套类中包括Site::TranslatorManager 需要密钥缓存作为类方法吗?考虑到这些文件是不同的,如何扩展它?我试过了,但没用。 cache_橡皮擦.rb

您需要在Cache橡皮擦类中包含Site::TranslatorManager模块:


这不起作用:NoMethodError:undefined method key_cache'对于同一文件夹中的所有对象,我将在问题中添加一个图像我已移动到上一个文件夹,现在您的解决方案起作用我想知道这是否是一个合理的解决方案。。。。就像包含一个模块,您已经are@AlbertCatal是的,您需要在app/services/site文件夹中存储模块
module Site
  module TranslatorManager
    class CacheEraser < ApplicationService
      def initialize(company_id, text, head_locale, locale)
        @company_id, @text, @head_locale, @locale = company_id, text, head_locale, locale
      end

      def call
        #             ---> Don't work <----
        Rails.cache.delete key_cache(@company_id, @text, @head_locale, @locale)
      end
    end
  end
end
module Site
  module TranslatorManager
    def key_cache(company_id, text, head_locale, locale)
      # return a string
    end

    def translatable_key?(key)
      # return true or false
    end
  end
end
module Site
  module TranslatorManager
    class CacheEraser < ApplicationService
      include Site::TranslatorManager # include module methods
      def initialize(company_id, text, head_locale, locale)
        @company_id, @text, @head_locale, @locale = company_id, text, head_locale, locale
      end

      def call
        #             ---> will work <----
        Rails.cache.delete key_cache(@company_id, @text, @head_locale, @locale)
      end
    end
  end
end