Sha256: 85252a6808ab8c1dd8f62098a4469d53bb479e07b618504f243bda749d277992

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/class/attribute_accessors'

module Sequential
  module SequentialInclude
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      # Public: Defines ActiveRecord callbacks to set a sequential ID scoped
      # on a specific class.
      #
      # options - The Hash of options for configuration:
      #           :scope    - The Symbol representing the columm on which the
      #                       sequence should be scoped (default: nil)
      #           :column   - The Symbol representing the column that stores the
      #                       sequence (default: :sequential_id)
      #           :start_at - The Integer value at which the sequence should
      #                       start (default: 1)
      #           :skip     - Skips the sequence generation when the lambda
      #                       expression evaluates to nil. Gets passed the
      #                       model object
      #
      # Examples
      #
      #   class Answer < ActiveRecord::Base
      #     belongs_to :question
      #     sequential scope: :question_id
      #   end
      #
      # Returns nothing.
      def sequential(options = {})
        cattr_accessor :sequence_options

        (self.sequence_options ||= []) << options

        before_create :set_sequential_values

        include Sequential::SequentialInclude::InstanceMethods
      end
    end

    module InstanceMethods
      def set_sequential_values
        self.sequence_options.each do |seq_opts|
          Sequential::Generator.new(self, seq_opts).set
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sequential-0.1 lib/sequential/sequential_include.rb
sequential-0.0.3 lib/sequential/sequential_include.rb
sequential-0.0.2 lib/sequential/sequential_include.rb
sequential-0.0.1 lib/sequential/sequential_include.rb