Sha256: 47a9c20e12fcec1977c7a52bd6f95914aa8927ad328ad558c62b473c8788809c

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

module Esvg
  module Utils
    def dasherize(input)
      input.gsub(/[\W,_]/, '-').sub(/^-/,'').gsub(/-{2,}/, '-')
    end

    def sub_path(root, path)
      path.sub(File.join(root,''),'')
    end

    def symbolize_keys(hash)
      h = {}
      hash.each {|k,v| h[k.to_sym] = v }
      h
    end

    def attributes(hash)
      att = []
      hash.each do |key, value|
        att << %Q{#{key.to_s.gsub(/_/,'-')}="#{value}"} unless value.nil?
      end
      att.join(' ')
    end

    def sort(hash)
      sorted = {}
      hash.sort.each do |h|
        sorted[h.first] = h.last
      end
      sorted
    end

    def compress(file)
      mtime = File.mtime(file)
      gz_file = "#{file}.gz"

      return if (File.exist?(gz_file) && File.mtime(gz_file) >= mtime)

      File.open(gz_file, "wb") do |dest|
        gz = ::Zlib::GzipWriter.new(dest, Zlib::BEST_COMPRESSION)
        gz.mtime = mtime.to_i
        IO.copy_stream(open(file), gz)
        gz.close
      end

      File.utime(mtime, mtime, gz_file)

      gz_file
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
esvg-4.3.9 lib/esvg/utils.rb
esvg-4.3.8 lib/esvg/utils.rb
esvg-4.3.7 lib/esvg/utils.rb
esvg-4.3.6 lib/esvg/utils.rb
esvg-4.3.5 lib/esvg/utils.rb
esvg-4.3.4 lib/esvg/utils.rb