Reputation: 143
I am creating two gems that wrap the same API. One is a vanilla client library that just wraps API endpoints, the second integrates with a third-party API and provides another layer of abstraction over the first. They do not have any overlapping methods.
Is it considered acceptable for the second to extend the namespace of the first?
For example:
# vanilla client lib
module Foo
class Bar
def do_bar
puts "Bar!"
end
end
end
# more complex client lib
module Foo
class Baz
def do_baz
puts "Baz!"
end
end
end
Based on this question it looks like AWS does this. Are there cases where this is okay and where it isn't?
Upvotes: 1
Views: 460
Reputation: 527
This seems like a perfectly acceptable way to structure your gems. In your code sample, Foo::Bar
and Foo::Baz
will be two completely different classes within the same namespace, and they will not clash with each other. This is true even if you used the same method names within the two classes:
module Foo
class Bar
def do_thing
puts "Bar!"
end
end
end
module Foo
class Baz
def do_thing
puts "Baz!"
end
end
end
In this modified example, Foo::Bar#do_thing
will output "Bar!"
and Foo::Baz#do_thing
will output "Baz!"
, since they are separate methods in separate classes. This approach allows you to swap out Foo::Baz
objects for Foo::Bar
objects to change behavior, since they have the same interface.
If you wanted your third-party API gem to extend the functionality of your vanilla API gem, you may want to consider adjusting your approach to use inheritance.
Upvotes: 1