Module: Adaptor::Loader::ClassMethods

Defined in:
lib/adaptor/loader.rb

Instance Method Summary collapse

Instance Method Details

#adaptorsArray<Class>

Returns the adaptors registered with this loader.

Returns:

  • (Array<Class>)


29
30
31
# File 'lib/adaptor/loader.rb', line 29

def adaptors
  @adaptors ||= []
end

#load_adaptor(object) ⇒ Object|NilClass

Loads the first available adaptor for the given object.

Parameters:

  • object (Object)

    the object to load the adaptor for

Returns:

  • (Object|NilClass)

    an instance of the adpator or nil if no adaptor was found



38
39
40
# File 'lib/adaptor/loader.rb', line 38

def load_adaptor(object)
  adaptors.find { |adaptor_klass| adaptor_klass.supports?(object) }&.new(object)
end

#load_adaptor!(object) ⇒ Object

Loads the first available adaptor for the given object.

Parameters:

  • object (Object)

    the object to load the adaptor for

Returns:

  • (Object)

    an instance of the adaptor

Raises:



49
50
51
# File 'lib/adaptor/loader.rb', line 49

def load_adaptor!(object)
  load_adaptor(object) || fail(NoAdaptorError, "No adaptor found for #{object}")
end

#load_adaptors(object) ⇒ Array

Loads all the adaptors which support the given object.

Parameters:

  • object (Object)

    the object to load the adaptors for

Returns:

  • (Array)

    instances of compatible adaptors



58
59
60
61
62
63
# File 'lib/adaptor/loader.rb', line 58

def load_adaptors(object)
  adaptors.map do |adaptor_klass|
    next unless adaptor_klass.supports?(object)
    adaptor_klass.new(object)
  end.compact
end

#load_adaptors!(object) ⇒ Array

Loads all the adaptors which support the given object.

Parameters:

  • object (Object)

    the object to load the adaptors for

Returns:

  • (Array)

    instances of compatible adaptors

Raises:



72
73
74
75
# File 'lib/adaptor/loader.rb', line 72

def load_adaptors!(object)
  adaptors = load_adaptors(object)
  adaptors.any? ? adaptors : fail(NoAdaptorError, "No adaptors found for #{object}")
end

#register(*klasses) ⇒ Object

Registers one or more new adaptors with the loader.

Parameters:

  • klasses (Array<Class>)

    the adaptors to register



20
21
22
23
24
# File 'lib/adaptor/loader.rb', line 20

def register(*klasses)
  klasses.each do |klass|
    adaptors << klass unless adaptors.include?(klass)
  end
end