Class: EncodedId::Rails::Coder

Inherits:
Object
  • Object
show all
Defined in:
lib/encoded_id/rails/coder.rb

Overview

Encodes and decodes IDs using the configured encoder and settings.

Instance Method Summary collapse

Constructor Details

#initialize(salt:, id_length:, character_group_size:, separator:, alphabet:, encoder: nil, blocklist: nil, blocklist_mode: nil, blocklist_max_length: nil, downcase_on_decode: nil) ⇒ Coder

Returns a new instance of Coder.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/encoded_id/rails/coder.rb', line 21

def initialize(salt:, id_length:, character_group_size:, separator:, alphabet:, encoder: nil, blocklist: nil, blocklist_mode: nil, blocklist_max_length: nil, downcase_on_decode: nil)
  @salt = salt
  @id_length = id_length
  @character_group_size = character_group_size
  @separator = separator
  @alphabet = alphabet
  config = EncodedId::Rails.configuration
  @encoder = encoder || config.encoder
  @blocklist = blocklist || config.blocklist
  @blocklist_mode = blocklist_mode || config.blocklist_mode
  @blocklist_max_length = blocklist_max_length || config.blocklist_max_length
  @downcase_on_decode = downcase_on_decode.nil? ? config.downcase_on_decode : downcase_on_decode
end

Instance Method Details

#decode(encoded_id) ⇒ Object



41
42
43
44
45
# File 'lib/encoded_id/rails/coder.rb', line 41

def decode(encoded_id)
  coder.decode(encoded_id, downcase: @downcase_on_decode)
rescue EncodedId::EncodedIdFormatError, EncodedId::InvalidInputError
  []
end

#encode(id) ⇒ Object



36
37
38
# File 'lib/encoded_id/rails/coder.rb', line 36

def encode(id)
  coder.encode(id)
end