/
node.rb
116 lines (98 loc) · 2.91 KB
/
node.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module Morpheus
module ActsAsNode
extend ActiveSupport::Concern
included do
include ActiveModel::Validations
include ActiveModel::Conversion
include HasProperties
include ActsAsPersistent
include ActsAsRestful
include Validators # To allow using as `validates :whatever`
end
module InstanceMethods
def method_missing(method, *args, &block)
#TODO: Redefine respond_to? and respond_to_missing?
name = method.to_s
bang = name[-1] == '!'
if bang
# person.likes! # All relations for :likes
# person.likes!(:in) # All incomming relations for :likes
get_relations(name, args[0])
elsif args.length == 0
# person.name # Property
# person.likes # Related nodes
if self.class.has_relation?(name)
get_relation_nodes(name)
else
get_property(name)
end
elsif args.length == 1
# person.name = 'abc' # Set the property
# person.likes(her) # Set relation to her
# person.likes(:in) # Get incomming :likes relations
if is_a_property_setter?(name, *args)
set_property(name, args[0])
else
arg = args[0]
arg.respond_to?(:to_sym) ? get_relation_nodes(name, arg) : set_relation(name, arg)
end
elsif args.length == 2
# person.likes(her, :in)
set_relation(name, *args)
else
super
end
end
def set_relation_node(name, node, direction=nil)
set_relation(name, node, direction)
end
def get_relation_nodes(name, direction=nil)
get_relations(name, direction).map{|r| r.to }
end
def set_relation(name, node, direction=nil)
name = name.to_s.sub(/!/, '')
key = name.to_sym
direction ||= :out
rel = Relationship.new_with_direction(key, self, node, direction)
existing = get_relations(name, direction).select{|r| r == rel}.first
return existing if existing
@relations[key] ||= []
@relations[key] << rel
rel
end
def get_relations(name, direction=nil)
name = name.to_s.sub(/!/, '')
key = name.to_sym
@relations ||= {}
all = @relations[name.to_sym] || []
if direction == :in
all.select{|r| r.to == self}
elsif direction == :out
all.select{|r| r.from == self}
else
all
end
end
def query
Query.new(self)
end
end
module ClassMethods
def relation(*names)
@relations = names
def relations
@relations
end
end
def has_relation?(name)
respond_to?(:relations) && (relations || []).include?(name.to_sym)
end
def api_endpoint
:nodes
end
end
end
class Node
include ActsAsNode
end
end