Class: OsCtld::DB::List

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

Overview

Thread-safe singleton object list

Direct Known Subclasses

PooledList, Pools

Constant Summary collapse

@@instances =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectsObject (readonly, protected)

Returns the value of attribute objects



91
92
93
# File 'lib/osctld/db/list.rb', line 91

def objects
  @objects
end

Class Method Details

.instanceObject



9
10
11
12
# File 'lib/osctld/db/list.rb', line 9

def instance
  @@instances[self] = new unless @@instances.has_key?(self)
  @@instances[self]
end

Instance Method Details

#add(obj) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/osctld/db/list.rb', line 50

def add(obj)
  sync { @objects << obj }

  Eventd.report(
    :db,
    object: obj.class.name.split('::').last.downcase,
    pool: obj.pool.name,
    id: obj.id,
    action: :add
  )
end

#contains?(id) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/osctld/db/list.rb', line 86

def contains?(id)
  !find(id).nil?
end

#countObject



46
47
48
# File 'lib/osctld/db/list.rb', line 46

def count
  sync { @objects.count }
end

#find(id, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/osctld/db/list.rb', line 74

def find(id, &block)
  sync do
    obj = @objects.detect { |v| v.id == id }

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

#get(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/osctld/db/list.rb', line 36

def get(&block)
  sync do
    if block
      block.call(@objects)
    else
      @objects.clone
    end
  end
end

#remove(obj) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/osctld/db/list.rb', line 62

def remove(obj)
  sync { @objects.delete(obj) }

  Eventd.report(
    :db,
    object: obj.class.name.split('::').last.downcase,
    pool: obj.pool.name,
    id: obj.id,
    action: :remove
  )
end

#sync(&block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/osctld/db/list.rb', line 28

def sync(&block)
  if @mutex.owned?
    block.call
  else
    @mutex.synchronize { block.call }
  end
end