Class: Authorize::Graph::Edge

Inherits:
Redis::Hash show all
Includes:
Redis::ModelReference
Defined in:
lib/authorize/graph/edge.rb

Overview

An edge connects two vertices. The order in which the vertices are supplied is preserved and can be used to imply direction. TODO: persist the connected vertices in an array. TODO: a hyperedge can be modeled with a set of vertices instead of explicit left and right vertices.

Constant Summary

Constants inherited from Redis::Base

Redis::Base::NAMESPACE_SEPARATOR

Instance Attribute Summary

Attributes inherited from Redis::Base

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redis::ModelReference

load_reference, set_reference

Methods inherited from Redis::Hash

#__getobj__, #get, #merge, #set

Methods inherited from Redis::Base

#==, #__getobj__, #_dump, _load, connection, connection_base?, connection_manager, #db, #eql?, #exists?, generate_key, #hash, index, load, #logger, #method_missing, new, next_counter, #reload, #respond_to?, subordinate_key, #subordinate_key, #to_yaml

Constructor Details

#initialize(v0, v1, properties = {}) ⇒ Edge

Returns a new instance of Edge.



22
23
24
25
26
27
28
29
# File 'lib/authorize/graph/edge.rb', line 22

def initialize(v0, v1, properties = {})
  super()
  set_reference(subordinate_key('l_id'), v0)
  set_reference(subordinate_key('r_id'), v1)
  v0.outbound_edges << self
  v1.inbound_edges << self
  merge(properties) if properties.any?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Authorize::Redis::Base

Class Method Details

.exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/authorize/graph/edge.rb', line 10

def self.exists?(id)
  super(subordinate_key(id, 'l_id'))
end

.load_all(namespace = name) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/authorize/graph/edge.rb', line 14

def self.load_all(namespace = name)
  redis_glob = subordinate_key(namespace, '*', 'l_id')
  re = Regexp.new(subordinate_key(namespace, ".+(?=#{NAMESPACE_SEPARATOR})"))
  keys = db.keys(redis_glob)
  keys = keys.map{|m| m.slice(re)}
  keys.map{|id| load(id)}
end

Instance Method Details

#destroyObject



45
46
47
48
49
50
51
# File 'lib/authorize/graph/edge.rb', line 45

def destroy
  from && from.outbound_edges.delete(self)
  to && to.inbound_edges.delete(self)
  self.class.db.del(subordinate_key('l_id'))
  self.class.db.del(subordinate_key('r_id'))
  super
end

#fromObject Also known as: left



31
32
33
# File 'lib/authorize/graph/edge.rb', line 31

def from
  load_reference(subordinate_key('l_id'), Vertex)
end

#toObject Also known as: right



36
37
38
# File 'lib/authorize/graph/edge.rb', line 36

def to
  load_reference(subordinate_key('r_id'), Vertex)
end

#valid?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/authorize/graph/edge.rb', line 53

def valid?
  from && to && super
end

#verticesObject



41
42
43
# File 'lib/authorize/graph/edge.rb', line 41

def vertices
  [from, to]
end