Class: Himari::ProviderChain

Inherits:
Object
  • Object
show all
Defined in:
lib/himari/provider_chain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(providers) ⇒ ProviderChain

Returns a new instance of ProviderChain.

Parameters:



4
5
6
# File 'lib/himari/provider_chain.rb', line 4

def initialize(providers)
  @providers = providers
end

Instance Attribute Details

#providersObject (readonly)

Returns the value of attribute providers.



8
9
10
# File 'lib/himari/provider_chain.rb', line 8

def providers
  @providers
end

Instance Method Details

#collect(**hint) ⇒ Object



20
21
22
23
24
# File 'lib/himari/provider_chain.rb', line 20

def collect(**hint)
  @providers.flat_map do |provider|
    provider.collect(**hint)
  end
end

#find(**hint, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/himari/provider_chain.rb', line 10

def find(**hint, &block)
  block ||= proc { |i,h| i.match_hint?(**h) } # ItemProvider#collect doesn't guarantee exact matches, so do exact match by match_hint?
  @providers.each do |provider|
    provider.collect(**hint).each do |item|
      return item if block.call(item, hint)
    end
  end
  nil
end