Class: Tagomatic::TagNormalizer

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

Constant Summary collapse

NORMALIZER_SPLIT_REGEX =
/[ _-]+/
NORMALIZER_LEFT_LITERALS =
Regexp.escape('-([.')
NORMALIZER_RIGHT_LITERALS =
Regexp.escape('-)].')
NORMALIZER_STRIP_LEFT =
/^[#{NORMALIZER_LEFT_LITERALS}]+\s*/
NORMALIZER_STRIP_RIGHT =
/\s*[#{NORMALIZER_RIGHT_LITERALS}]+$/

Instance Method Summary collapse

Instance Method Details

#capitalize_wordsObject



32
33
34
# File 'lib/tagomatic/tag_normalizer.rb', line 32

def capitalize_words
  @parts.map! { |part| part.capitalize }
end

#drop_empty_wordsObject



28
29
30
# File 'lib/tagomatic/tag_normalizer.rb', line 28

def drop_empty_words
  @parts = @parts.select { |part| not part.empty? }
end

#get_resulting_valueObject



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

def get_resulting_value
  @parts.join(' ')
end

#process(tags_hash) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/tagomatic/tag_normalizer.rb', line 5

def process(tags_hash)
  normalized = Hash.new
  tags_hash.each do |tag, value|
    next unless value

    split_by_spaces_and_underscores value
    strip_dashes_and_brackets
    drop_empty_words
    capitalize_words

    normalized[tag] = get_resulting_value
  end
  normalized
end

#split_by_spaces_and_underscores(value) ⇒ Object



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

def split_by_spaces_and_underscores(value)
  @parts = value.split(NORMALIZER_SPLIT_REGEX)
end

#strip_dashes_and_bracketsObject



24
25
26
# File 'lib/tagomatic/tag_normalizer.rb', line 24

def strip_dashes_and_brackets
  @parts.map! { |part| part.sub(NORMALIZER_STRIP_LEFT, '').sub(NORMALIZER_STRIP_RIGHT, '') }
end