Class: Archipelago::Disco::ServiceLocker

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Current::ThreadedCollection
Defined in:
lib/archipelago/disco.rb

Overview

A container of services.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Current::ThreadedCollection

#t_collect, #t_each, #t_reject, #t_select

Constructor Details

#initialize(options = {}) ⇒ ServiceLocker

Returns a new instance of ServiceLocker.



390
391
392
393
394
# File 'lib/archipelago/disco.rb', line 390

def initialize(options = {})
  @hash = options[:hash] || {}
  @hash.extend(Archipelago::Current::ThreadedCollection)
  @jockey = options[:jockey]
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



387
388
389
# File 'lib/archipelago/disco.rb', line 387

def hash
  @hash
end

Instance Method Details

#[]=(key, value) ⇒ Object

Set key to value.



408
409
410
411
412
413
414
415
# File 'lib/archipelago/disco.rb', line 408

def []=(key, value)
  existed_before = @hash.include?(key)
  @hash[key] = value
  # Notifying AFTER the fact to avoid loops.
  if @jockey && !existed_before
    @jockey.instance_eval do notify_subscribers(:found, value) end 
  end
end

#convert_to_tree!Object

Will make this ServiceLocker convert its Hash into an RBTree.



427
428
429
430
431
432
433
# File 'lib/archipelago/disco.rb', line 427

def convert_to_tree!
  t = RBTree.new
  @hash.each do |k,v|
    t[k] = v
  end
  @hash = t
end

#delete(key) ⇒ Object

Delete key.



419
420
421
422
423
# File 'lib/archipelago/disco.rb', line 419

def delete(key)
  value = @hash.delete(key)
  # Notifying AFTER the fact to avoid loops.
  @jockey.instance_eval do notify_subscribers(:lost, value) end if @jockey && value
end

#each(*args, &block) ⇒ Object



395
396
397
398
399
# File 'lib/archipelago/disco.rb', line 395

def each(*args, &block)
  clone = @hash.clone
  clone.extend(Archipelago::Current::ThreadedCollection)
  clone.each(*args, &block)
end

#get_services(match) ⇒ Object

Find all containing services matching match.



444
445
446
447
448
449
450
451
452
# File 'lib/archipelago/disco.rb', line 444

def get_services(match)
  rval = ServiceLocker.new
  self.each do |service_id, service_data|
    if service_data.matches?(match)
      rval[service_id] = service_data 
    end
  end
  return rval
end

#merge(sd) ⇒ Object

Merge this locker with another.



437
438
439
440
# File 'lib/archipelago/disco.rb', line 437

def merge(sd)
  rval = @hash.merge(sd.hash)
  ServiceLocker.new(:hash => rval)
end

#reverse_each(*args, &block) ⇒ Object



400
401
402
403
404
# File 'lib/archipelago/disco.rb', line 400

def reverse_each(*args, &block)
  clone = @hash.clone
  clone.extend(Archipelago::Current::ThreadedCollection)
  clone.reverse_each(*args, &block)
end

#validate!Object

Remove all non-valid services.



456
457
458
459
460
461
462
463
# File 'lib/archipelago/disco.rb', line 456

def validate!
  self.t_each do |service_id, service_data|
    unless service_data.valid?
      self.delete(service_id) 
    end
  end
  return self
end