Class: Econfig::BackendCollection
- Inherits:
-
Object
- Object
- Econfig::BackendCollection
- Includes:
- Enumerable
- Defined in:
- lib/econfig/backend_collection.rb
Instance Method Summary collapse
- #[](name) ⇒ Object
- #delete(name) ⇒ Object
- #each ⇒ Object
- #get(key) ⇒ Object
-
#initialize ⇒ BackendCollection
constructor
A new instance of BackendCollection.
- #insert_after(other, name, backend) ⇒ Object
- #insert_before(other, name, backend) ⇒ Object
- #keys ⇒ Object
- #push(name, backend) ⇒ Object (also: #use)
- #unshift(name, backend) ⇒ Object
Constructor Details
#initialize ⇒ BackendCollection
Returns a new instance of BackendCollection.
6 7 8 |
# File 'lib/econfig/backend_collection.rb', line 6 def initialize @backends = [] end |
Instance Method Details
#[](name) ⇒ Object
20 21 22 23 |
# File 'lib/econfig/backend_collection.rb', line 20 def [](name) pair = @backends.assoc(name) pair.last if pair end |
#delete(name) ⇒ Object
54 55 56 |
# File 'lib/econfig/backend_collection.rb', line 54 def delete(name) @backends.delete_at(index_of!(name)) end |
#each ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/econfig/backend_collection.rb', line 25 def each if block_given? @backends.each { |(name, backend)| yield backend } else enum_for { @backends.length } end end |
#get(key) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/econfig/backend_collection.rb', line 58 def get(key) @backends.each do |(name, backend)| has_key = true if backend.respond_to?(:has_key?) has_key = backend.has_key?(key) value = backend.get(key) if has_key else value = backend.get(key) { has_key = false } end has_key or next return value end yield if block_given? end |
#insert_after(other, name, backend) ⇒ Object
49 50 51 52 |
# File 'lib/econfig/backend_collection.rb', line 49 def insert_after(other, name, backend) exists?(name) @backends.insert(index_of!(other) + 1, [name, backend]) end |
#insert_before(other, name, backend) ⇒ Object
44 45 46 47 |
# File 'lib/econfig/backend_collection.rb', line 44 def insert_before(other, name, backend) exists?(name) @backends.insert(index_of!(other), [name, backend]) end |
#keys ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/econfig/backend_collection.rb', line 10 def keys @backends.reduce(Set.new) do |agg, (name, backend)| if backend.respond_to?(:keys) agg.union(backend.keys) else agg end end end |
#push(name, backend) ⇒ Object Also known as: use
33 34 35 36 |
# File 'lib/econfig/backend_collection.rb', line 33 def push(name, backend) exists?(name) @backends.push([name, backend]) end |
#unshift(name, backend) ⇒ Object
39 40 41 42 |
# File 'lib/econfig/backend_collection.rb', line 39 def unshift(name, backend) exists?(name) @backends.unshift([name, backend]) end |