class Related::Relationship

Public Class Methods

create(label, node1, node2, attributes = {}) click to toggle source
# File lib/related/relationship.rb, line 39
def self.create(label, node1, node2, attributes = {})
  self.new(attributes.merge(
    :label => label,
    :start_node_id => node1.is_a?(String) ? node1 : (node1 ? node1.id : nil),
    :end_node_id => node2.is_a?(String) ? node2 : (node2 ? node2.id : nil)
  )).save
end
new(*attributes) click to toggle source
# File lib/related/relationship.rb, line 6
def initialize(*attributes)
  @_internal_id = attributes.first.is_a?(String) ? attributes.first : Related.generate_id
  @attributes = attributes.last
end
weight(&block) click to toggle source
# File lib/related/relationship.rb, line 35
def self.weight(&block)
  @weight = block
end

Private Class Methods

weight_for(relationship, direction) click to toggle source
# File lib/related/relationship.rb, line 69
def self.weight_for(relationship, direction)
  if @weight
    relationship.instance_exec(direction, &@weight).to_i
  else
    Time.now.to_f
  end
end

Public Instance Methods

decrement_weight!(direction, by = 1) click to toggle source
# File lib/related/relationship.rb, line 31
def decrement_weight!(direction, by = 1)
  Related.redis.zincrby(r_key(direction), -by.to_f, self.id)
end
end_node() click to toggle source
# File lib/related/relationship.rb, line 15
def end_node
  @end_node ||= Related::Node.find(end_node_id)
end
increment_weight!(direction, by = 1) click to toggle source
# File lib/related/relationship.rb, line 27
def increment_weight!(direction, by = 1)
  Related.redis.zincrby(r_key(direction), by.to_f, self.id)
end
rank(direction) click to toggle source
# File lib/related/relationship.rb, line 19
def rank(direction)
  Related.redis.zrevrank(r_key(direction), self.id)
end
start_node() click to toggle source
# File lib/related/relationship.rb, line 11
def start_node
  @start_node ||= Related::Node.find(start_node_id)
end
weight(direction) click to toggle source
# File lib/related/relationship.rb, line 23
def weight(direction)
  Related.redis.zscore(r_key(direction), self.id).to_f
end

Private Instance Methods

create() click to toggle source
Calls superclass method Related::Entity::create
# File lib/related/relationship.rb, line 77
def create
  #Related.redis.multi do
    super
    Related.redis.zadd(r_key(:out), self.class.weight_for(self, :out), self.id)
    Related.redis.zadd(r_key(:in), self.class.weight_for(self, :in), self.id)
    Related.redis.sadd(n_key(:out), self.end_node_id)
    Related.redis.sadd(n_key(:in), self.start_node_id)
    Related.redis.set(dir_key, self.id)
  #end
  Related.execute_data_flow(self.label, self)
  self
end
delete() click to toggle source
Calls superclass method Related::Entity#delete
# File lib/related/relationship.rb, line 96
def delete
  #Related.redis.multi do
    Related.redis.zrem(r_key(:out), self.id)
    Related.redis.zrem(r_key(:in), self.id)
    Related.redis.srem(n_key(:out), self.end_node_id)
    Related.redis.srem(n_key(:in), self.start_node_id)
    Related.redis.del(dir_key)
    super
  #end
  Related.execute_data_flow(self.label, self)
  self
end
dir_key() click to toggle source
# File lib/related/relationship.rb, line 65
def dir_key
  "#{self.start_node_id}:#{self.label}:#{self.end_node_id}"
end
n_key(direction) click to toggle source
# File lib/related/relationship.rb, line 57
def n_key(direction)
  if direction.to_sym == :out
    "#{self.start_node_id}:n:#{self.label}:out"
  elsif direction.to_sym == :in
    "#{self.end_node_id}:n:#{self.label}:in"
  end
end
r_key(direction) click to toggle source
# File lib/related/relationship.rb, line 49
def r_key(direction)
  if direction.to_sym == :out
    "#{self.start_node_id}:r:#{self.label}:out"
  elsif direction.to_sym == :in
    "#{self.end_node_id}:r:#{self.label}:in"
  end
end
update() click to toggle source
Calls superclass method Related::Entity#update
# File lib/related/relationship.rb, line 90
def update
  super
  Related.execute_data_flow(self.label, self)
  self
end