class AssetBomRemoval::BomRemover

Attributes

string[R]

Public Class Methods

new(string) click to toggle source
# File lib/asset_bom_removal/bom_remover.rb, line 7
def initialize(string)
  @string = string
end
remove_bom(string) click to toggle source
# File lib/asset_bom_removal/bom_remover.rb, line 3
def self.remove_bom(string)
  new(string).remove_bom
end

Public Instance Methods

remove_bom() click to toggle source
# File lib/asset_bom_removal/bom_remover.rb, line 11
def remove_bom
  if string_starts_with_utf8_bom?
    string.dup.force_encoding('UTF-8').tap do |utf8_string|
      utf8_string.slice!(0)
    end
  else
    string
  end
end

Private Instance Methods

string_starts_with_utf8_bom?() click to toggle source
# File lib/asset_bom_removal/bom_remover.rb, line 25
def string_starts_with_utf8_bom?
  with_encoding('UTF-8') do |utf8_string|
    utf8_string[0] == "\xEF\xBB\xBF"
  end
end
with_encoding(encoding) { |force_encoding)| ... } click to toggle source
# File lib/asset_bom_removal/bom_remover.rb, line 31
def with_encoding(encoding)
  old_encoding = string.encoding
  begin
    return (yield string.force_encoding(encoding))
  ensure
    string.force_encoding(old_encoding)
  end
end