Class: Avatar::Source::SourceChain

Inherits:
Object
  • Object
show all
Includes:
AbstractSource
Defined in:
lib/avatar/source/source_chain.rb

Instance Method Summary collapse

Constructor Details

#initializeSourceChain

:nodoc:



9
10
11
# File 'lib/avatar/source/source_chain.rb', line 9

def initialize
  clear!
end

Instance Method Details

#<<(source) ⇒ Object

Alias for add_source



30
31
32
# File 'lib/avatar/source/source_chain.rb', line 30

def <<(source)
  add_source(source)
end

#[](n) ⇒ Object

Retrieve the n<sup>th</sup> Source.



19
20
21
# File 'lib/avatar/source/source_chain.rb', line 19

def [](n)
  @chain[n]
end

#add_source(source) ⇒ Object

Add a source to the chain. source must be an instance of (a subclass of) Avatar::Source::AbstractSource.

Raises:

  • (ArgumentError)


24
25
26
27
# File 'lib/avatar/source/source_chain.rb', line 24

def add_source(source)
  raise ArgumentError.new("#{source} is not an Avatar::Source::AbstractSource") unless source.kind_of?(Avatar::Source::AbstractSource)
  @chain << source
end

#avatar_url_for(person, options = {}) ⇒ Object

Iterate through the chain and return the first URL returned. Any error raised will propagate. Duplicates options before passing so each Source receives the same arguments.



42
43
44
45
46
47
48
# File 'lib/avatar/source/source_chain.rb', line 42

def avatar_url_for(person, options = {})
  @chain.each do |source|
    result = source.avatar_url_for(person, options.dup)
    return result unless result.nil?
  end
  return nil
end

#clear!Object

Clear the chain



14
15
16
# File 'lib/avatar/source/source_chain.rb', line 14

def clear!
  @chain = []
end

#empty?Boolean

True unless a Source has been added.

Returns:

  • (Boolean)


35
36
37
# File 'lib/avatar/source/source_chain.rb', line 35

def empty?
  @chain.empty?
end