class MongoidAutoIncrement::Incrementor

Public Class Methods

new(options = nil) click to toggle source
# File lib/mongoid_auto_increment/incrementor.rb, line 66
def initialize(options = nil); end

Public Instance Methods

inc(sequence, options, record) click to toggle source
# File lib/mongoid_auto_increment/incrementor.rb, line 68
def inc(sequence, options, record)
  collection = options[:collection] || 'sequences'
  seed = options[:seed].to_i
  step = options[:step] || 1
  scope = resolve_scope(record, options[:scope])

  Sequence.new(sequence, collection, seed, step, scope).inc
end

Private Instance Methods

resolve_scope(record, scope) click to toggle source
# File lib/mongoid_auto_increment/incrementor.rb, line 79
def resolve_scope(record, scope)
  Array(scope).each_with_object({}) do |scope_item, query|
    reflection = record.class.reflect_on_association(scope_item)

    if reflection
      scope_value = record.send(reflection.foreign_key)
      scope_item  = reflection.foreign_key
      query[scope_item] = scope_value
    else
      query[scope_item] = record.read_attribute(scope_item)
    end
  end
end