Class: EncodedId::ReversibleId

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

Overview

@rbs!

type encodeableValue = Array[String | Integer] | String | Integer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ ReversibleId

Initialize with a configuration object Defaults to Sqids configuration if called with no arguments



28
29
30
31
32
33
34
35
36
37
# File 'lib/encoded_id/reversible_id.rb', line 28

def initialize(config = nil)
  @config = config || Encoders::SqidsConfiguration.new

  unless @config.is_a?(Encoders::BaseConfiguration)
    raise InvalidConfigurationError, "config must be an instance of Encoders::BaseConfiguration (or nil for default Sqids)"
  end

  @hex_represention_encoder = HexRepresentation.new(@config.hex_digit_encoding_group_size)
  @encoder = create_encoder
end

Instance Attribute Details

#configObject (readonly)

The configuration object for this encoder instance. Returns either an Encoders::HashidConfiguration or Encoders::SqidsConfiguration. Useful for introspecting settings like alphabet, min_length, blocklist, etc.



42
43
44
# File 'lib/encoded_id/reversible_id.rb', line 42

def config
  @config
end

#encoderObject (readonly)

: Encoders::Hashid | Encoders::Sqids



45
46
47
# File 'lib/encoded_id/reversible_id.rb', line 45

def encoder
  @encoder
end

#hex_represention_encoderObject (readonly)

: HexRepresentation



44
45
46
# File 'lib/encoded_id/reversible_id.rb', line 44

def hex_represention_encoder
  @hex_represention_encoder
end

Class Method Details

.hashid(salt:, **options) ⇒ Object

Factory method to create a Hashid-based reversible ID



15
16
17
# File 'lib/encoded_id/reversible_id.rb', line 15

def self.hashid(salt:, **options)
  new(Encoders::HashidConfiguration.new(salt: salt, **options))
end

.sqids(**options) ⇒ Object

Factory method to create a Sqids-based reversible ID (default)



21
22
23
# File 'lib/encoded_id/reversible_id.rb', line 21

def self.sqids(**options)
  new(Encoders::SqidsConfiguration.new(**options))
end

Instance Method Details

#decode(str, downcase: false) ⇒ Object

Decode the hash to original array



67
68
69
70
71
72
73
# File 'lib/encoded_id/reversible_id.rb', line 67

def decode(str, downcase: false)
  raise EncodedIdFormatError, "Max length of input exceeded" if max_length_exceeded?(str)

  encoder.decode(convert_to_hash(str, downcase))
rescue InvalidInputError => error
  raise EncodedIdFormatError, error.message
end

#decode_hex(str, downcase: false) ⇒ Object

Decode hex strings from a hash



77
78
79
80
# File 'lib/encoded_id/reversible_id.rb', line 77

def decode_hex(str, downcase: false)
  integers = encoder.decode(convert_to_hash(str, downcase))
  @hex_represention_encoder.integers_as_hex(integers)
end

#encode(values) ⇒ Object

Encode the input values into a hash



49
50
51
52
53
54
55
56
57
# File 'lib/encoded_id/reversible_id.rb', line 49

def encode(values)
  inputs = prepare_input(values)
  encoded_id = encoder.encode(inputs)
  encoded_id = humanize_length(encoded_id) if @config.split_with && @config.split_at

  raise EncodedIdLengthError if max_length_exceeded?(encoded_id)

  encoded_id
end

#encode_hex(hexs) ⇒ Object

Encode hex strings into a hash



61
62
63
# File 'lib/encoded_id/reversible_id.rb', line 61

def encode_hex(hexs)
  encode(@hex_represention_encoder.hex_as_integers(hexs))
end