Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/couchbase/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require "couchbase/configuration"
require "couchbase/authenticator"
require "couchbase/bucket"
require "couchbase/cluster_registry"

require "couchbase/management"
require "couchbase/options"
Expand Down Expand Up @@ -62,7 +63,12 @@ class Cluster
#
# @return [Cluster]
def self.connect(connection_string, *options)
Cluster.new(connection_string, *options)
regexp = /^((couchbases?|http):\/\/.*)$/i
if regexp.match?(connection_string) || !connection_string.include?("://")
Cluster.new(connection_string, *options)
else
ClusterRegistry.instance.connect(connection_string, *options)
end
end

# Returns an instance of the {Bucket}
Expand Down
44 changes: 44 additions & 0 deletions lib/couchbase/cluster_registry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

# Copyright 2023 Couchbase, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "singleton"

require "couchbase/errors"

module Couchbase
class ClusterRegistry
include Singleton
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the use of Singleton is needed here. Initially I was thinking about having these methods here as 'static' and @handlers as a class instance variable, but having mutable class instance variables violates rubocop's rules


def initialize
@handlers = {}
end

def connect(connection_string, *options)
@handlers.each do |regexp, cluster_class|
return cluster_class.connect(connection_string, *options) if regexp.match?(connection_string)
end
raise(Error::FeatureNotAvailable, "Connection string '#{connection_string}' not supported.")
end

def register_connection_handler(regexp, cluster_class)
@handlers[regexp] = cluster_class
end

def deregister_connection_handler(regexp)
@handlers.delete(regexp)
end
end
end