Sha256: 3a94f7d0287e20d2e03f43e2e110fab85200a238074b886c1275d7d7e8abf280

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

# -*- coding: utf-8 -*-
#
# 採番
#

module ActiveRecord::Turntable
  class Sequencer
    extend ActiveSupport::Autoload

    eager_autoload do
      autoload :Api
      autoload :Mysql
      autoload :Barrage
    end

    class_attribute :sequence_types
    class_attribute :sequences
    class_attribute :tables

    self.sequence_types = {
      api: Api,
      mysql: Mysql,
      barrage: Barrage,
    }
    self.sequences = {}
    self.tables = {}

    class << self
      def build(klass, sequence_name = nil, cluster_name = nil)
        sequence_name ||= current_cluster_config_for(cluster_name || klass)[:seq].keys.first
        seq_config = current_cluster_config_for(cluster_name || klass)[:seq][sequence_name]
        seq_type = (seq_config[:seq_type] ? seq_config[:seq_type].to_sym : :mysql)
        tables[klass.table_name] ||= (sequences[sequence_name(klass.table_name, klass.primary_key)] ||= sequence_types[seq_type].new(klass, seq_config))
      end

      def has_sequencer?(table_name)
        !!tables[table_name]
      end

      def sequence_name(table_name, pk)
        "#{table_name}_#{pk || 'id'}_seq"
      end

      def table_name(seq_name)
        seq_name.split("_").first
      end

      private

        def current_cluster_config_for(klass_or_name)
          cluster_name = if klass_or_name.is_a?(Symbol)
                           klass_or_name
                         else
                           klass_or_name.turntable_cluster_name.to_s
                         end
          ActiveRecord::Base.turntable_config[:clusters][cluster_name]
        end
    end

    def next_sequence_value
      raise NotImplementedError
    end

    def current_sequence_value
      raise NotImplementedError
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activerecord-turntable-3.1.0 lib/active_record/turntable/sequencer.rb
activerecord-turntable-3.0.1 lib/active_record/turntable/sequencer.rb
activerecord-turntable-3.0.0 lib/active_record/turntable/sequencer.rb