Class: SeapigObject

Inherits:
Hash
  • Object
show all
Defined in:
lib/seapig/client.rb

Direct Known Subclasses

SeapigWildcardObject

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, object_id, parent = nil) ⇒ SeapigObject

Returns a new instance of SeapigObject.



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/seapig/client.rb', line 173

def initialize(server, object_id, parent = nil)
	@server = server
	@object_id = object_id
	@version = 0
	@onchange_proc = nil
	@onproduce_proc = nil
	@valid = false
	@shadow = JSON.load(JSON.dump(self))
	@stall = false
	@parent = parent
	@destroyed = false
end

Instance Attribute Details

#destroyedObject

Returns the value of attribute destroyed.



164
165
166
# File 'lib/seapig/client.rb', line 164

def destroyed
  @destroyed
end

#object_idObject

Returns the value of attribute object_id.



164
165
166
# File 'lib/seapig/client.rb', line 164

def object_id
  @object_id
end

#parentObject

Returns the value of attribute parent.



164
165
166
# File 'lib/seapig/client.rb', line 164

def parent
  @parent
end

#received_atObject (readonly)

Returns the value of attribute received_at.



165
166
167
# File 'lib/seapig/client.rb', line 165

def received_at
  @received_at
end

#stallObject

Returns the value of attribute stall.



164
165
166
# File 'lib/seapig/client.rb', line 164

def stall
  @stall
end

#versionObject

Returns the value of attribute version.



164
165
166
# File 'lib/seapig/client.rb', line 164

def version
  @version
end

#version_requestedObject

Returns the value of attribute version_requested.



164
165
166
# File 'lib/seapig/client.rb', line 164

def version_requested
  @version_requested
end

Instance Method Details

#matches?(id) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/seapig/client.rb', line 168

def matches?(id)
	id =~ Regexp.new(Regexp.escape(@object_id).gsub('\*','.*?'))
end

#onchange(&block) ⇒ Object



187
188
189
# File 'lib/seapig/client.rb', line 187

def onchange(&block)
	@onchange_proc = block
end

#onproduce(&block) ⇒ Object



192
193
194
# File 'lib/seapig/client.rb', line 192

def onproduce(&block)
	@onproduce_proc = block
end

#patch(message) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/seapig/client.rb', line 197

def patch(message)
	@received_at = Time.new
	old_data = @shadow
	if (not message['old_version']) or (message['old_version'] == 0) or (message['value'])
		self.clear
	elsif not @version == message['old_version']
		p @version, message
		puts "Seapig lost some updates, this should never happen"
		exit 2
	end
	if message['value']
		self.merge!(message['value'])
	else
		Hana::Patch.new(message['patch']).apply(self)
	end
	@version = message['new_version']
	@valid = true
	@shadow = JSON.load(JSON.dump(self))
	@onchange_proc.call(self) if @onchange_proc and old_data != @shadow
end

#produce(object_id, version) ⇒ Object



275
276
277
278
279
280
281
282
# File 'lib/seapig/client.rb', line 275

def produce(object_id, version)
	@version_requested = version
	if @onproduce_proc
		@onproduce_proc.call(self)
	else
		self.upload(0,{},object_id)
	end
end

#resetObject



219
220
221
222
223
# File 'lib/seapig/client.rb', line 219

def reset()
	@version = 0
	@shadow = {}
	self
end

#sanitizedObject



247
248
249
# File 'lib/seapig/client.rb', line 247

def sanitized
	JSON.load(JSON.dump(self))
end

#send(new_version = nil) ⇒ Object



238
239
240
241
242
243
244
# File 'lib/seapig/client.rb', line 238

def send(new_version=nil)
	old_version = @version
	old_object = @shadow
	@version = (new_version or (Time.new.to_f*1000000).to_i)
	@shadow = sanitized
	upload(old_version, old_object, @object_id)
end

#set(data) ⇒ Object



226
227
228
229
230
231
232
233
234
235
# File 'lib/seapig/client.rb', line 226

def set(data)
	if data
		@stall = false
		self.clear
		self.merge!(data)
	else
		@stall = true
	end
	self
end

#upload(old_version, old_object, object_id) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/seapig/client.rb', line 252

def upload(old_version, old_object, object_id)
	message = {
		id: object_id,
		action: 'object-patch',
		old_version: old_version,
		new_version: @version,
	}
	if old_version == 0 or @stall
		message.merge!(value: (if @stall then false else @shadow end))
	else
		diff = JsonDiff.generate(old_object, @shadow)
		value = @shadow
		if JSON.dump(diff.size) < JSON.dump(value.size) #can we afford this?
			message.merge!(patch: diff)
		else
			message.merge!(old_version: 0, value: @shadow)
		end
	end
	@server.socket.send JSON.dump(message)
	self
end

#valid?Boolean

Returns:

  • (Boolean)


285
286
287
# File 'lib/seapig/client.rb', line 285

def valid?
	@valid
end