我的代码库中的一个实用工具类使用元编程动态生成类方法。
我的问题:
FeatureFlagHandler.foo_enabled?
方法。FeatureFlagHandler.fo
时,应该可以建议
FeatureFlagHandler.foo_enabled?
方法。
类的源代码:
class FeatureFlagHandler
class << self
# Generates `_enabled?` and `_disabled?` helper methods for feature flags
# for all Constants defined in AppUtils::FeatureFlags
#
# module AppUtils
# module FeatureFlags
# FOO = "FOO"
# BAR = "BAR"
# end
# end
#
# e.g. If AppUtils::FeatureFlags contains FOO and BAR constant, then
# below code will generate following method definitions:
#
# def self.foo_enabled?
# enabled?("FOO")
# end
# def self.bar_enabled?
# enabled?("BAR")
# end
#
# Then above methods can be used as:
#
# do_something if FeatureFlagHandler.foo_enabled?
#
AppUtils::FeatureFlags.constants.each do |flag|
define_method("#{flag.to_s.downcase}_enabled?") do
# Logic to check if `feature_flag` is ON or OFF
# For eg:
ENV[feature_flag] == "ENABLE"
end
end
end
end
注意:定期从AppUtils::FeatureFlags
模块上方添加/删除功能标志
答案 0 :(得分:0)
可以使用YARD @!macro
完成,但不幸的是,RubyMine还不支持该请求,有一个相应的请求:https://youtrack.jetbrains.com/issue/RUBY-20130