Class: ProviderKit::Provider

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

Overview

Given an object, try and find its registered data provider

Or directly pass a provider key

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Provider

Returns a new instance of Provider.



11
12
13
# File 'lib/provider_kit/provider.rb', line 11

def initialize(record)
  @record = record
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



9
10
11
# File 'lib/provider_kit/provider.rb', line 9

def record
  @record
end

Instance Method Details

#keyObject



15
16
17
# File 'lib/provider_kit/provider.rb', line 15

def key
  record_provider.presence || record_source.presence
end

#present?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/provider_kit/provider.rb', line 19

def present?
  provider.present?
end

#providerObject



23
24
25
26
27
# File 'lib/provider_kit/provider.rb', line 23

def provider
  return nil unless registration

  registration.klass
end

#provider_instanceObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/provider_kit/provider.rb', line 29

def provider_instance
  return nil unless provider

  if provider.class == Module
    raise "Provider #{ provider } is a module, not a class"
  end

  instance = provider.new

  if instance.respond_to?(:context=) && record != key
    instance.context = record
  end

  instance
end

#registrationObject



45
46
47
48
49
# File 'lib/provider_kit/provider.rb', line 45

def registration
  if key.present?
    ProviderKit.registrations[key.to_sym]
  end
end