Class: Paxx::NameNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/paxx/names/name_normalizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_name) ⇒ NameNormalizer

Returns a new instance of NameNormalizer.



7
8
9
10
# File 'lib/paxx/names/name_normalizer.rb', line 7

def initialize src_name
  @name = src_name
  @first_name, @last_name = splitt
end

Instance Attribute Details

#first_nameObject (readonly)

Returns the value of attribute first_name.



5
6
7
# File 'lib/paxx/names/name_normalizer.rb', line 5

def first_name
  @first_name
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



5
6
7
# File 'lib/paxx/names/name_normalizer.rb', line 5

def full_name
  @full_name
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



5
6
7
# File 'lib/paxx/names/name_normalizer.rb', line 5

def last_name
  @last_name
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/paxx/names/name_normalizer.rb', line 5

def name
  @name
end

Instance Method Details

#as_id(txt = name) ⇒ Object



48
49
50
# File 'lib/paxx/names/name_normalizer.rb', line 48

def as_id(txt=name)
  subst_norwegian_chars(txt.delete(" ").delete("-")).downcase if txt
end

#as_short_refObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/paxx/names/name_normalizer.rb', line 64

def as_short_ref
  counter = 0
  unique = !block_given?
  xref =  ""
  start = camelize(as_id(first_name))

  begin
    shortref = compose_short_ref start,xref

    if !unique
      unique = yield shortref
      if !unique
        counter += 1
        xref =  last_part(camelize(as_id(last_name)),counter)
      end
    end
  end while !unique  && counter < 100
  shortref
end

#as_slug(txt = name) ⇒ Object



84
85
86
# File 'lib/paxx/names/name_normalizer.rb', line 84

def as_slug(txt=name)
  txt.to_s.to_slug.normalize(transliterations: :norwegian).to_s
end

#camelize(txt = name) ⇒ Object



31
32
33
# File 'lib/paxx/names/name_normalizer.rb', line 31

def camelize(txt=name)
  txt.split(/[^a-zøæåØÆÅ0-9]/i).map { |w| w.capitalize }.join if txt
end

#compose_short_ref(first, last) ⇒ Object



52
53
54
# File 'lib/paxx/names/name_normalizer.rb', line 52

def compose_short_ref first,last
  "#{first}#{last}"
end

#last_part(name, counter) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/paxx/names/name_normalizer.rb', line 56

def last_part name,counter
  if name && counter < name.length
    name[0..counter]
  else
    "#{name}#{counter-name.length+1}"
  end
end

#normalize(txt = name) ⇒ Object



16
17
18
# File 'lib/paxx/names/name_normalizer.rb', line 16

def normalize(txt=name)
  txt.to_s.gsub("\n", ' ').squeeze(' ').strip
end

#splittObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/paxx/names/name_normalizer.rb', line 35

def splitt
  words = name.to_s.split()
  if words.count > 1
    last_name = words.last
    words.pop
    first_name = words.join(" ")
  else
    first_name = name
    last_name=""
  end
  [camelize(first_name), camelize(last_name)]
end

#subst_norwegian_chars(txt) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/paxx/names/name_normalizer.rb', line 21

def subst_norwegian_chars(txt)
  [["æ", "ae"], ["ø", "oe"], ["å", "aa"]].each do |int|
    txt = txt.gsub(int[0], int[1])
  end
  [["Æ", "AE"], ["Ø", "OE"], ["Å", "AA"]].each do |int|
    txt = txt.gsub(int[0], int[1])
  end
  txt
end

#validObject



12
13
14
# File 'lib/paxx/names/name_normalizer.rb', line 12

def valid
  !(name.to_s.strip.length == 0)
end