Module: QuickShort

Defined in:
lib/quick_short.rb,
lib/quick_short/version.rb,
lib/quick_short/short_id.rb,
lib/quick_short/has_short_id.rb,
lib/quick_short/no_such_prefix.rb,
lib/quick_short/duplicate_prefix.rb

Defined Under Namespace

Modules: HasShortId, ShortId Classes: DuplicatePrefix, NoSuchPrefix

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.find(short_id) ⇒ Object

A convenient alias for #find which locates the class and calls find(id)

Parameters:

  • short_id (String)
    • the short id to look up



21
22
23
24
25
# File 'lib/quick_short.rb', line 21

def find(short_id)
  lookup(short_id) do |klass, id|
    klass.find id
  end
end

.find_by_short_id(short_id) ⇒ Object

A convenient alias for #find_by_id which locates the class and calls find_by_id(id)

Parameters:

  • short_id (String)
    • the short id to look up



30
31
32
33
34
# File 'lib/quick_short.rb', line 30

def find_by_short_id(short_id)
  lookup(short_id) do |klass, id|
    klass.find_by_id id
  end
end

.lookup(short_id) ⇒ Object

Look up a short id, and yield [klass, id] to a block, or return as array

Parameters:

  • short_id (String)
    • the short id to look up



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/quick_short.rb', line 38

def lookup(short_id)
  prefix, short_id = short_id.split('-', 2)
  if klass = short_id_prefixes[prefix]
    id = QuickShort::ShortId.decode(short_id)
    if block_given?
      yield klass, id # return result
    else
      [klass, id] # otherwise, return [klass, id]
    end
  else
    raise NoSuchPrefix.new(prefix)
  end
end

.short_id_prefixesHash

The current mapping of short_id_prefixes

Returns:

  • (Hash)

    the mapping



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

def short_id_prefixes
  @prefixes ||= {}
end