lib/active_notifier/configurable.rb in active_notifier-0.1.0 vs lib/active_notifier/configurable.rb in active_notifier-0.2.0
- old
+ new
@@ -1,76 +1,104 @@
module ActiveNotifier
module Configurable
- def self.included(base)
- base.extend ClassMethods
- end
+ extend ActiveSupport::Concern
module ClassMethods
- def config
- @config ||= Configuration.new
- end
-
+ # Configure for ActiveNotifier
+ # @yield [config] Give a Configuration instance for settings
+ # @raise [ConfigureError]
+ # @example
+ # ActiveNotifier.configure do |config|
+ # config.const_name = :Message
+ # config.adapter = :slack
+ # # ...
+ # end
def configure
yield config
end
+
+ def config
+ @config ||= Configuration.new
+ end
end
class Configuration
- # 常量名, 简化调用, 符号,默认 :Notifier (如果被占用请更换)
+ # Alias const for ActiveNotifier
+ # @!attribute [r]
attr_reader :const_name
+ # Set alias const for ActiveNotifier
+ # @param const_name [#to_sym] (:Notifier) Alias const name
+ # @example
+ # ActiveNotifier.config.const_name = :Message
+ # ::Message == ::ActiveNotifier # => true
def const_name=(const_name)
const_name = const_name.to_sym
- const_name = "Notifier" if const_name.empty?
- raise ActiveNotifier::ConfigureError, "const_name 已经存在,请配置其他值" if Kernel.const_defined?(const_name)
+ const_name = :Notifier if const_name.empty?
+ error_message = "const_name is already defined, please set another value"
+ raise ActiveNotifier::ConfigureError, error_message if Kernel.const_defined?(const_name)
Kernel.const_set(const_name, ActiveNotifier)
@const_name = const_name
end
- # 消息适配器, 字符串, 默认 :dingtalk
+ # Message Adapter
def adapter
@adapter || :dingtalk
end
+ # Set message Adapter
+ # @param adapter [#to_sym] (:dingtalk)
def adapter=(adapter)
adapter = adapter.to_sym
unless adapters_with_base_url.key?(adapter)
- raise ActiveNotifier::ConfigureError, "adapter 当前只支持:#{adapters_with_base_url.keys.join(', ')}"
+ raise ActiveNotifier::ConfigureError, "adapter only support: #{adapters_with_base_url.keys.join(', ')}"
end
@adapter = adapter
end
- # 消息渠道的 channel_tokens 设置
+ # The tokens of channel
def channel_tokens
@channel_tokens || {}
end
+ # Set the tokens of channel
+ # @param channel_tokens [Hash] ({})
+ # @example
+ # ActiveNotifier.config.channel_tokens = {
+ # default: "xxx",
+ # order: "xxx"
+ # }
def channel_tokens=(channel_tokens)
channel_tokens = channel_tokens.to_h.symbolize_keys
@channel_tokens = channel_tokens
end
- # 消息模版主目录, 字符串, 默认 views/notifier/application), 不需要加模版后缀
+ # The template home directory
def template_home
@template_home || "#{File.expand_path(__dir__)}/templates"
end
+ # Set the template home directory
+ # @param template_home [String] (lib/active_notifier/templates)
def template_home=(template_home)
template_home = template_home.to_s
- raise ActiveNotifier::ConfigureError, "模板主目录不能为空" if template_home.empty?
+ raise ActiveNotifier::ConfigureError, "template_home value can't be blank" if template_home.empty?
@template_home = template_home
end
- # 消息优先类型, 字符串, 默认 :markdown, 对应模板后缀名,如果未指定该参数且存在多个模板,则优先选择此类型
+ # The priority type for message, if notifier execute without type option and there are multiple valid types,
+ # then ActiveNotifier will use the template with this type first
def priority_type
@priority_type || :markdown
end
+ # Set the priority type
+ # @param priority_type [#to_sym] (:markdown)
def priority_type=(priority_type)
@priority_type = priority_type.to_sym
end
- # 适配器以及基本连接
+ # The adapters and its base url
def adapters_with_base_url
@adapters_with_base_url ||= {
dingtalk: "https://oapi.dingtalk.com/robot/send?access_token="
}
end