Sha256: a2aafc8f76fe6d52e9c2eabe2e8cec5e46058131cd14ca26f8d788c330b1bb6e

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require "rails-i18n"
require "the_string_to_slug/version"

module TheStringToSlug; end

class String
  def to_slug_param opts = {}
    self.class.to_slug_param(self, opts)
  end

  def slugged_filename opts = {}
    self.class.slugged_filename(self, opts)
  end

  def slugged_file opts = {}
    self.class.slugged_file(self, opts)
  end

  # -----------------------------------
  # Self methods
  # -----------------------------------
  class << self
    def to_slug_param str, opts = {}
      delimiter = opts.delete(:delimiter) || '-'
      str = str.gsub(/\-{2,}/, '-').mb_chars
      str = I18n::transliterate(str, opts)
        .gsub('_', delimiter)
        .gsub('-', delimiter)
        .parameterize(delimiter)
        .downcase.to_s
    end

    def file_ext file_name, opts = {}
      File.extname(file_name)[1..-1].to_s.to_slug_param opts
    end

    def file_name name, opts = {}
      name = File.basename name
      ext  = File.extname  name
      File.basename(name, ext).to_s.to_slug_param opts
    end

    def slugged_filename name, opts = {}
      name  = File.basename  name
      fname = self.file_name name, opts
      ext   = self.file_ext  name, opts

      return fname if ext.blank?
      [fname, ext].join('.')
    end

    def slugged_file file_full_path, opts = {}
      fname = slugged_filename file_full_path, opts
      file_full_path.split('/')[0...-1].push(fname).join '/'
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
the_string_to_slug-1.1 lib/the_string_to_slug.rb