lib/moped/session.rb in moped-1.3.2 vs lib/moped/session.rb in moped-1.4.0

- old
+ new

@@ -128,10 +128,22 @@ # @since 1.0.0 def drop current_database.drop end + # Provide a string inspection for the session. + # + # @example Inspect the session. + # session.inspect + # + # @return [ String ] The string inspection. + # + # @since 1.4.0 + def inspect + "<#{self.class.name} seeds=#{cluster.seeds} database=#{current_database_name}>" + end + # Log in with +username+ and +password+ on the current database. # # @param (see Moped::Database#login) # # @raise (see Moped::Database#login) @@ -227,11 +239,11 @@ # @yieldparam [ Session ] session The new session. def new(options = {}) session = with(options) session.instance_variable_set(:@cluster, cluster.dup) if block_given? - yield session + yield(session) else session end end @@ -269,11 +281,11 @@ # @param [ String, Symbol ] database The database to use. # # @since 1.0.0 def use(database) options[:database] = database - set_current_database database + set_current_database(database) end # Create a new session with +options+ reusing existing connections. # # @example Change safe mode @@ -307,11 +319,11 @@ # @yieldparam [ Session ] session The new session. def with(options = {}) session = dup session.options.update(options) if block_given? - yield session + yield(session) else session end end @@ -336,24 +348,26 @@ end private def current_database - return @current_database if defined? @current_database - + return @current_database if defined?(@current_database) if database = options[:database] set_current_database(database) else raise "No database set for session. Call #use or #with before accessing the database" end end + def current_database_name + defined?(@current_database) ? current_database.name : :none + end + def initialize_copy(_) @context = Context.new(self) @options = @options.dup - - if defined? @current_database - remove_instance_variable :@current_database + if defined?(@current_database) + remove_instance_variable(:@current_database) end end def set_current_database(database) @current_database = Database.new(self, database)