Skip to content

Commit 556fff6

Browse files
committed
Change api namespace to fail on redefinition.
1 parent f1da49d commit 556fff6

3 files changed

Lines changed: 47 additions & 43 deletions

File tree

lib/github_api/api.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,21 +300,27 @@ def set(option, value=(not_set=true), ignore_setter=false, &block)
300300
# @param [Array[Symbol]] names
301301
# the name for the scope
302302
#
303+
# @example
304+
# namespace :scopes
305+
#
303306
# @return [self]
304307
#
305308
# @api public
306309
def self.namespace(*names)
307310
options = names.last.is_a?(Hash) ? names.pop : {}
308311
names = names.map(&:to_sym)
309312
name = names.pop
310-
return if public_method_defined?(name)
313+
314+
if public_method_defined?(name)
315+
raise ArgumentError, "namespace '#{name}' is already defined"
316+
end
311317

312318
class_name = extract_class_name(name, options)
319+
313320
define_method(name) do |*args, &block|
314321
options = args.last.is_a?(Hash) ? args.pop : {}
315322
API::Factory.new(class_name, current_options.merge(options), &block)
316323
end
317-
self
318324
end
319325

320326
# Extracts class name from options
@@ -325,6 +331,9 @@ def self.namespace(*names)
325331
# @option options [Boolean] :root
326332
# if the class is at the root or not
327333
#
334+
# @example
335+
# extract_class_name(:stats, class_name: :statistics)
336+
#
328337
# @return [String]
329338
#
330339
# @api private

spec/github/api/namespace_spec.rb

Lines changed: 0 additions & 41 deletions
This file was deleted.

spec/unit/api/namespace_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe Github::API, '::namespace' do
6+
let(:foo_class) { Class.new(Github::API) }
7+
8+
let(:foo_scopes_class) { Class.new(Github::API) }
9+
10+
it "inherits namespace method" do
11+
stub_const("Foo", foo_class)
12+
expect(Foo).to respond_to(:namespace)
13+
end
14+
15+
it "registers namespaced method" do
16+
stub_const("Foo", foo_class)
17+
stub_const("Foo::Scopes", foo_scopes_class)
18+
19+
Foo.namespace(:scopes)
20+
21+
foo_instance = Foo.new
22+
23+
expect(foo_instance).to respond_to(:scopes)
24+
expect(foo_instance.scopes).to be_instance_of(Foo::Scopes)
25+
end
26+
27+
it "doesn't redefine already existing namespace" do
28+
stub_const("Foo", foo_class)
29+
30+
Foo.namespace(:scopes)
31+
32+
expect {
33+
Foo.namespace(:scopes)
34+
}.to raise_error(ArgumentError, /namespace 'scopes' is already defined/)
35+
end
36+
end

0 commit comments

Comments
 (0)