Skip to content

Commit

Permalink
Use autoload instead of require.
Browse files Browse the repository at this point in the history
Fix adapter getter.
  • Loading branch information
merqlove committed Jan 9, 2016
1 parent bc92801 commit a686c3d
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/do_snapshot/adapter.rb
@@ -1,30 +1,29 @@
require_relative 'adapter/abstract'

require_relative 'adapter/digitalocean_v2'

module DoSnapshot
# Adapter interface for API connections
# Ability to select DigitalOcean API versions.
#
module Adapter
autoload :Abstract, 'do_snapshot/adapter/abstract'
autoload :DigitaloceanV2, 'do_snapshot/adapter/digitalocean_v2'

class << self
def api(protocol, options = {})
konst = find_protocol(protocol)
fail DoSnapshot::NoProtocolError, "Not existing protocol: #{protocol}." unless
Object.const_defined?(konst)
obj = Object.const_get(konst)
DoSnapshot::Adapter.const_defined?(konst)
obj = DoSnapshot::Adapter.const_get(konst)
obj.new(options)
end

private

def find_protocol(protocol)
if protocol.is_a?(Integer)
"DoSnapshot::Adapter::DigitaloceanV#{protocol}"
"::DoSnapshot::Adapter::DigitaloceanV#{protocol}"
elsif protocol.is_a?(String)
protocol
else
'DoSnapshot::Adapter::DigitaloceanV2'
'::DoSnapshot::Adapter::DigitaloceanV2'
end
end
end
Expand Down

0 comments on commit a686c3d

Please sign in to comment.