-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_data.rb
More file actions
141 lines (124 loc) · 3.8 KB
/
Copy pathsource_data.rb
File metadata and controls
141 lines (124 loc) · 3.8 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
require "./log"
class SourceData
attr_accessor :info
def initialize(id)
@info = { 'id' => id }
end
def merge_with(other)
$logger.debug "merge_with: %s %s %s %s %s %s" % [self.info["id"], self, self.info, other.info["id"], other, other.info]
return if self.equal? other
other.info.each do |key, value|
if @info.key? key
if value.class == Hash
@info.merge(value) { |key, old_val, new_val|
msg = "Info hash value disagree: %s %s %s" % [key, old_val, new_val]
$logger.debug msg
throw :bad_merge, msg
}
elsif value.class == Array
$logger.debug "%s %s %s" % [key, @info[key], value]
$logger.debug "%s %s" % [self, other]
if @info.key? key
@info[key] = (@info[key] + value).uniq
else
@info[key] = value.uniq
end
else
if @info[key] != value
msg = "Info value not hash and not an Array. key:%s class:%s value:%s old value %s" % [key, value.class, value, @info[key]]
$logger.debug msg
throw :bad_merge, msg
end
end
else
@info[key] = value
end
end
end
end
def merge_source_data_maps(first, second)
$logger.debug "merge source data maps: %s %s" % [first, second]
second.each do |id, source_data|
if first.has_key?(id)
$logger.debug "%s %s %s" % [id, first[id], source_data]
first[id].merge_with(source_data)
else
first[id] = source_data
end
end
end
class SourceMember < SourceData
attr_accessor :projects
attr_accessor :events
def initialize(id)
super(id)
@projects = {}
@events = {}
end
def to_json(options={})
begin
$logger.debug "%s" % @info["id"]
{ 'info' => @info, 'projects' => @projects, 'events' => @events }.to_json(options)
rescue JSON::NestingError => e
$logger.debug "%s %s" % [@info["id"], e]
{ 'info' => @info }.to_json()
end
end
def merge_with(other)
return if self.equal? other
super # Merge info
merge_source_data_maps(@projects, other.projects)
merge_source_data_maps(@events, other.events)
$logger.debug "merge_with_end: %s %s %s %s %s %s" % [self.info["id"], self, self.info, other.info["id"], other, other.info]
end
end
class SourceProject < SourceData
attr_accessor :members
attr_accessor :events
def initialize(id)
super(id)
@members = {}
@events = {}
end
def to_json(options={})
begin
$logger.debug "%s" % @info["id"]
{ 'info' => @info, 'members' => @members, 'events' => @events }.to_json(options)
rescue JSON::NestingError => e
$logger.debug "%s %s" % [@info["id"], e]
{ 'info' => @info }.to_json()
end
end
def merge_with(other)
return if self.equal? other
super # Merge info
merge_source_data_maps(@members, other.members)
merge_source_data_maps(@events, other.events)
$logger.debug "merge_with_end: %s %s %s %s %s %s" % [self.info["id"], self, self.info, other.info["id"], other, other.info]
end
end
class SourceEvent < SourceData
attr_accessor :members
attr_accessor :projects
def initialize(id)
super(id)
@members = {}
@projects = {}
end
def to_json(options={})
begin
$logger.debug "%s" % @info["id"]
{ 'info' => @info, 'members' => @members, 'projects' => @projects }.to_json(options)
rescue JSON::NestingError => e
$logger.debug "%s %s" % [@info["id"], e]
{ 'info' => @info }.to_json()
end
end
def merge_with(other)
return if self.equal? other
super # Merge info
merge_source_data_maps(@members, other.members)
merge_source_data_maps(@events, other.events)
$logger.debug "merge_with_end: %s %s %s %s %s %s" % [self.info["id"], self, self.info, other.info["id"], other, other.info]
end
end