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.



379
380
381
382
383
# File 'lib/archipelago/disco.rb', line 379

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.



376
377
378
# File 'lib/archipelago/disco.rb', line 376

def hash
  @hash
end

Instance Method Details

#[]=(key, value) ⇒ Object

Set key to value.



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

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.



416
417
418
419
420
421
422
# File 'lib/archipelago/disco.rb', line 416

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

#delete(key) ⇒ Object

Delete key.



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

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



384
385
386
387
388
# File 'lib/archipelago/disco.rb', line 384

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.



433
434
435
436
437
438
439
440
441
# File 'lib/archipelago/disco.rb', line 433

def get_services(match)
  rval = ServiceLocker.new
  self.t_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.



426
427
428
429
# File 'lib/archipelago/disco.rb', line 426

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

#reverse_each(*args, &block) ⇒ Object



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

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.



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

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