Sha256: 2924b8a20558ecb64d3170fdbddd3b8acccd7204ca2476f870ae881769a159a1
Contents?: true
Size: 1.04 KB
Versions: 3
Compression:
Stored size: 1.04 KB
Contents
module Sequential class Generator attr_reader :record, :scope, :column, :start_at, :skip def initialize(record, options = {}) @record = record @scope = options[:scope] @column = (options[:column] || :sequential_id).to_sym @start_at = options[:start_at] || 1 @skip = options[:skip] end def set unless id_set? || skip? where_opts = { model: record.class.name, column: column } where_opts.merge!( scope: scope.to_s, scope_value: record.send(scope.to_sym).to_s, ) if scope sequence = Sequential::Sequence.where(where_opts). first_or_create(value: start_at - 1) sequence.with_lock do sequence.value += 1 record.send(:"#{column}=", sequence.value) sequence.save end end end def id_set? !record.send(column).nil? end def skip? skip && skip.call(record) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
sequential-0.2 | lib/sequential/generator.rb |
sequential-0.1 | lib/sequential/generator.rb |
sequential-0.0.3 | lib/sequential/generator.rb |