Module: Dapp::Dapp::Slug

Included in:
Dapp::Dapp
Defined in:
lib/dapp/dapp/slug.rb

Constant Summary collapse

SLUG_SEPARATOR =
'-'.freeze
SLUG_V2_LIMIT_LENGTH =
53

Instance Method Summary collapse

Instance Method Details

#consistent_uniq_slug_regObject



27
28
29
# File 'lib/dapp/dapp/slug.rb', line 27

def consistent_uniq_slug_reg
  /(?!-)((-?[a-z0-9]+)+)(?<!-)/
end

#consistent_uniq_slugify(s) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dapp/dapp/slug.rb', line 7

def consistent_uniq_slugify(s)
  return s unless should_be_slugged?(s)
  consistent_uniq_slug_reg =~ s.tr('/', '-').slugify.squeeze('--')
  consistent_uniq_slug = Regexp.last_match(1)
  murmur_hash = MurmurHash3::V32.str_hexdigest(s)
  [].tap do |slug|
    slug << begin
      unless consistent_uniq_slug.nil?
        index = ENV['DAPP_SLUG_V2'] ? SLUG_V2_LIMIT_LENGTH - murmur_hash.length - SLUG_SEPARATOR.length - 1 : -1
        consistent_uniq_slug[0..index]
      end
    end
    slug << murmur_hash
  end.compact.join(SLUG_SEPARATOR)
end

#should_be_slugged?(s) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/dapp/dapp/slug.rb', line 23

def should_be_slugged?(s)
  !(/^#{consistent_uniq_slug_reg}$/ =~ s)
end