Sha256: f638fbbfebe8648b11976208154f1e2cc80e69964e68813d12b7a239504acbdb

Contents?: true

Size: 1.34 KB

Versions: 27

Compression:

Stored size: 1.34 KB

Contents

module DbCharmer
  module FinderOverrides
    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

    private

      def first_level_on_slave
        if db_charmer_top_level_connection? && on_master.connection.open_transactions.zero?
          on_slave { yield }
        else
          yield
        end
      end

    end

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

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
db-charmer-1.6.19 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.18 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.17 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.16 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.15 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.14 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.13 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.12 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.11 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.10 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.9 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.8 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.7 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.6 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.5 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.4 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.3 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.2 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.1 lib/db_charmer/finder_overrides.rb
db-charmer-1.6.0 lib/db_charmer/finder_overrides.rb