Top Level Namespace

Defined Under Namespace

Modules: Polyfill

Instance Method Summary collapse

Instance Method Details

#Polyfill(options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/polyfill.rb', line 8

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