Class: OsCtld::DB::PooledList

Inherits:
List
  • Object
show all
Defined in:
lib/osctld/db/pooled_list.rb

Direct Known Subclasses

Containers, Groups, IdRanges, Repositories, Users

Instance Attribute Summary

Attributes inherited from List

#objects

Instance Method Summary collapse

Methods inherited from List

#add, #count, #get, instance, #remove, #sync

Instance Method Details

#contains?(id, pool = nil) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/osctld/db/pooled_list.rb', line 40

def contains?(id, pool = nil)
  !find(id, pool).nil?
end

#find(id, pool = nil, &block) ⇒ Object

Find object by `id` and `pool`.

There are two ways to specify a pool:

- use the `pool` argument
- when `pool` is `nil` and `id` contains a colon, it is parsed as
  `<pool>:<id>`


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/osctld/db/pooled_list.rb', line 11

def find(id, pool = nil, &block)
  if pool.nil? && id.index(':')
    pool, id = id.split(':')
  end

  sync do
    obj = objects.detect do |v|
      next if v.id != id
      next(true) if pool.nil?

      if pool.is_a?(Pool)
        v.pool.name == pool.name

      elsif pool.is_a?(String)
        v.pool.name == pool

      else
        fail "invalid pool type '#{pool.class}', extected OsCtld::Pool or String"
      end
    end

    if block
      block.call(obj)
    else
      obj
    end
  end
end