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, #each, #get, instance, #remove, #sync

Instance Method Details

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

Parameters:

  • id (String)
  • pool (String, Pool, nil) (defaults to: nil)

Returns:

  • (Boolean)


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

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

#each_by_ids(ids, pool = nil) {|object| ... } ⇒ Object

Iterate objects matching ids and pool

Parameters:

  • ids (Array<String>, nil)
  • pool (String, Pool, nil) (defaults to: nil)

Yield Parameters:

  • object (any)


75
76
77
78
79
80
81
82
# File 'lib/osctld/db/pooled_list.rb', line 75

def each_by_ids(ids, pool = nil)
  select_by_ids(ids, pool) do |obj|
    yield(obj)
    false
  end

  nil
end

#find(id, pool = nil) {|object| ... } ⇒ any

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>`

Parameters:

  • id (String)
  • pool (String, Pool, nil) (defaults to: nil)

Yield Parameters:

  • object (any)

Returns:

  • (any)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/osctld/db/pooled_list.rb', line 24

def find(id, pool = nil, &block)
  obj_id = DB::ObjectId.new(id, pool)

  sync do
    obj = objects.detect { |v| obj_id.match?(v) }

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

#select_by_ids(ids, pool = nil) {|object| ... } ⇒ Array

Select objects matching ids and pool

Parameters:

  • ids (Array<String>, nil)
  • pool (String, Pool, nil) (defaults to: nil)

Yield Parameters:

  • object (any)

Yield Returns:

  • (Boolean)

    select the object or not

Returns:

  • (Array)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/osctld/db/pooled_list.rb', line 50

def select_by_ids(ids, pool = nil, &block)
  if ids.nil?
    return get.each(&block) if block

    return get

  end

  obj_ids = ids.map { |id| DB::ObjectId.new(id, pool) }

  get.select do |obj|
    next(false) if obj_ids.detect { |obj_id| obj_id.match?(obj) }.nil?

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