Module: NeatIds

Defined in:
lib/neat_ids.rb,
lib/neat_ids/engine.rb,
lib/neat_ids/neat_id.rb,
lib/neat_ids/version.rb

Defined Under Namespace

Modules: Attribute, Finder, Rails, ToParam Classes: Engine, Error, NeatId

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.find(neat_id) ⇒ Object



17
18
19
20
21
22
# File 'lib/neat_ids.rb', line 17

def self.find(neat_id)
  prefix, _ = split_id(neat_id)
  models.fetch(prefix).find_by_neat_id(neat_id)
rescue KeyError
  raise Error, "Unable to find model with prefix `#{prefix}`. Available prefixes are: #{models.keys.join(", ")}"
end

.register_prefix(prefix, model:) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/neat_ids.rb', line 30

def self.register_prefix(prefix, model:)
  if (existing_model = NeatIds.models[prefix]) && existing_model.name != model.name
    raise Error, "Prefix #{prefix} already defined for model #{existing_model.name}"
  end

  NeatIds.models[prefix] = model
end

.split_id(neat_id, delimiter = NeatIds.delimiter) ⇒ Object

Splits a prefixed ID into its prefix and ID



25
26
27
28
# File 'lib/neat_ids.rb', line 25

def self.split_id(neat_id, delimiter = NeatIds.delimiter)
  prefix, _, id = neat_id.to_s.rpartition(delimiter)
  [prefix, id]
end