Class: SaneTitle::Sanifier

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

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ Sanifier

Returns a new instance of Sanifier.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
# File 'lib/sanetitle.rb', line 7

def initialize(title)
  raise ArgumentError.new('Invalid string.') unless (is_string?(title) and empty_string?(title))
  step1 = title
  step2 = to_lower(step1)
  step3 = remove_special_chars(step2)
  step4 = spaces_to_underlines(step3)
  @@title_str = step4
end

Instance Method Details

#empty_string?(title) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sanetitle.rb', line 60

def empty_string?(title)
  title.length > 0
end

#is_string?(title) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/sanetitle.rb', line 56

def is_string?(title)
  title.is_a? String
end

#remove_special_chars(str) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sanetitle.rb', line 24

def remove_special_chars(str)
  s1 = str.gsub(/[á|à|ã|ä|â]/,"a")
  s1 = s1.gsub(/[é|è|ẽ|ë|ê]/,"e")
  s1 = s1.gsub(/[í|ì|ĩ|ï|î]/,"i")
  s1 = s1.gsub(/[ó|ò|õ|ö|ô]/,"o")
  s1 = s1.gsub(/[ú|ù|ũ|ü|û]/,"u")
  s1 = s1.gsub(/[ñ]/,"n")
  s1 = s1.gsub(/[ç]/,"c")
  s1 = s1.gsub(/[\+|\=|\_|\-|\(|\)|\*|\%|\&|\$|\#|\@|\!|\?|\/|\:|\;|\.|\,|\>|\<]/,"")
  s1.gsub(/\ \ /," ")
end

#resultObject



36
37
38
# File 'lib/sanetitle.rb', line 36

def result
  @@title_str
end

#result_imp(lim, html, tstamp = false) ⇒ Object



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

def result_imp(lim,html,tstamp = false)
  resimp = result
  resimp = resimp[0,lim]
  if (html) then
    resimp = resimp + '.html'
  end
  if (tstamp) then
    dt = DateTime.now
    yyyy = dt.year.to_s 
    mm = (dt.month < 10) ? '0' + dt.month.to_s : dt.month.to_s
    dd = (dt.day < 10) ? '0' + dt.day.to_s : dt.day.to_s 
    resimp = "#{yyyy}-#{mm}-#{dd}-#{resimp}"
  end
  resimp
end

#spaces_to_underlines(str) ⇒ Object



20
21
22
# File 'lib/sanetitle.rb', line 20

def spaces_to_underlines(str)
  str.gsub(/\ /,"-")
end

#to_lower(str) ⇒ Object



16
17
18
# File 'lib/sanetitle.rb', line 16

def to_lower(str)
  str.downcase
end