Sha256: 43da4cdd98b81fc5c1250ad143870c92f8c43a761a49eb56e50c32d0dc750e6c

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 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
          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
          def #{master_method}(*args, &block)
            on_master do
              super(*args, &block)
            end
          end
        EOF
      end

    private

      def first_level_on_slave
        if db_charmer_top_level_connection?
          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

5 entries across 5 versions & 2 rubygems

Version Path
glebpom-db-charmer-1.1.5 lib/db_charmer/finder_overrides.rb
kovyrin-db-charmer-1.0.1 lib/db_charmer/finder_overrides.rb
kovyrin-db-charmer-1.1.0 lib/db_charmer/finder_overrides.rb
kovyrin-db-charmer-1.1.2 lib/db_charmer/finder_overrides.rb
kovyrin-db-charmer-1.1.3 lib/db_charmer/finder_overrides.rb