Sha256: 33137f495a2d17b5c2d48bb40c0942175332e215ba5f84452ef2882c3e11e2bc

Contents?: true

Size: 996 Bytes

Versions: 2

Compression:

Stored size: 996 Bytes

Contents

module MemoryModel
  class Base
    module AutoIncrement

      extend ActiveSupport::Concern

      included do
        before_create(:auto_increment_fields!)
      end

      private

      def auto_increment_fields!
        fields.select { |field| field.options[:auto_increment] === true }.each do |field|
          write_attribute(field.name, self.class.auto_increment_for!(field.name))
        end
      end

      def reset_incremented_fields!
        fields.select { |field| field.options[:auto_increment] === true }.each do |field|
          clear_attribute(field.name)
        end
      end

      module ClassMethods

        def auto_increment_for!(field)
          fields[field].increment!
        end

      end

    end

    module Fields
      class Field

        def increment!
          raise ArguementError, "#{name} is not incrementable" unless options[:auto_increment] === true
          @incrementor ||= 0
          @incrementor += 1
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
memory_model-1.0.0 lib/memory_model/base/auto_increment.rb
memory_model-0.1.0 lib/memory_model/base/auto_increment.rb