Module: HMap::Utils
- Defined in:
- lib/hmap/helper/utils.rb
Overview
A collection of utility functions used throughout cocoapods-hmap.
Class Method Summary collapse
-
.effective_platform_name(symbolic_name) ⇒ String
Converts the symbolic name of a platform to a string name suitable to be presented to the user.
- .effective_platforms_names(platforms) ⇒ Object
- .file_symlink_to(path, filepath) ⇒ Object
- .hash_set_value(hash, *args) ⇒ Object
- .index_of_range(num, range) ⇒ Object
- .magic?(magic) ⇒ Boolean
- .next_power_of_two(num) ⇒ Object
- .power_of_two?(num) ⇒ Boolean
- .safe_encode(string, target_encoding) ⇒ Object
- .specialize_format(format, swapped) ⇒ Object
- .string_downcase_hash(str) ⇒ Object
- .swapped_magic?(magic, version) ⇒ Boolean
- .update_changed_file(path, contents) ⇒ Object
Class Method Details
.effective_platform_name(symbolic_name) ⇒ String
Converts the symbolic name of a platform to a string name suitable to be presented to the user.
15 16 17 18 19 20 21 22 23 |
# File 'lib/hmap/helper/utils.rb', line 15 def self.effective_platform_name(symbolic_name) case symbolic_name when :ios then %w[iphoneos iphonesimulator] when :osx then %w[macosx] when :watchos then %w[watchos watchsimulator] when :tvos then %w[appletvos appletvsimulator] else [] end end |
.effective_platforms_names(platforms) ⇒ Object
25 26 27 |
# File 'lib/hmap/helper/utils.rb', line 25 def self.effective_platforms_names(platforms) platforms.flat_map { |name| effective_platform_name(name) }.compact.uniq end |
.file_symlink_to(path, filepath) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/hmap/helper/utils.rb', line 74 def self.file_symlink_to(path, filepath) return unless path.exist? filepath.dirname.mkpath unless filepath.exist? if Gem.win_platform? FileUtils.ln(path, filepath, force: true) else FileUtils.ln_sf(path, filepath) end end |
.hash_set_value(hash, *args) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/hmap/helper/utils.rb', line 48 def self.hash_set_value(hash, *args) args.each do |arg| hash.merge(arg) end hash end |
.index_of_range(num, range) ⇒ Object
29 30 31 32 |
# File 'lib/hmap/helper/utils.rb', line 29 def self.index_of_range(num, range) num &= range - 1 num end |
.magic?(magic) ⇒ Boolean
90 91 92 |
# File 'lib/hmap/helper/utils.rb', line 90 def self.magic?(magic) magic.eql?(HEADER_CONST[:HMAP_SWAPPED_MAGIC]) || magic.eql?(HEADER_CONST[:HMAP_HEADER_MAGIC_NUMBER]) end |
.next_power_of_two(num) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/hmap/helper/utils.rb', line 38 def self.next_power_of_two(num) num |= (num >> 1) num |= (num >> 2) num |= (num >> 4) num |= (num >> 8) num |= (num >> 16) num |= (num >> 32) num + 1 end |
.power_of_two?(num) ⇒ Boolean
34 35 36 |
# File 'lib/hmap/helper/utils.rb', line 34 def self.power_of_two?(num) num != 0 && (num & (num - 1)).zero? end |
.safe_encode(string, target_encoding) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/hmap/helper/utils.rb', line 94 def self.safe_encode(string, target_encoding) string.encode(target_encoding) rescue Encoding::InvalidByteSequenceError string.force_encoding(target_encoding) rescue Encoding::UndefinedConversionError string.encode(target_encoding, fallback: lambda { |c| c.force_encoding(target_encoding) }) end |
.specialize_format(format, swapped) ⇒ Object
55 56 57 58 |
# File 'lib/hmap/helper/utils.rb', line 55 def self.specialize_format(format, swapped) modifier = swapped ? '<' : '>' format.tr('=', modifier) end |
.string_downcase_hash(str) ⇒ Object
60 61 62 |
# File 'lib/hmap/helper/utils.rb', line 60 def self.string_downcase_hash(str) str.downcase.bytes.inject(:+) * 13 end |
.swapped_magic?(magic, version) ⇒ Boolean
86 87 88 |
# File 'lib/hmap/helper/utils.rb', line 86 def self.swapped_magic?(magic, version) magic.eql?(HEADER_CONST[:HMAP_SWAPPED_MAGIC]) && version.eql?(HEADER_CONST[:HMAP_SWAPPED_VERSION]) end |
.update_changed_file(path, contents) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/hmap/helper/utils.rb', line 64 def self.update_changed_file(path, contents) if path.exist? content_stream = StringIO.new(contents) identical = File.open(path, 'rb') { |f| FileUtils.compare_stream(f, content_stream) } return if identical end path.dirname.mkpath File.open(path, 'w') { |f| f.write(contents) } end |