Skip to content

Commit 8ac545c

Browse files
authored
Merge pull request #229 from fog/best-practices
Force Gem Best Practice
2 parents 8d34a75 + 473abf8 commit 8ac545c

File tree

5 files changed

+63
-47
lines changed

5 files changed

+63
-47
lines changed

lib/fog/core/provider.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@ def service(new_service, constant_string)
2121
Fog.services[new_service] ||= []
2222
Fog.services[new_service] |= [to_s.split("::").last.downcase.to_sym]
2323
@services_registry ||= {}
24-
@services_registry[new_service] = [to_s, constant_string].join("::")
24+
@services_registry[new_service] = service_klass(constant_string)
2525
end
2626

2727
def services
2828
@services_registry.keys
2929
end
30+
31+
def service_klass(constant_string)
32+
eval([to_s, constant_string].join("::"))
33+
[to_s, constant_string].join("::")
34+
rescue NameError
35+
provider = to_s.split("::").last
36+
Fog::Logger.deprecation("Unable to load #{[to_s, constant_string].join("::")}")
37+
Fog::Logger.deprecation("The format #{['Fog', constant_string, provider].join("::")} is deprecated")
38+
['Fog', constant_string, provider].join("::")
39+
end
3040
end
3141
end

lib/fog/core/services_mixin.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,24 @@ def providers
2424

2525
private
2626

27+
# This method should be removed once all providers are extracted.
28+
# Bundler will correctly require all dependencies automatically and thus
29+
# fog-core wont need to know any specifics providers. Each provider will
30+
# have to load its dependencies.
2731
def require_service_provider_library(service, provider)
2832
require "fog/#{provider}/#{service}"
2933
rescue LoadError # Try to require the service provider in an alternate location
34+
Fog::Logger.deprecation("Unable to require fog/#{provider}/#{service}")
35+
Fog::Logger.deprecation("The format fog/#{service}/#{provider} is deprecated")
3036
require "fog/#{service}/#{provider}"
3137
end
3238

3339
def service_provider_constant(service_name, provider_name)
34-
Fog.const_get(service_name).const_get(*const_get_args(provider_name))
35-
rescue NameError # Try to find the constant from in an alternate location
3640
Fog.const_get(provider_name).const_get(*const_get_args(service_name))
41+
rescue NameError # Try to find the constant from in an alternate location
42+
Fog::Logger.deprecation("Unable to load Fog::#{provider_name}::#{service_name}")
43+
Fog::Logger.deprecation("The format Fog::#{service_name}::#{provider_name} is deprecated")
44+
Fog.const_get(service_name).const_get(*const_get_args(provider_name))
3745
end
3846

3947
def const_get_args(*args)

spec/compute_spec.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ module TheRightWay
1616
end
1717

1818
module Fog
19-
module Compute
20-
class TheRightWay
19+
module TheRightWay
20+
class Compute
2121
def initialize(_args); end
2222
end
2323
end
2424
end
2525

2626
it "instantiates an instance of Fog::Compute::<Provider> from the :provider keyword arg" do
2727
compute = Fog::Compute.new(:provider => :therightway)
28-
assert_instance_of(Fog::Compute::TheRightWay, compute)
28+
assert_instance_of(Fog::TheRightWay::Compute, compute)
2929
end
3030

3131
module Fog
@@ -36,16 +36,16 @@ module TheWrongWay
3636
end
3737

3838
module Fog
39-
module TheWrongWay
40-
class Compute
39+
module Compute
40+
class TheWrongWay
4141
def initialize(_args); end
4242
end
4343
end
4444
end
4545

4646
it "instantiates an instance of Fog::<Provider>::Compute from the :provider keyword arg" do
4747
compute = Fog::Compute.new(:provider => :thewrongway)
48-
assert_instance_of(Fog::TheWrongWay::Compute, compute)
48+
assert_instance_of(Fog::Compute::TheWrongWay, compute)
4949
end
5050

5151
module Fog
@@ -58,26 +58,26 @@ module BothWays
5858
module Fog
5959
module BothWays
6060
class Compute
61-
def initialize(_args); end
61+
attr_reader :args
62+
def initialize(args)
63+
@args = args
64+
end
6265
end
6366
end
6467
end
6568

6669
module Fog
6770
module Compute
6871
class BothWays
69-
attr_reader :args
70-
def initialize(args)
71-
@args = args
72-
end
72+
def initialize(_args); end
7373
end
7474
end
7575
end
7676

7777
describe "when both Fog::Compute::<Provider> and Fog::<Provider>::Compute exist" do
78-
it "prefers Fog::Compute::<Provider>" do
78+
it "prefers Fog::<Provider>::Compute" do
7979
compute = Fog::Compute.new(:provider => :bothways)
80-
assert_instance_of(Fog::Compute::BothWays, compute)
80+
assert_instance_of(Fog::BothWays::Compute, compute)
8181
end
8282
end
8383

