Sha256: 05ddd6f77729479c1467d2acbce7e1292b8cd63b4c7a981670efbc987827a690

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require 'polyfill/version'
require 'polyfill/v2_4'

module Polyfill
  include V2_4
end

def Polyfill(options)
  mod = Module.new

  klasses, others = options.partition { |key,| key[/\A[A-Z]/] }

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

  klasses.each do |name, methods|
    class_or_module_mod =
      begin
        Polyfill::V2_4.const_get(name, false)
      rescue NameError
        raise ArgumentError, %Q("#{name}" is not a valid class or has no updates)
      end

    if methods == :all
      mod.module_eval do
        include class_or_module_mod
      end
    else
      methods.each do |method|
        type =
          case method[0]
          when '.'
            :Class
          when '#'
            :Instance
          else
            raise ArgumentError, %Q("#{method}" must start with a "." if it's a class method or "#" if it's an instance method)
          end
        method_name =
          case method[-1]
          when '?'
            "#{method[1..-2]}_q"
          when '!'
            "#{method[1..-2]}_e"
          else
            method[1..-1]
          end
        method_name.capitalize!
        method_name.gsub!(/_(.)/) { $1.capitalize }

        method_mod =
          begin
            class_or_module_mod
              .const_get(type, false)
              .const_get(method_name, false)
          rescue NameError
            raise ArgumentError, %Q("#{method}" is not a valid method on #{name} or has no updates)
          end

        mod.module_eval do
          include class_or_module_mod.const_get(type, false).const_get(method_name, false)
        end
      end
    end
  end

  mod
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polyfill-0.3.0 lib/polyfill.rb