Ruby on rails 在类中使用类方法定义选项,就像Mailer所做的操作一样
我希望有一个可以包含在课堂中的模块,并允许设置如下选项:Ruby on rails 在类中使用类方法定义选项,就像Mailer所做的操作一样,ruby-on-rails,ruby,Ruby On Rails,Ruby,我希望有一个可以包含在课堂中的模块,并允许设置如下选项: class MyService include Healthcheck healthcheck_id 'foobar' end 模块将类似于: module Healthcheck extend ActiveSupport::Concern included do def self.healthcheck_id(value) # What do I do here? end end e
class MyService
include Healthcheck
healthcheck_id 'foobar'
end
模块将类似于:
module Healthcheck
extend ActiveSupport::Concern
included do
def self.healthcheck_id(value)
# What do I do here?
end
end
end
问题是:如何存储作为参数传递的这个值,以便以后使用
也许一些背景可能会有所帮助,我受到了Action Mailer的启发:
class ExampleMailer < ActionMailer::Base
default from: "no-reply@example.com"
end
class-ExampleMailer
在上面的示例中,类方法(?)
default
接受带参数的散列,显然,当发送电子邮件时,Action Mailer使用了from
将其存储在类变量中
@@arguments\u passed=value
将其存储在类变量中
@@arguments\u passed=value
您可以查看
它使用()您可以查看
它使用()
module Healthcheck
extend ActiveSupport::Concern
included do
def self.healthcheck_id(value)
@@healthcheck_id_value = value
end
class_eval do
def healthcheck_id_value
self.class.class_variable_get(:@@healthcheck_id_value)
end
end
end
end
因此,从现在起,您可以访问健康检查\u id\u值
,例如:
class MyService
include Healthcheck
healthcheck_id 'foobar'
def foo
puts healthcheck_id_value
end
end
让我们调用
MyService.new.foo
,它将打印“foobar”您可以使用类变量来执行此操作,因此代码为:
module Healthcheck
extend ActiveSupport::Concern
included do
def self.healthcheck_id(value)
@@healthcheck_id_value = value
end
class_eval do
def healthcheck_id_value
self.class.class_variable_get(:@@healthcheck_id_value)
end
end
end
end
因此,从现在起,您可以访问健康检查\u id\u值
,例如:
class MyService
include Healthcheck
healthcheck_id 'foobar'
def foo
puts healthcheck_id_value
end
end
让我们调用MyService.new.foo
,它将打印“foobar”pb with的解决方案是不能为不同的类定义不同的healthcheck\u id
值:
classmyservice1
包括健康检查
healthcheck_id“foobar_1”
德福
将healthcheck\u id\u值
结束
结束
类MyService2
包括健康检查
healthcheck_id“foobar_2”
德福
将healthcheck\u id\u值
结束
结束
MyService1.new.foo#foobar_2
MyService2.new.foo#foobar_2
更好的解决办法是:
module Healthcheck
extend ActiveSupport::Concern
included do
def self.healthcheck_id(value)
@@healthcheck_id_value = value
end
class_eval do
def healthcheck_id_value
self.class.class_variable_get(:@@healthcheck_id_value)
end
end
end
end
模块健康检查
扩展ActiveSupport::关注点
包括做
class\u属性:healthcheck\u id\u值
定义自身健康检查\u id(值)
self.healthcheck\u id\u value=value
结束
def self.foo
健康检查\u id\u值
结束
结束
结束
类MyService1
包括健康检查
healthcheck_id“foobar_1”
结束
类MyService2
包括健康检查
healthcheck_id“foobar_2”
结束
MyService1.foo#foobar_1
MyService2.foo#foobar_2
pb with的解决方案是,您不能为不同的类定义不同的healthcheck\u id
值:
classmyservice1
包括健康检查
healthcheck_id“foobar_1”
德福
将healthcheck\u id\u值
结束
结束
类MyService2
包括健康检查
healthcheck_id“foobar_2”
德福
将healthcheck\u id\u值
结束
结束
MyService1.new.foo#foobar_2
MyService2.new.foo#foobar_2
更好的解决办法是:
module Healthcheck
extend ActiveSupport::Concern
included do
def self.healthcheck_id(value)
@@healthcheck_id_value = value
end
class_eval do
def healthcheck_id_value
self.class.class_variable_get(:@@healthcheck_id_value)
end
end
end
end
模块健康检查
扩展ActiveSupport::关注点
包括做
class\u属性:healthcheck\u id\u值
定义自身健康检查\u id(值)
self.healthcheck\u id\u value=value
结束
def self.foo
健康检查\u id\u值
结束
结束
结束
类MyService1
包括健康检查
healthcheck_id“foobar_1”
结束
类MyService2
包括健康检查
healthcheck_id“foobar_2”
结束
MyService1.foo#foobar_1
MyService2.foo#foobar_2
不是说不要使用类变量,只是在子类周围小心使用它们:类MySubService
然后
MyService.new.foo#=>“barfoo”;MySubService.new.foo#=>'barfoo'
不是说不要使用类变量,只是在子类周围小心使用它们:类MySubService
然后MyService.new.foo#=>“barfoo”;MySubService.new.foo#=>“barfoo”