Class: LCBO::CrawlKit::TitleCaseHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/lcbo/crawlkit/titlecase_helper.rb

Constant Summary collapse

SMALL_WORDS =
%w[
  a an and as at but by en for
  if in of del de on or the to
  v v. via vs vs.
]
ACRONYMS =
%w[
  vqa vsop xo nq5 vs xxx igt
  xiii xi xoxo srl bdb cvbg
  ocb lcbo i ii iii
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



19
20
21
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 19

def input
  @input
end

Class Method Details

.[](string) ⇒ Object



21
22
23
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 21

def self.[](string)
  titlecase(string)
end

.capitalize(string) ⇒ Object



33
34
35
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 33

def self.capitalize(string)
  UnicodeUtils.titlecase(string)
end

.downcase(string) ⇒ Object



29
30
31
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 29

def self.downcase(string)
  UnicodeUtils.simple_downcase(string)
end

.titlecase(string) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 37

def self.titlecase(string)
  preclean = lambda { |s|
    # Strip bracketed stuff and trailing junk: Product (Junk)**
    s.gsub(/\(.+\Z/, '').gsub(/\*+\Z/, '').strip
  }
  count = 0 # Ewwww
  capitalize(preclean.(string)).split.map do |word|
    count += 1
    case word.downcase
    when /[\w]\/[\w]/ # words with slashes
      word.split('/').map { |w| capitalize(w) }.join(' / ')
    when /[\w]\&[\w]/ # words with &, like E&J
      word.split('&').map { |w| capitalize(w) }.join('&')
    when /[\w]\-[\w]/ # words with dashes, like "Super-Cool"
      word.split('-').map { |w| capitalize(w) }.join('-')
    when /[\w]\.[\w]/ # words with dots, like "A.B.C."
      word.split('.').map { |w| upcase(w) }.join('.') + '.'
    when *SMALL_WORDS
      1 == count ? word : word.downcase
    when *ACRONYMS
      word.upcase
    else
      word
    end
  end.join(' ').gsub(/(['’])S\b/, '\1s')
end

.upcase(string) ⇒ Object



25
26
27
# File 'lib/lcbo/crawlkit/titlecase_helper.rb', line 25

def self.upcase(string)
  UnicodeUtils.simple_upcase(string)
end