Sha256: 116c651348bdebff0d9c8651ea15bca39c7b7efd9fbb4a7c0ce4faa33873e820

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

# Define the class where all the actives relationships are
# efficiently stored
class Phenomenal::RelationshipsStore
  attr_accessor :sources, :targets
  
  def initialize
    @sources = {}
    @targets = {}
  end
  
  def add(relationship)
    if @sources[relationship.source].nil?
      @sources[relationship.source] = Array.new 
    end
    @sources[relationship.source].push(relationship)
    
    if @targets[relationship.target].nil?
      @targets[relationship.target] = Array.new 
    end
    @targets[relationship.target].push(relationship)
  end
  
  def remove(relationship)
    @sources[relationship.source].delete(relationship) if @sources[relationship.source] # In case of rollback
    @targets[relationship.target].delete(relationship) if @targets[relationship.target]
  end
  
  def include?(relationship)
    if @sources[relationship.source]
      @sources[relationship.source].include?(relationship)
    else
      false
    end
  end
  
  def update_references(context)
    # Do nothing when anonymous, references are already valid
    return if context.anonymous?
    # Update sources
    if not @sources[context.name].nil?
      @sources[context.name].each do |relationship|
        relationship.source=context
      end
      @sources[context]=@source.delete(context.name)
    end
    # Update targets
    if not @targets[context.name].nil?
      @targets[context.name].each do |relationship|
        relationship.target=context
      end
      @targets[context]=@targets.delete(context.name)
    end
  end
  
  def get_for(context)
    get_for_source(context).concat(get_for_target(context))
  end
  
  private
  # Return an array of relationships
  def get_for_source(source)
    rel = @sources[source]
    if rel.nil?
      Array.new
    else
      rel
    end
  end
  
  # Return an array of relationships
  def get_for_target(target)
    rel = @targets[target]
    if rel.nil?
      Array.new
    else
      rel
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
phenomenal-1.1.0 lib/phenomenal/relationships/relationships_store.rb
phenomenal-1.0.1 lib/phenomenal/relationships/relationships_store.rb
phenomenal-1.0.0 lib/phenomenal/relationships/relationships_store.rb