forked from jplewicke/rivulet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neo_classes.rb
127 lines (97 loc) · 3.63 KB
/
neo_classes.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
117
118
119
120
121
122
123
124
125
126
127
require 'neo4j'
require 'app_classes'
Neo4j::Config[:storage_path] = "#{Dir.pwd}/dbneo"
#Neo4j-based classes. Objects with these types are the ones that are actually persisted
#in the Neo4j graph store. Changing the properties for them may require a special
#migration for ALL existing database data. Changing the few helper methods we
#have for them shouldn't have a similar impact, but it's best to be cautious here.
#A CreditOffer is an offer by the source user to allow the destination user to owe
#up to a specified number of units to the source user.
#In double-entry book-keeping, the balance (amount_used + amount_held) of an outgoing
#CreditOffer is the value of debits(assets) that the destination user has assured the
#source user of.
#The balance of an incoming CreditOffer is the value of credits(liabilities) that are
#owed by the user.
#The credit limits of an outgoing CreditOffer that are offered represent
#the maximum number of debits that the source user is willing to accept from the
#destination user. The max_desired debits is the maximum number of debits that the
#destination user is willing to provide to the source user.
#The debits that are in amount_held are counted against the current credit limits.
#They are assumed to be reserved for repayment outside the credit network through
#provision of some sort of real-world service or good.
#The debits that are in amount_used are available for clearing the credits
#of the source user.
class CreditOffer
include Neo4j::RelationshipMixin
property :max_offered, :max_desired, :amount_used, :amount_held
def max
return [self.max_offered, self.max_desired].min
end
def usable
return [self.max - self.amount_used - self.amount_held,0.0].max
end
def empty?
return [self.max_offered, self.max_desired, self.amount_used,
self.amount_held] == [0,0,0,0]
end
end
#Each user is identified by a user_id and a secret, both assumed to be hashed values.
#Each user has a default depth of transactions that they are limited to in the
class User
include Neo4j::NodeMixin
property :user_id, :depth, :encrypted_password
has_n(:trusts).relationship(CreditOffer)
has_n(:activelytrusts)
has_n(:trusted_by).relationship(CreditOffer).from(:user)
index :user_id
def trustrel(dest)
if self.trusts.include?(dest)
rel = self.rels(:trusts).outgoing.find {|r| r.getEndNode == dest}
else
rel = self.trusts.new(dest)
rel.max_offered = 0.0
rel.max_desired = 0.0
rel.amount_used = 0.0
rel.amount_held = 0.0
end
rel
end
def creditors
self.outgoing(:trusts).incoming(:trusts).depth(1).collect {|dest| CreditRelationship.new(self, dest)}
end
def balance
self.creditors.collect {|creditRel| creditRel.source_offer.amount_used - creditRel.dest_offer.amount_used}.reduce {|a,b| a + b}
end
def self.fromid(id)
ret = nil
result = User.find(:user_id => id)
result.each {|x| ret = x}
if ret == nil
raise "No such user found."
end
return ret
rescue
nil
end
def self.creds_from_id(user_id)
user = self.fromid(user_id)
unless user.nil?
return user.credentials
end
nil
end
def credentials
return [self.user_id, BCrypt::Password.new(self.encrypted_password)]
end
def to_json
{
:user => self.user_id,
:depth => self.depth,
:balance => self.balance,
:account_url => "/accounts/#{self.user_id}",
:credit_url => "/credits/#{self.user_id}",
:transaction_url => "/transactions/#{self.user_id}",
:reservation_url => "/transactions/#{self.user_id}/held"
}.to_json
end
end