Sha256: 9ca55b36fa220ffbafafe7a8b68b018639816ee55e8f72d704e4c8651bc590c5

Contents?: true

Size: 1.24 KB

Versions: 18

Compression:

Stored size: 1.24 KB

Contents

module DbCharmer
  module ActiveRecord
    module MasterSlaveRouting

      module ClassMethods
        SLAVE_METHODS = [ :find_by_sql, :count_by_sql, :calculate ]
        MASTER_METHODS = [ :update, :create, :delete, :destroy, :delete_all, :destroy_all, :update_all, :update_counters ]

        SLAVE_METHODS.each do |slave_method|
          class_eval <<-EOF, __FILE__, __LINE__ + 1
            def #{slave_method}(*args, &block)
              first_level_on_slave do
                super(*args, &block)
              end
            end
          EOF
        end

        MASTER_METHODS.each do |master_method|
          class_eval <<-EOF, __FILE__, __LINE__ + 1
            def #{master_method}(*args, &block)
              on_master do
                super(*args, &block)
              end
            end
          EOF
        end

        def find(*args, &block)
          options = args.last
          if options.is_a?(Hash) && options[:lock]
            on_master { super(*args, &block) }
          else
            super(*args, &block)
          end
        end
      end

      module InstanceMethods
        def reload(*args, &block)
          self.class.on_master do
            super(*args, &block)
          end
        end
      end

    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
db-charmer-1.9.1 lib/db_charmer/rails2/active_record/master_slave_routing.rb
db-charmer-1.9.0 lib/db_charmer/rails2/active_record/master_slave_routing.rb
db-charmer-1.8.4 lib/db_charmer/rails2/active_record/master_slave_routing.rb
db-charmer-1.8.3 lib/db_charmer/rails2/active_record/master_slave_routing.rb
db-charmer-1.8.2 lib/db_charmer/rails2/active_record/master_slave_routing.rb
db-charmer-1.8.1 lib/db_charmer/rails2/active_record/master_slave_routing.rb
db-charmer-1.8.0 lib/db_charmer/rails2/active_record/master_slave_routing.rb
db-charmer-1.8.0.pre1 lib/db_charmer/rails2/active_record/master_slave_routing.rb
yam-db-charmer-1.7.4.14 lib/db_charmer/rails2/active_record/master_slave_routing.rb
yam-db-charmer-1.7.4.13 lib/db_charmer/rails2/active_record/master_slave_routing.rb
yam-db-charmer-1.7.4.10 lib/db_charmer/rails2/active_record/master_slave_routing.rb
yam-db-charmer-1.7.4.9 lib/db_charmer/rails2/active_record/master_slave_routing.rb
yam-db-charmer-1.7.4.8 lib/db_charmer/rails2/active_record/master_slave_routing.rb
yam-db-charmer-1.7.4.0 lib/db_charmer/rails2/active_record/master_slave_routing.rb
yam-db-charmer-1.7.01 lib/db_charmer/rails2/active_record/master_slave_routing.rb
db-charmer-1.7.1 lib/db_charmer/rails2/active_record/master_slave_routing.rb
db-charmer-1.7.0 lib/db_charmer/rails2/active_record/master_slave_routing.rb
db-charmer-1.7.0.pre7 lib/db_charmer/rails2/active_record/master_slave_routing.rb