Class: HumanizedId::Humanizer

Inherits:
Object
  • Object
show all
Defined in:
lib/humanized_id/humanizer.rb

Constant Summary collapse

SOURCE_CHARSET =
%w(0123456789abcdefghijklmnopqrstuvwxyz).freeze

Instance Method Summary collapse

Constructor Details

#initialize(id:, min_length: nil, prefix: '') ⇒ Humanizer

Returns a new instance of Humanizer.



6
7
8
9
10
11
12
# File 'lib/humanized_id/humanizer.rb', line 6

def initialize(id:, min_length: nil, prefix: '')
  @id               = id
  @min_length       = min_length
  @prefix           = prefix.nil? ? '' : prefix
  @target_charset   = HumanizedId::CHARACTERSET.join('')
  @source_charset   = SOURCE_CHARSET.join('')
end

Instance Method Details

#generate_humanized_idObject



14
15
16
17
18
19
# File 'lib/humanized_id/humanizer.rb', line 14

def generate_humanized_id
  new_id = convert_to_target_base id: @id
  new_id = convert_to_target_charset id: new_id
  new_id = resize id: new_id if @min_length
  "#{@prefix}#{new_id}"
end