Module: Polyfill::InternalUtils

Defined in:
lib/polyfill/internal_utils.rb

Overview

rubocop:disable Metrics/ModuleLength

Class Method Summary collapse

Class Method Details

.create_module {|mod| ... } ⇒ Object

Yields:

  • (mod)


85
86
87
88
89
90
91
# File 'lib/polyfill/internal_utils.rb', line 85

def create_module
  mod = ::Module.new

  yield(mod)

  Polyfill::Module.const_set("M#{mod.object_id}", mod)
end

.current_ruby_versionObject



11
12
13
# File 'lib/polyfill/internal_utils.rb', line 11

def current_ruby_version
  @current_ruby_version ||= RUBY_VERSION[/\A(\d+\.\d+)/, 1]
end

.ignore_warningsObject



16
17
18
19
20
21
22
23
# File 'lib/polyfill/internal_utils.rb', line 16

def ignore_warnings
  orig = $VERBOSE
  $VERBOSE = nil

  yield

  $VERBOSE = orig
end

.keep_only_these_methods!(mod, whitelist) ⇒ Object



40
41
42
43
44
# File 'lib/polyfill/internal_utils.rb', line 40

def keep_only_these_methods!(mod, whitelist)
  mod.instance_methods.each do |name|
    mod.send(:remove_method, name) unless whitelist.include?(name)
  end
end

.methods_to_keep(modules, methods, lead_symbol, module_name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/polyfill/internal_utils.rb', line 73

def methods_to_keep(modules, methods, lead_symbol, module_name)
  methods_with_updates = modules.flat_map(&:instance_methods).uniq
  requested_methods = methods == :all ? methods_with_updates : methods

  unless (leftovers = (requested_methods - methods_with_updates)).empty?
    raise ArgumentError, %Q("#{lead_symbol}#{leftovers.first}" is not a valid method on #{module_name} or has no updates)
  end

  requested_methods
end

.modules_to_use(module_name, versions) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/polyfill/internal_utils.rb', line 47

def modules_to_use(module_name, versions)
  modules_with_updates = []
  modules = []

  versions.each do |version_number, version_module|
    begin
      final_module = version_module.const_get(module_name.to_s, false)

      modules_with_updates << final_module

      next if version_number <= InternalUtils.current_ruby_version

      modules << final_module.clone
    rescue NameError
      nil
    end
  end

  if modules_with_updates.empty?
    raise ArgumentError, %Q("#{module_name}" has no updates)
  end

  [modules_with_updates, modules]
end

.polyfill_versions_to_use(desired_version = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/polyfill/internal_utils.rb', line 26

def polyfill_versions_to_use(desired_version = nil)
  desired_version = VERSIONS.keys.max if desired_version.nil?

  unless VERSIONS.keys.include?(desired_version)
    raise ArgumentError, "invalid value for keyword version: #{desired_version}"
  end

  VERSIONS
    .reject { |version, _| version > desired_version }
    .map { |version, mod| [version, Polyfill.const_get(mod, false)] }
    .to_h
end

.to_f(obj) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/polyfill/internal_utils.rb', line 120

def to_f(obj)
  begin
    unless obj.respond_to?(:to_f)
      raise TypeError, "no implicit conversion of #{obj.class} into Float"
    end
  rescue NoMethodError
    raise TypeError, 'no implicit conversion of BasicObject into Float'
  end

  obj.to_f
end

.to_hash(obj) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/polyfill/internal_utils.rb', line 133

def to_hash(obj)
  begin
    unless obj.respond_to?(:to_hash)
      raise TypeError, "no implicit conversion of #{obj.class} into Hash"
    end
  rescue NoMethodError
    raise TypeError, 'no implicit conversion of BasicObject into Hash'
  end

  obj.to_hash
end

.to_int(obj) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/polyfill/internal_utils.rb', line 107

def to_int(obj)
  begin
    unless obj.respond_to?(:to_int)
      raise TypeError, "no implicit conversion of #{obj.class} into Integer"
    end
  rescue NoMethodError
    raise TypeError, 'no implicit conversion of BasicObject into Integer'
  end

  obj.to_int
end

.to_str(obj) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/polyfill/internal_utils.rb', line 94

def to_str(obj)
  begin
    unless obj.respond_to?(:to_str)
      raise TypeError, "no implicit conversion of #{obj.class} into String"
    end
  rescue NoMethodError
    raise TypeError, 'no implicit conversion of BasicObject into String'
  end

  obj.to_str
end