class Mongokit::Formater

Public Instance Methods

format(number, options) click to toggle source
# File lib/mongokit/auto_increment/formater.rb, line 4
def format(number, options)
  @options = options

  build(number)
end

Private Instance Methods

build(number) click to toggle source

gets the next number. it prepends the prefix + counter + sufix ex:

"%Y####BANK"
%Y => prefix (year)
#### => counter (starts with 0001)
BANK => sufix

if we are in 2011, the first model to be saved will get “20110001BANK” the next model to be saved will get “20110002BANK”, “20110003BANK”…

number => is the counter

next_custom_number(1)
=> "20110001BANK"
# File lib/mongokit/auto_increment/formater.rb, line 27
def build(number)
  prefix(@options[:pattern]).to_s +
  counter(@options[:pattern], number).to_s +
  sufix(@options[:pattern]).to_s
end
counter(pattern, n) click to toggle source
# File lib/mongokit/auto_increment/formater.rb, line 38
def counter(pattern, n)
  format_counter(digits_size(pattern), n)
end
digits_size(pattern) click to toggle source
# File lib/mongokit/auto_increment/formater.rb, line 67
def digits_size(pattern)
  symbol = @options[:number_symbol]
  (pattern =~ /[#{symbol}]+/ and $&).length
end
expand_times(pattern) click to toggle source
# File lib/mongokit/auto_increment/formater.rb, line 63
def expand_times(pattern)
  Time.now.strftime(pattern)
end
extract_prefix(pattern) click to toggle source
# File lib/mongokit/auto_increment/formater.rb, line 51
def extract_prefix(pattern)
  # Company#### => Company
  symbol = @options[:number_symbol]
  (pattern =~ /^(\s|\d)*[^#{symbol}]+/ and $&)
end
extract_sufix(pattern) click to toggle source
# File lib/mongokit/auto_increment/formater.rb, line 57
def extract_sufix(pattern)
  # ###Company => Company
  symbol = @options[:number_symbol]
  (pattern =~ /[^#{symbol}]+$/ and $&)
end
format_counter(zeros, value) click to toggle source
# File lib/mongokit/auto_increment/formater.rb, line 47
def format_counter(zeros, value)
  "%0#{zeros}d" % value
end
prefix(pattern) click to toggle source
# File lib/mongokit/auto_increment/formater.rb, line 33
def prefix(pattern)
  prefx = extract_prefix(pattern)
  expand_times(prefx.to_s)
end
sufix(pattern) click to toggle source
# File lib/mongokit/auto_increment/formater.rb, line 42
def sufix(pattern)
  sufx = extract_sufix(pattern)
  expand_times(sufx.to_s)
end