Module: Polyfill::InternalUtils
- Defined in:
- lib/polyfill/internal_utils.rb
Overview
rubocop:disable Metrics/ModuleLength
Class Method Summary collapse
- .create_module {|mod| ... } ⇒ Object
- .current_ruby_version ⇒ Object
- .ignore_warnings ⇒ Object
- .keep_only_these_methods!(mod, whitelist) ⇒ Object
- .methods_to_keep(modules, methods, lead_symbol, module_name) ⇒ Object
- .modules_to_use(module_name, versions) ⇒ Object
- .polyfill_versions_to_use(desired_version = nil) ⇒ Object
- .to_f(obj) ⇒ Object
- .to_hash(obj) ⇒ Object
- .to_int(obj) ⇒ Object
- .to_str(obj) ⇒ Object
Class Method Details
.create_module {|mod| ... } ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/polyfill/internal_utils.rb', line 86 def create_module mod = ::Module.new yield(mod) Polyfill::Module.const_set("M#{mod.object_id}", mod) end |
.current_ruby_version ⇒ Object
12 13 14 |
# File 'lib/polyfill/internal_utils.rb', line 12 def current_ruby_version @current_ruby_version ||= RUBY_VERSION[/\A(\d+\.\d+)/, 1] end |
.ignore_warnings ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/polyfill/internal_utils.rb', line 17 def ignore_warnings orig = $VERBOSE $VERBOSE = nil yield $VERBOSE = orig end |
.keep_only_these_methods!(mod, whitelist) ⇒ Object
41 42 43 44 45 |
# File 'lib/polyfill/internal_utils.rb', line 41 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
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/polyfill/internal_utils.rb', line 74 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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/polyfill/internal_utils.rb', line 48 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
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/polyfill/internal_utils.rb', line 27 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
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/polyfill/internal_utils.rb', line 121 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
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/polyfill/internal_utils.rb', line 134 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
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/polyfill/internal_utils.rb', line 108 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
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/polyfill/internal_utils.rb', line 95 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 |