module Polyfill

Constants

VERSION

Public Class Methods

get(module_name, methods, options = {}) click to toggle source
# File lib/polyfill.rb, line 7
def get(module_name, methods, options = {})
  if Object.const_get(module_name.to_s, false).is_a?(Class)
    raise ArgumentError, "#{module_name} is a class not a module"
  end

  version_option = options.delete(:version)

  #
  # parse options
  #
  versions = InternalUtils.polyfill_versions_to_use(version_option)

  unless options.empty?
    raise ArgumentError, "unknown keyword: #{options.first[0]}"
  end

  #
  # find all polyfills for the module across all versions
  #
  modules_with_updates, modules = InternalUtils.modules_to_use(module_name, versions)

  #
  # remove methods that were not requested
  #
  requested_methods = InternalUtils.methods_to_keep(modules_with_updates, methods, '#', module_name)

  modules.each do |instance_module|
    InternalUtils.keep_only_these_methods!(instance_module, requested_methods)
  end

  #
  # build the module to return
  #
  InternalUtils.create_module(module_name, methods, options, version_option) do |mod|
    # make sure the methods get added if this module is included
    mod.singleton_class.send(:define_method, :included) do |base|
      modules.each do |module_to_add|
        base.include module_to_add unless module_to_add.instance_methods.empty?
      end
    end

    # make sure the methods get added if this module is extended
    mod.singleton_class.send(:define_method, :extended) do |base|
      modules.each do |module_to_add|
        base.extend module_to_add unless module_to_add.instance_methods.empty?
      end
    end

    # make sure the methods get added if this module is prepended
    mod.singleton_class.send(:define_method, :prepended) do |base|
      modules.each do |module_to_add|
        base.prepend module_to_add unless module_to_add.instance_methods.empty?
      end
    end
  end
end

Private Instance Methods

get(module_name, methods, options = {}) click to toggle source
# File lib/polyfill.rb, line 7
def get(module_name, methods, options = {})
  if Object.const_get(module_name.to_s, false).is_a?(Class)
    raise ArgumentError, "#{module_name} is a class not a module"
  end

  version_option = options.delete(:version)

  #
  # parse options
  #
  versions = InternalUtils.polyfill_versions_to_use(version_option)

  unless options.empty?
    raise ArgumentError, "unknown keyword: #{options.first[0]}"
  end

  #
  # find all polyfills for the module across all versions
  #
  modules_with_updates, modules = InternalUtils.modules_to_use(module_name, versions)

  #
  # remove methods that were not requested
  #
  requested_methods = InternalUtils.methods_to_keep(modules_with_updates, methods, '#', module_name)

  modules.each do |instance_module|
    InternalUtils.keep_only_these_methods!(instance_module, requested_methods)
  end

  #
  # build the module to return
  #
  InternalUtils.create_module(module_name, methods, options, version_option) do |mod|
    # make sure the methods get added if this module is included
    mod.singleton_class.send(:define_method, :included) do |base|
      modules.each do |module_to_add|
        base.include module_to_add unless module_to_add.instance_methods.empty?
      end
    end

    # make sure the methods get added if this module is extended
    mod.singleton_class.send(:define_method, :extended) do |base|
      modules.each do |module_to_add|
        base.extend module_to_add unless module_to_add.instance_methods.empty?
      end
    end

    # make sure the methods get added if this module is prepended
    mod.singleton_class.send(:define_method, :prepended) do |base|
      modules.each do |module_to_add|
        base.prepend module_to_add unless module_to_add.instance_methods.empty?
      end
    end
  end
end