Module: ProcessIt::Utils
- Defined in:
- lib/processit/utils.rb
Overview
‘Utils`, we didn’t know where else to put it!
Constant Summary collapse
- UTF8_BOM_PATTERN =
Define UTF-8 and UTF-16 BOM pattern matchers. Avoid using a Regexp literal to prevent syntax errors in other interpreters.
Regexp.new("\\A\\xEF\\xBB\\xBF")
- UTF16_BOM_PATTERN =
Regexp.new("\\A(\\xFE\\xFF|\\xFF\\xFE)")
Class Method Summary collapse
-
.normalize_extension(extension) ⇒ Object
Prepends a leading “.” to an extension if its missing.
- .read_unicode(pathname) ⇒ Object
Class Method Details
.normalize_extension(extension) ⇒ Object
Prepends a leading “.” to an extension if its missing.
normalize_extension("js")
# => ".js"
normalize_extension(".css")
# => ".css"
60 61 62 63 64 65 66 67 |
# File 'lib/processit/utils.rb', line 60 def self.normalize_extension(extension) extension = extension.to_s if extension[/^\./] extension else ".#{extension}" end end |
.read_unicode(pathname) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/processit/utils.rb', line 11 def self.read_unicode(pathname, external_encoding = Encoding.default_external) File.open(pathname, "r:#{external_encoding}") do |f| f.read.tap do |data| # Eager validate the file's encoding. In most cases we # expect it to be UTF-8 unless `default_external` is set to # something else. An error is usually raised if the file is # saved as UTF-16 when we expected UTF-8. if !data.valid_encoding? raise EncodingError, "#{pathname} has a invalid " + "#{data.encoding} byte sequence" # If the file is UTF-8 and theres a BOM, strip it for safe concatenation. elsif data.encoding.name == "UTF-8" && data =~ UTF8_BOM_PATTERN data.sub!(UTF8_BOM_PATTERN, "") end end end end |