Class: CoolId::Registry

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

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



70
71
72
# File 'lib/cool_id.rb', line 70

def initialize
  @prefix_map = {}
end

Instance Method Details

#locate(id) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/cool_id.rb', line 78

def locate(id)
  parsed = parse(id)
  return nil unless parsed

  id_field = CoolId.resolve_cool_id_field(parsed.model_class)
  parsed.model_class.find_by(id_field => id)
end

#parse(id) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/cool_id.rb', line 86

def parse(id)
  prefix, key = id.split(CoolId.separator, 2)
  model_class = @prefix_map[prefix]
  return nil unless model_class
  id_field = CoolId.resolve_cool_id_field(model_class)
  Id.new(key, prefix, id, model_class, id_field)
end

#register(prefix, model_class) ⇒ Object



74
75
76
# File 'lib/cool_id.rb', line 74

def register(prefix, model_class)
  @prefix_map[prefix] = model_class
end