Module: ProviderKit::Registerable

Included in:
ProviderKit
Defined in:
lib/provider_kit/registerable.rb

Overview

Designed to add registration methods to the core namespace

Instance Method Summary collapse

Instance Method Details

#deregister(key) ⇒ Object



7
8
9
10
11
# File 'lib/provider_kit/registerable.rb', line 7

def deregister(key)
  if registrations[key]
    config.registered_providers.delete(key)
  end
end

#for(type) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/provider_kit/registerable.rb', line 13

def for(type)
  if env_key = ENV["PROVIDER_FOR_#{ type.upcase }"].presence
    registrations[env_key.to_clean_sym].klass
  elsif default_key = config.type_defaults[type].presence
    registrations[default_key].klass
  else
    providers(type:).last
  end
end

#nullObject

special provider for placeholders



24
25
26
# File 'lib/provider_kit/registerable.rb', line 24

def null
  ProviderKit.registrations[:null].klass
end

#providers(type: nil) ⇒ Object



28
29
30
# File 'lib/provider_kit/registerable.rb', line 28

def providers(type: nil)
  registrations(type:).values.map(&:klass)
end

#register(key, **options) ⇒ Object

ProviderKit.register :stripe



33
34
35
36
37
38
39
40
# File 'lib/provider_kit/registerable.rb', line 33

def register(key, **options)
  registration = Registration.new(key, **options)
  return false unless registration.valid?

  deregister(key)

  config.registered_providers[key] = registration
end

#registrations(type: nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/provider_kit/registerable.rb', line 42

def registrations(type: nil)
  if type.present?
    config.registered_providers.select { |_, reg| reg.types.include?(type) }
  else
    config.registered_providers
  end
end

#use(key, **options) ⇒ Object



50
51
52
53
54
# File 'lib/provider_kit/registerable.rb', line 50

def use(key, **options)
  type = options[:for] || options[:type]

  config.type_defaults[type] = key
end