Module: ActiveModel::LoggerAttributes::ClassMethods

Defined in:
lib/active_model/logger_attributes.rb

Instance Method Summary collapse

Instance Method Details

#define_logger_attr_accessor(attribute) ⇒ Object



40
41
42
43
44
45
# File 'lib/active_model/logger_attributes.rb', line 40

def define_logger_attr_accessor(attribute)
  options = @logger_attributes[attribute]
  define_method(options[:logger_name]) do
    instance_variable_get(:"@#{options[:logger_name]}") || send(:"initialize_#{options[:logger_name]}")
  end
end

#define_logger_attr_initializer(attribute) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/active_model/logger_attributes.rb', line 31

def define_logger_attr_initializer(attribute)
  options = @logger_attributes[attribute]
  define_method(:"initialize_#{options[:logger_name]}") do
    define_attr_logger_attribute(attribute)
    logger = logger_for_logger_attribute(attribute, options[:logger_class], &options[:logger_init])
    instance_variable_set :"@#{options[:logger_name]}", logger
  end
end

#logger_attr(attribute, options = {}) ⇒ Object



25
26
27
28
29
# File 'lib/active_model/logger_attributes.rb', line 25

def logger_attr(attribute, options = {})
  attribute = attribute.to_sym
  setup_logger_attr(attribute, options)
  attr_accessor(attribute)
end

#logger_attr_default_optionsObject



13
14
15
# File 'lib/active_model/logger_attributes.rb', line 13

def logger_attr_default_options
  { logger_class: ::Logger, logger_name: nil, logger_init: ->(l) {} }
end

#setup_logger_attr(attribute, options) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/active_model/logger_attributes.rb', line 17

def setup_logger_attr(attribute, options)
  @logger_attributes ||= {}
  @logger_attributes[attribute] = logger_attr_default_options.merge(options)
  @logger_attributes[attribute][:logger_name] ||= "#{attribute}_logger"
  define_logger_attr_initializer(attribute)
  define_logger_attr_accessor(attribute)
end