Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/to_slug_param/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.file_ext(file_name, opts = {}) ⇒ Object



57
58
59
# File 'lib/to_slug_param/string.rb', line 57

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

.file_name(name, opts = {}) ⇒ Object



61
62
63
64
65
# File 'lib/to_slug_param/string.rb', line 61

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

.slugged_filename(name, opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/to_slug_param/string.rb', line 67

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

.slugged_filepath(file_full_path, opts = {}) ⇒ Object



76
77
78
79
# File 'lib/to_slug_param/string.rb', line 76

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

.to_slug_param(str, opts = {}) ⇒ Object



26
27
28
# File 'lib/to_slug_param/string.rb', line 26

def to_slug_param str, opts = {}
  to_smart_slug_param(str, opts)
end

.to_slug_param_base(str, opts = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/to_slug_param/string.rb', line 30

def to_slug_param_base str, opts = {}
  sep = opts.delete(:sep) || '-'
  str = str.gsub(/\-{2,}/, '-').mb_chars
  str = I18n::transliterate(str, opts)
  str = ToSlugParam::basic_parameterize(sep, opts)
  ToSlugParam::parameterize(sep, opts)
end

.to_smart_slug_param(str, opts = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/to_slug_param/string.rb', line 38

def to_smart_slug_param str, opts = {}
  tolerance = opts.delete(:tolerance) || 75

  x = str.to_slug_param_base
  y = str.to_url

  ratio = (x.size.to_f/y.size.to_f)

  # x=0.0/y=0.0 => NaN
  return '' if ratio.nan?
  # x=1.0/y=0.0 => Infinite
  return x if ratio.infinite?
  # x=0.0/y=1.0 => 0.0
  return y if ratio.zero?
  # ((x=5.0/y=3.0)*100).to_i => 166 => x
  # ((x=3.0/y=5.0)*100).to_i => 60  => y
  (ratio*100).to_i > tolerance ? x : y
end

Instance Method Details

#slugged_filename(opts = {}) ⇒ Object



10
11
12
# File 'lib/to_slug_param/string.rb', line 10

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

#slugged_filepath(opts = {}) ⇒ Object



14
15
16
# File 'lib/to_slug_param/string.rb', line 14

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

#to_slug_param(opts = {}) ⇒ Object



2
3
4
# File 'lib/to_slug_param/string.rb', line 2

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

#to_slug_param_base(opts = {}) ⇒ Object



6
7
8
# File 'lib/to_slug_param/string.rb', line 6

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

#to_smart_slug_param(opts = {}) ⇒ Object



18
19
20
# File 'lib/to_slug_param/string.rb', line 18

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