Sha256: 721afabf6e27f43028efa2e0529939982255f23680984d8eed2ad7e0e8d41b13

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module Tagomatic

  class TagNormalizer

    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

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

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

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

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

    def get_resulting_value
      @parts.join(' ')
    end

    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}]+$/

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tagomatic-0.1.9 lib/tagomatic/tag_normalizer.rb
tagomatic-0.1.8 lib/tagomatic/tag_normalizer.rb