Module: ROM::Deprecations

Defined in:
lib/rom/support/deprecations.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.announce(name, msg) ⇒ Object



44
45
46
# File 'lib/rom/support/deprecations.rb', line 44

def self.announce(name, msg)
  warn(deprecation_message(name, msg))
end

.deprecation_message(name, msg) ⇒ Object



48
49
50
51
52
53
# File 'lib/rom/support/deprecations.rb', line 48

def self.deprecation_message(name, msg)
  <<-MSG
    #{name} is deprecated and will be removed in the next major version
    #{message(msg)}
  MSG
end

.logger(output = nil) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rom/support/deprecations.rb', line 62

def self.logger(output = nil)
  if defined?(@logger)
    @logger
  else
    set_logger!(output)
  end
end

.message(msg) ⇒ Object



55
56
57
58
59
60
# File 'lib/rom/support/deprecations.rb', line 55

def self.message(msg)
  <<-MSG
    #{msg}
    #{caller.detect { |l| !l.include?('lib/rom')}}
  MSG
end

.set_logger!(output = nil) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/rom/support/deprecations.rb', line 70

def self.set_logger!(output = nil)
  @logger = Logger.new(output || $stdout)
  @logger.formatter = proc { |severity, datetime, progname, msg|
    "[rom] #{msg}\n"
  }
  @logger
end

.warn(msg) ⇒ Object



40
41
42
# File 'lib/rom/support/deprecations.rb', line 40

def self.warn(msg)
  logger.warn(msg.gsub(/^\s+/, ''))
end

Instance Method Details

#deprecate(old_name, new_name, msg = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rom/support/deprecations.rb', line 6

def deprecate(old_name, new_name, msg = nil)
  class_eval do
    define_method(old_name) do |*args, &block|
      ROM::Deprecations.announce "#{self.class}##{old_name} is", <<-MSG
        Please use #{self.class}##{new_name} instead.
        #{msg}
      MSG
      __send__(new_name, *args, &block)
    end
  end
end

#deprecate_class_method(old_name, new_name, msg = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rom/support/deprecations.rb', line 18

def deprecate_class_method(old_name, new_name, msg = nil)
  full_msg =
    if new_name.is_a?(Symbol)
      full_msg = Deprecations.deprecation_message "#{self.name}.#{old_name}", <<-MSG
      Please use #{self.name}.#{new_name} instead.
      #{msg}
      MSG
    else
      Deprecations.deprecation_message "#{self.name}.#{old_name}", new_name
    end

  meth = new_name.is_a?(Symbol) ? method(new_name) : method(old_name)
  instance_eval "undef #{old_name}"

  class_eval do
    define_singleton_method(old_name) do |*args, &block|
      Deprecations.warn(full_msg)
      meth.call(*args, &block)
    end
  end
end