module Mongoid::Autoinc

Include module to allow defining of autoincrementing fields.

@example

class Invoice
  include Mongoid::Document
  include Mongoid::Autoinc

  field :number, type: Integer
  increments :number
end

Constants

AlreadyAssignedError
AutoIncrementsError
VERSION

Public Instance Methods

assign!(field) click to toggle source

Manually assign the next number to the passed autoinc field.

@raise [ Mongoid::Autoinc::AutoIncrementsError ] When `auto: true` is set in the increments call for `field` @raise [ AlreadyAssignedError ] When called more then once.

@return [ Fixnum ] The assigned number

# File lib/autoinc.rb, line 82
def assign!(field)
  options = self.class.incrementing_fields[field]
  fail AutoIncrementsError if options[:auto]
  fail AlreadyAssignedError if send(field).present?
  increment!(field, options)
end
evaluate_scope(scope) click to toggle source

Asserts the validity of the passed scope

@param [ Object ] scope The Symbol or Proc to evaluate

@raise [ ArgumentError ] When scope is not a Symbol or Proc

@return [ Object ] The scope of the autoincrement call

# File lib/autoinc.rb, line 123
def evaluate_scope(scope)
  return send(scope) if scope.is_a? Symbol
  return instance_exec(&scope) if scope.is_a? Proc
  fail ArgumentError, 'scope is not a Symbol or a Proc'
end
evaluate_step(step) click to toggle source

Returns the number to add to the current increment

@param [ Object ] step The Integer to be returned or Proc to be evaluated

@raise [ ArgumentError ] When step is not an Integer or Proc

@return [ Integer ] The number to add to the current increment

# File lib/autoinc.rb, line 137
def evaluate_step(step)
  return step if step.is_a? Integer
  return evaluate_step_proc(step) if step.is_a? Proc
  fail ArgumentError, 'step is not an Integer or a Proc'
end
evaluate_step_proc(step_proc) click to toggle source

Executes a proc and returns its Integer value

@param [ Proc ] step_proc The Proc to call

@raise [ ArgumentError ] When step_proc does not evaluate to Integer

@return [ Integer ] The number to add to the current increment

# File lib/autoinc.rb, line 150
def evaluate_step_proc(step_proc)
  result = instance_exec(&step_proc)
  return result if result.is_a? Integer
  fail 'step Proc does not evaluate to an Integer'
end
increment!(field, options) click to toggle source

Set autoincrement value for the passed autoincrement field, using the passed options

@param [ Symbol ] field Field to set the autoincrement value for. @param [ Hash ] options Options to pass through to the serializer.

@return [ true ] The value of `write_attribute`

# File lib/autoinc.rb, line 105
def increment!(field, options)
  options = options.dup
  model_name = (options.delete(:model_name) || self.class.model_name).to_s
  options[:scope] = evaluate_scope(options[:scope]) if options[:scope]
  options[:step] = evaluate_step(options[:step]) if options[:step]
  write_attribute(
      field.to_sym,
      Mongoid::Autoinc::Incrementor.new(model_name, field, options).inc
  )
end
update_auto_increments() click to toggle source

Sets autoincrement values for all autoincrement fields.

@return [ true ]

# File lib/autoinc.rb, line 92
def update_auto_increments
  self.class.incrementing_fields.each do |field, options|
    increment!(field, options) if options[:auto]
  end && true
end