Sha256: 47b5c2d11be9bc1a601fcf6e7c70b0d7c70b4dd8fa919d36a58d5de02b2ba5bf

Contents?: true

Size: 552 Bytes

Versions: 1

Compression:

Stored size: 552 Bytes

Contents

require 'digest'

module AxleAttributes
  module Segmented
    extend ActiveSupport::Concern

    included do
      class_attribute :max_segment
      self.max_segment = 1024

      before_create do
        regenerate_segment_id unless segment_id
      end
    end

    module ClassMethods
      def segment_range
        0..(max_segment-1)
      end
    end

    def generate_segment_id
      Digest::MD5.digest(id.to_s).unpack("L")[0] % max_segment
    end

    def regenerate_segment_id
      self.segment_id = generate_segment_id
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
axle_attributes-1.13.2 lib/axle_attributes/segmented.rb