Module: IdCode::ClassMethods

Defined in:
app/models/id_code.rb

Instance Method Summary collapse

Instance Method Details

#code_to_id(code) ⇒ Object

exact inverse of id_to_code



22
23
24
# File 'app/models/id_code.rb', line 22

def code_to_id(code)
  (code.to_s.tr('a-z', '0-9a-p').to_i(26) - pad) / factor
end

#factorObject

Determines spacing between consecutive referral codes



6
7
8
# File 'app/models/id_code.rb', line 6

def factor
  9
end

#find_by_id_code(code) ⇒ Object



26
27
28
# File 'app/models/id_code.rb', line 26

def find_by_id_code(code)
  find(code_to_id(code))
end

#id_to_code(id) ⇒ Object

Use base 26 here to avoid numeric codes (We want codes to be clearly distinguishable from ids)



17
18
19
# File 'app/models/id_code.rb', line 17

def id_to_code(id)
  (id.to_i * factor + pad).to_s(26).tr('0-9a-p', 'a-z')
end

#padObject

determines minimum size of a referal code



11
12
13
# File 'app/models/id_code.rb', line 11

def pad
  26 ** 3 # insure at least four digits
end