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.



98
99
100
# File 'lib/osctld/db/list.rb', line 98

def objects
  @objects
end

Class Method Details

.instanceObject



7
8
9
10
# File 'lib/osctld/db/list.rb', line 7

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

Instance Method Details

#add(obj) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/osctld/db/list.rb', line 56

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)


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

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

#countObject



52
53
54
# File 'lib/osctld/db/list.rb', line 52

def count
  sync { @objects.count }
end

#each(&block) ⇒ Object



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

def each(&block)
  sync do
    @objects.each(&block)
  end
end

#find(id, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/osctld/db/list.rb', line 80

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



42
43
44
45
46
47
48
49
50
# File 'lib/osctld/db/list.rb', line 42

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

#remove(obj) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/osctld/db/list.rb', line 68

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)
  end
end