class PrefixedIds::PrefixId
Constants
- TOKEN
Attributes
hashids[R]
prefix[R]
Public Class Methods
new(model, prefix, minimum_length: PrefixedIds.minimum_length, alphabet: PrefixedIds.alphabet, delimiter: PrefixedIds.delimiter, **options)
click to toggle source
# File lib/prefixed_ids/prefix_id.rb, line 7 def initialize(model, prefix, minimum_length: PrefixedIds.minimum_length, alphabet: PrefixedIds.alphabet, delimiter: PrefixedIds.delimiter, **options) @prefix = prefix.to_s @delimiter = delimiter.to_s @hashids = Hashids.new(model.table_name, minimum_length, alphabet) end
Public Instance Methods
decode(id, fallback: false)
click to toggle source
decode returns an array
# File lib/prefixed_ids/prefix_id.rb, line 18 def decode(id, fallback: false) fallback_value = fallback ? id : nil _, id_without_prefix = PrefixedIds.split_id(id, @delimiter) decoded_hashid = @hashids.decode(id_without_prefix) return fallback_value unless valid?(decoded_hashid) decoded_hashid.last || fallback_value end
encode(id)
click to toggle source
# File lib/prefixed_ids/prefix_id.rb, line 13 def encode(id) @prefix + @delimiter + @hashids.encode(TOKEN, id) end
Private Instance Methods
valid?(decoded_hashid)
click to toggle source
# File lib/prefixed_ids/prefix_id.rb, line 29 def valid?(decoded_hashid) decoded_hashid.size == 2 && decoded_hashid.first == TOKEN end