Class: PrettyId::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_id/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Generator

Returns a new instance of Generator.



5
6
7
# File 'lib/pretty_id/generator.rb', line 5

def initialize(record)
  @record = record
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



3
4
5
# File 'lib/pretty_id/generator.rb', line 3

def record
  @record
end

Instance Method Details

#idObject



9
10
11
# File 'lib/pretty_id/generator.rb', line 9

def id
  "#{prefix}#{separator}#{SecRandom.alphanumeric(length)}"
end

#lengthObject



13
14
15
# File 'lib/pretty_id/generator.rb', line 13

def length
  12
end

#prefixObject



17
18
19
20
21
22
23
# File 'lib/pretty_id/generator.rb', line 17

def prefix
  if record.class.id_prefix.is_a? Proc
    record.class.id_prefix.call(record)
  else
    record.class.id_prefix
  end
end

#separatorObject



25
26
27
# File 'lib/pretty_id/generator.rb', line 25

def separator
  record.class.id_separator
end