Class: ProviderKit::Registration

Inherits:
Object
  • Object
show all
Defined in:
lib/provider_kit/registration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, **options) ⇒ Registration

Returns a new instance of Registration.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/provider_kit/registration.rb', line 9

def initialize(key, **options)
  @key      = key

  if options[:type]
    @types = Array(options.delete(:type)).flatten.compact.presence
  end

  if options[:types]
    @types = Array(options.delete(:types)).flatten.compact.presence
  end

  @options  = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

generic? => (types.include?(:generic))



52
53
54
55
56
# File 'lib/provider_kit/registration.rb', line 52

def method_missing(method_name)
  if match = method_name.to_s.match(/^(?<attribute>[a-z0-9_]+)\?$/)
    types.include?(match[:attribute].to_sym)
  end
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/provider_kit/registration.rb', line 6

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/provider_kit/registration.rb', line 7

def options
  @options
end

Instance Method Details

#class_nameObject



23
24
25
# File 'lib/provider_kit/registration.rb', line 23

def class_name
  options[:class_name].presence || "#{ key.to_s.classify }::Provider"
end

#configObject



27
28
29
# File 'lib/provider_kit/registration.rb', line 27

def config
  @config ||= Settings.load(options)
end

#credentialsObject



31
32
33
# File 'lib/provider_kit/registration.rb', line 31

def credentials
  @credentials ||= Settings.load(Rails.application.credentials.dig(:providers, key)&.to_h)
end

#disallow_in_test_env?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/provider_kit/registration.rb', line 39

def disallow_in_test_env?
  options[:test] == false
end

#display_keyObject



35
36
37
# File 'lib/provider_kit/registration.rb', line 35

def display_key
  config.short_key.presence || key
end

#klassObject



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

def klass
  if Rails.application.config.cache_classes
    @klass ||= build_klass_from_name
  else
    build_klass_from_name
  end
end

#nameObject



58
59
60
# File 'lib/provider_kit/registration.rb', line 58

def name
  options[:name].presence || key.to_s.titleize
end

#providerObject



62
63
64
# File 'lib/provider_kit/registration.rb', line 62

def provider
  ProviderKit::Provider.new(key).provider_instance
end

#typesObject



66
67
68
# File 'lib/provider_kit/registration.rb', line 66

def types
  @types ||= [ :generic ]
end

#valid?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/provider_kit/registration.rb', line 70

def valid?
  if disallow_in_test_env? && Rails.env.test?
    return false
  end

  true
end