spec/identity_spec.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,36 @@ module TheRightWay
1616
end
1717

1818
module Fog
19-
module Identity
20-
class TheRightWay
19+
module TheRightWay
20+
class Identity
2121
def initialize(_args); end
2222
end
2323
end
2424
end
2525

2626
it "instantiates an instance of Fog::Identity::<Provider> from the :provider keyword arg" do
2727
identity = Fog::Identity.new(:provider => :therightway)
28-
assert_instance_of(Fog::Identity::TheRightWay, identity)
28+
assert_instance_of(Fog::TheRightWay::Identity, identity)
2929
end
3030

3131
module Fog
32-
module Rackspace
32+
module TheWrongWay
3333
extend Provider
3434
service(:identity, "Identity")
3535
end
3636
end
3737

3838
module Fog
39-
module Rackspace
40-
class Identity
39+
module Identity
40+
class TheWrongWay
4141
def initialize(_args); end
4242
end
4343
end
4444
end
4545

4646
it "instantiates an instance of Fog::<Provider>::Identity from the :provider keyword arg" do
47-
identity = Fog::Identity.new(:provider => :rackspace)
48-
assert_instance_of(Fog::Rackspace::Identity, identity)
47+
identity = Fog::Identity.new(:provider => :thewrongway)
48+
assert_instance_of(Fog::Identity::TheWrongWay, identity)
4949
end
5050

5151
module Fog
@@ -58,26 +58,26 @@ module BothWays
5858
module Fog
5959
module BothWays
6060
class Identity
61-
def initialize(_args); end
61+
attr_reader :args
62+
def initialize(args)
63+
@args = args
64+
end
6265
end
6366
end
6467
end
6568

6669
module Fog
6770
module Identity
6871
class BothWays
69-
attr_reader :args
70-
def initialize(args)
71-
@args = args
72-
end
72+
def initialize(_args); end
7373
end
7474
end
7575
end
7676

7777
describe "when both Fog::Identity::<Provider> and Fog::<Provider>::Identity exist" do
7878
it "prefers Fog::Identity::<Provider>" do
7979
identity = Fog::Identity.new(:provider => :bothways)
80-
assert_instance_of(Fog::Identity::BothWays, identity)
80+
assert_instance_of(Fog::BothWays::Identity, identity)
8181
end
8282
end
8383

spec/storage_spec.rb

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,34 @@ module TheRightWay
1616
end
1717

1818
module Fog
19-
module Storage
20-
class TheRightWay
19+
module TheRightWay
20+
class Storage
2121
def initialize(_args); end
2222
end
2323
end
2424
end
2525

26-
it "instantiates an instance of Fog::Storage::<Provider> from the :provider keyword arg" do
26+
it "instantiates an instance of Fog::<Provider>::Storage from the :provider keyword arg" do
2727
compute = Fog::Storage.new(:provider => :therightway)
28-
assert_instance_of(Fog::Storage::TheRightWay, compute)
28+
assert_instance_of(Fog::TheRightWay::Storage, compute)
2929
end
3030

3131
module Fog
3232
module TheWrongWay
3333
extend Provider
3434
service(:storage, "Storage")
3535
end
36-
end
3736

38-
module Fog
39-
module TheWrongWay
40-
class Storage
37+
module Storage
38+
class TheWrongWay
4139
def initialize(_args); end
4240
end
4341
end
4442
end
4543

46-
it "instantiates an instance of Fog::<Provider>::Storage from the :provider keyword arg" do
44+
it "instantiates an instance of Fog::Storage::<Provider> from the :provider keyword arg" do
4745
compute = Fog::Storage.new(:provider => :thewrongway)
48-
assert_instance_of(Fog::TheWrongWay::Storage, compute)
46+
assert_instance_of(Fog::Storage::TheWrongWay, compute)
4947
end
5048

5149
module Fog
@@ -58,27 +56,27 @@ module BothWays
5856
module Fog
5957
module BothWays
6058
class Storage
61-
def initialize(_args); end
59+
attr_reader :args
60+
61+
def initialize(args)
62+
@args = args
63+
end
6264
end
6365
end
6466
end
6567

6668
module Fog
6769
module Storage
6870
class BothWays
69-
attr_reader :args
70-
71-
def initialize(args)
72-
@args = args
73-
end
71+
def initialize(_args); end
7472
end
7573
end
7674
end
7775

7876
describe "when both Fog::Storage::<Provider> and Fog::<Provider>::Storage exist" do
7977
it "prefers Fog::Storage::<Provider>" do
8078
compute = Fog::Storage.new(:provider => :bothways)
81-
assert_instance_of(Fog::Storage::BothWays, compute)
79+
assert_instance_of(Fog::BothWays::Storage, compute)
8280
end
8381
end
8482

0 commit comments

Comments
 (0)