Module: Mongoid::Autoinc

Extended by:
ActiveSupport::Concern
Defined in:
lib/autoinc.rb,
lib/autoinc/version.rb,
lib/autoinc/incrementor.rb

Overview

Include module to allow defining of autoincrementing fields.

Examples:

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

  field :number, type: Integer
  increments :number
end

Defined Under Namespace

Modules: ClassMethods Classes: Incrementor

Constant Summary collapse

AlreadyAssignedError =
Class.new(StandardError)
AutoIncrementsError =
Class.new(StandardError)
VERSION =
'6.0.3'.freeze

Instance Method Summary collapse

Instance Method Details

#assign!(field) ⇒ Fixnum

Manually assign the next number to the passed autoinc field.

in the increments call for ‘field`

Returns:

  • (Fixnum)

    The assigned number

Raises:



82
83
84
85
86
87
# 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) ⇒ Object

Asserts the validity of the passed scope

Parameters:

  • scope (Object)

    The Symbol or Proc to evaluate

Returns:

  • (Object)

    The scope of the autoincrement call

Raises:

  • (ArgumentError)

    When scope is not a Symbol or Proc



123
124
125
126
127
# 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) ⇒ Integer

Returns the number to add to the current increment

or Proc to be evaluated

Parameters:

  • step (Object)

    The Integer to be returned

Returns:

  • (Integer)

    The number to add to the current increment

Raises:

  • (ArgumentError)

    When step is not an Integer or Proc



137
138
139
140
141
# 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) ⇒ Integer

Executes a proc and returns its Integer value

Parameters:

  • step_proc (Proc)

    The Proc to call

Returns:

  • (Integer)

    The number to add to the current increment

Raises:

  • (ArgumentError)

    When step_proc does not evaluate to Integer



150
151
152
153
154
# 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) ⇒ true

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

Parameters:

  • field (Symbol)

    Field to set the autoincrement value for.

  • options (Hash)

    Options to pass through to the serializer.

Returns:

  • (true)

    The value of ‘write_attribute`



105
106
107
108
109
110
111
112
113
114
# 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_incrementstrue

Sets autoincrement values for all autoincrement fields.

Returns:

  • (true)


92
93
94
95
96
# 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