Module: Adaptor::Loader::ClassMethods
- Defined in:
- lib/adaptor/loader.rb
Instance Method Summary collapse
-
#adaptors ⇒ Array<Class>
Returns the adaptors registered with this loader.
-
#load_adaptor(object) ⇒ Object|NilClass
Loads the first available adaptor for the given object.
-
#load_adaptor!(object) ⇒ Object
Loads the first available adaptor for the given object.
-
#load_adaptors(object) ⇒ Array
Loads all the adaptors which support the given object.
-
#load_adaptors!(object) ⇒ Array
Loads all the adaptors which support the given object.
-
#register(*klasses) ⇒ Object
Registers one or more new adaptors with the loader.
Instance Method Details
#adaptors ⇒ Array<Class>
Returns the adaptors registered with this loader.
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.
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.
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.
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.
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.
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 |