medea 0.2.22 → 0.2.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/medea/jasondeferredquery.rb +15 -7
- data/lib/medea/jasonobject.rb +60 -13
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.23
|
@@ -1,12 +1,16 @@
|
|
1
1
|
module Medea
|
2
2
|
class JasonDeferredQuery
|
3
3
|
require 'rest_client'
|
4
|
+
require 'uri'
|
4
5
|
|
5
6
|
attr_accessor :time_limit, :result_format, :type, :time_limit, :state, :contents, :filters
|
6
7
|
|
7
8
|
def initialize a_class, format=:search
|
8
9
|
self.type = a_class
|
9
|
-
self.filters = {
|
10
|
+
self.filters = { :VERSION0 => nil }
|
11
|
+
if self.type
|
12
|
+
self.filters[:FILTER] = {:HTTP_X_CLASS => a_class.name.to_s}
|
13
|
+
end
|
10
14
|
self.result_format = format
|
11
15
|
self.time_limit = 0
|
12
16
|
self.state = :prefetch
|
@@ -70,10 +74,10 @@ module Medea
|
|
70
74
|
val.each do |field ,value|
|
71
75
|
if value.is_a? Array
|
72
76
|
value.each do |i|
|
73
|
-
filter_array << "#{name.to_s}=#{field}:#{i}"
|
77
|
+
filter_array << "#{name.to_s}=#{URI.escape(field)}:#{URI.escape(i)}"
|
74
78
|
end
|
75
79
|
else
|
76
|
-
filter_array << "#{name.to_s}=#{field.to_s}:#{value.to_s}"
|
80
|
+
filter_array << "#{name.to_s}=#{URI.escape(field.to_s)}:#{URI.escape(value.to_s)}"
|
77
81
|
end
|
78
82
|
end
|
79
83
|
end
|
@@ -100,7 +104,7 @@ module Medea
|
|
100
104
|
end
|
101
105
|
#end array interface
|
102
106
|
|
103
|
-
def execute_query
|
107
|
+
def execute_query
|
104
108
|
#hit the URL
|
105
109
|
#fill self.contents with :ghost versions of JasonObjects
|
106
110
|
begin
|
@@ -114,17 +118,21 @@ module Medea
|
|
114
118
|
/\/([^\/]*)\/([^\/]*)$/.match result[k]["POST_TO"]
|
115
119
|
#$1 is the class name, $2 is the key
|
116
120
|
item = type.new($2, :lazy)
|
117
|
-
if result[k].has_key? "CONTENT" &&
|
121
|
+
if result[k].has_key? "CONTENT" && result[k]["CONTENT"] != ""
|
118
122
|
item.instance_variable_set(:@__jason_data, result[k]["CONTENT"])
|
119
123
|
item.instance_variable_set(:@__jason_state, :stale)
|
120
124
|
end
|
125
|
+
if result[k].has_key? "HTTP_X_PARENT" && result[k]["HTTP_X_PARENT"] != ""
|
126
|
+
item.jason_parent_key = result[k]["HTTP_X_PARENT"]
|
127
|
+
end
|
121
128
|
self.contents << item
|
122
129
|
end
|
123
130
|
end
|
131
|
+
|
132
|
+
self.state = :postfetch
|
133
|
+
result
|
124
134
|
rescue
|
125
135
|
self.contents = []
|
126
|
-
ensure
|
127
|
-
self.state = :postfetch
|
128
136
|
end
|
129
137
|
end
|
130
138
|
end
|
data/lib/medea/jasonobject.rb
CHANGED
@@ -54,6 +54,31 @@ module Medea
|
|
54
54
|
end
|
55
55
|
#end query interface
|
56
56
|
|
57
|
+
#the resolve method takes a key and returns the JasonObject that has that key
|
58
|
+
#This is useful when you have the key, but not the class
|
59
|
+
def JasonObject.resolve(key, mode=:lazy)
|
60
|
+
q = JasonDeferredQuery.new nil
|
61
|
+
q.filters[:FILTER] ||= {}
|
62
|
+
q.filters[:FILTER][:HTTP_X_KEY] = key
|
63
|
+
resp = JSON.parse(RestClient.get(q.to_url))
|
64
|
+
if resp.has_key? "1"
|
65
|
+
#this is the object, figure out its class
|
66
|
+
resp["1"]["POST_TO"] =~ /([^\/]+)\/#{key}/
|
67
|
+
begin
|
68
|
+
result = Kernel.const_get($1).get_by_key key, :lazy
|
69
|
+
if result["1"].has_key? "CONTENT"
|
70
|
+
result.instance_variable_set(:@__jason_data, result["1"]["CONTENT"])
|
71
|
+
result.instance_variable_set(:@__jason_state, :stale)
|
72
|
+
end
|
73
|
+
if mode == :eager
|
74
|
+
result.send(:load)
|
75
|
+
end
|
76
|
+
rescue
|
77
|
+
nil
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
57
82
|
#"flexihash" access interface
|
58
83
|
def []=(key, value)
|
59
84
|
@__jason_data ||= {}
|
@@ -116,6 +141,26 @@ module Medea
|
|
116
141
|
|
117
142
|
def jason_parent
|
118
143
|
@__jason_parent ||= nil
|
144
|
+
if @__jason_parent == nil && @__jason_parent_key
|
145
|
+
#key is set but parent not? load the parent
|
146
|
+
@__jason_parent = JasonObject.resolve @__jason_parent_key
|
147
|
+
end
|
148
|
+
@__jason_parent
|
149
|
+
end
|
150
|
+
|
151
|
+
def jason_parent= parent
|
152
|
+
@__jason_parent = parent
|
153
|
+
@__jason_parent_key = parent.jason_key
|
154
|
+
end
|
155
|
+
|
156
|
+
def jason_parent_key
|
157
|
+
@__jason_parent_key ||= nil
|
158
|
+
end
|
159
|
+
|
160
|
+
def jason_parent_key= value
|
161
|
+
@__jason_parent_key = value
|
162
|
+
#reset the parent here?
|
163
|
+
@__jason_parent = nil
|
119
164
|
end
|
120
165
|
|
121
166
|
def jason_parent_list
|
@@ -126,10 +171,6 @@ module Medea
|
|
126
171
|
@__jason_parent_list = value
|
127
172
|
end
|
128
173
|
|
129
|
-
def jason_parent= parent
|
130
|
-
@__jason_parent = parent
|
131
|
-
end
|
132
|
-
|
133
174
|
#object persistence methods
|
134
175
|
|
135
176
|
#POSTs the current values of this object back to JasonDB
|
@@ -197,15 +238,21 @@ module Medea
|
|
197
238
|
def load
|
198
239
|
#because this object might be owned by another, we need to search by key.
|
199
240
|
#not passing a format to the query is a shortcut to getting just the object.
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
241
|
+
url = "#{JasonDB::db_auth_url}@0.content?"
|
242
|
+
params = [
|
243
|
+
"VERSION0",
|
244
|
+
"FILTER=HTTP_X_KEY:#{self.jason_key}"
|
245
|
+
]
|
246
|
+
|
247
|
+
if not self.class.owned
|
248
|
+
#if the class is owned, we don't want to filter by class name (X-CLASS will be the list name)
|
249
|
+
#if it isn't owned, it is safe to filter by X-CLASS
|
250
|
+
#if this item is "had" rather than owned, we MUST filter by class, otherwise we get the references.
|
251
|
+
params << "FILTER=HTTP_X_CLASS:#{self.class.name}"
|
252
|
+
end
|
253
|
+
|
254
|
+
url << params.join("&")
|
255
|
+
#url = "#{JasonDB::db_auth_url}#{self.class.name}/#{self.jason_key}"
|
209
256
|
|
210
257
|
#puts " = Retrieving #{self.class.name} at #{url}"
|
211
258
|
response = RestClient.get url
|