Sha256: f6db91339104c320de855bcb281f6fcd370503bac4b9ff57765416825a482dc1

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

module BubbleWrap
  module Deprecated

    class DeprecatedError < StandardError; end

    def deprecated(method_sym, version)
      unless method_sym.kind_of?(Symbol)
        raise ArgumentError, "deprecated() requires symbols for its first argument."
      end

      scope = nil
      alias_scope = nil
      if self.methods.include?(method_sym)
        scope = :define_singleton_method
        alias_scope = (class << self; self end)
      elsif self.instance_methods.include?(method_sym)
        scope = :define_method
        alias_scope = self
      else
        raise ArgumentError, "Method not found for deprecated() - #{method_sym}"
      end


      send(scope, "#{method_sym}_with_deprecation", ->(*args, &block) {
        fail = BubbleWrap.version.to_s >= version.to_s
        if fail
          raise DeprecatedError, "#{method_sym} was deprecated and removed in BubbleWrap #{version}"
        else
          NSLog "#{method_sym} is deprecated -- it will be removed in BubbleWrap #{version}"
          send("#{method_sym}_without_deprecation", *args, &block)
        end
      })

      alias_scope.send(:alias_method, "#{method_sym}_without_deprecation", method_sym)
      alias_scope.send(:alias_method, method_sym, "#{method_sym}_with_deprecation")
    end

    def self.included(base)
      base.extend(self)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bubble-wrap-1.8.0 motion/util/deprecated.rb
bubble-wrap-1.7.1 motion/util/deprecated.rb
bubble-wrap-1.7.0 motion/util/deprecated.rb
bubble-wrap-1.6.0 motion/util/deprecated.rb
bubble-wrap-1.6.0.rc1 motion/util/deprecated.rb
bubble-wrap-1.5.0 motion/util/deprecated.rb