Module: GLib::Deprecatable

Included in:
GLib, Idle, Param, SpawnError, Unicode
Defined in:
lib/glib2/deprecatable.rb

Constant Summary collapse

@@deprecated_const =
{}
@@deprecated_signal =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(class_or_module) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/glib2/deprecatable.rb', line 73

def self.extended(class_or_module)
  GLib::Instantiatable.class_eval do
    %w(signal_connect signal_connect_after).each do |connect_method|
      alias_name = "__deprecatable_#{connect_method}__"
      next if private_method_defined?(alias_name)
      alias_method alias_name, connect_method
      private alias_name

      define_method(connect_method) do |signal, *margs, &mblock|
        signal = signal.to_s.gsub('_', '-').to_sym
        signals = @@deprecated_signal[self]
        if new_signal = (signals || {})[signal]
          msg = "#{caller[0]}: '#{signal}' signal has been deprecated."
          case new_signal
          when String, Symbol
            warn "#{msg} Use '#{new_signal}' signal."
            signal = new_signal
          when Hash
            if new_signal[:raise]
              raise DeprecatedError.new("#{msg} #{new_signal[:raise]}")
            elsif new_signal[:warn]
              warn "#{msg} #{new_signal[:warn]}"
            else
              warn "#{msg} Don't use this signal anymore."
            end
            return
          end
        end
        __send__(alias_name, signal, *margs, &mblock)
      end
    end
  end
end

Instance Method Details

#define_deprecated_const(deprecated_const, new_const = {}) ⇒ Object



27
28
29
30
# File 'lib/glib2/deprecatable.rb', line 27

def define_deprecated_const(deprecated_const, new_const = {})
  @@deprecated_const[self] ||= {}
  @@deprecated_const[self][deprecated_const.to_sym] = new_const
end

#define_deprecated_enums(enums, prefix = nil) ⇒ Object Also known as: define_deprecated_flags



32
33
34
35
36
37
38
39
# File 'lib/glib2/deprecatable.rb', line 32

def define_deprecated_enums(enums, prefix = nil)
  enums = resolve_constant_name(enums.to_s)
  enums.constants.each do |const|
    deprecated_const = prefix ? "#{prefix}_#{const}" : const
    new_const = [enums, const].join('::')
    define_deprecated_const(deprecated_const, new_const)
  end
end

#define_deprecated_method(deprecated_method, new_method = {}, &block) ⇒ Object



46
47
48
# File 'lib/glib2/deprecatable.rb', line 46

def define_deprecated_method(deprecated_method, new_method = {}, &block)
  __define_deprecated_method__(:instance, deprecated_method, new_method, &block)
end

#define_deprecated_method_by_hash_args(deprecated_method, old_args, new_args, req_argc = 0, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/glib2/deprecatable.rb', line 50

def define_deprecated_method_by_hash_args(deprecated_method, old_args, new_args, req_argc = 0, &block)
  klass = self
  alias_name = "__deprecatable_#{object_id}_#{deprecated_method}__"
  alias_method alias_name, deprecated_method
  private alias_name

  define_method(deprecated_method) do |*margs, &mblock|
    if (margs.size == req_argc) || (margs.size == (req_argc + 1) && margs.last.is_a?(Hash))
    else
      margs = block.call(self, *margs, &mblock)
      msg = "#{caller[0]}: '#{klass}##{deprecated_method}(#{old_args})' style has been deprecated."
      warn "#{msg} Use '#{klass}##{deprecated_method}(#{new_args})' style."
    end
    __send__(alias_name, *margs, &mblock)
  end
end

#define_deprecated_signal(deprecated_signal, new_signal = {}) ⇒ Object



68
69
70
71
# File 'lib/glib2/deprecatable.rb', line 68

def define_deprecated_signal(deprecated_signal, new_signal = {})
  @@deprecated_signal[self] ||= {}
  @@deprecated_signal[self][deprecated_signal.to_s.gsub('_', '-').to_sym] = new_signal
end

#define_deprecated_singleton_method(deprecated_method, new_method = {}, &block) ⇒ Object



42
43
44
# File 'lib/glib2/deprecatable.rb', line 42

def define_deprecated_singleton_method(deprecated_method, new_method = {}, &block)
  __define_deprecated_method__(:singleton, deprecated_method, new_method, &block)
end

#define_singleton_method(name, &block) ⇒ Object



20
21
22
23
# File 'lib/glib2/deprecatable.rb', line 20

def define_singleton_method(name, &block)
  singleton_class = class << self; self; end
  singleton_class.__send__(:define_method, name, &block)
end