Class: SeapigObject
- Inherits:
-
Hash
- Object
- Hash
- SeapigObject
- Defined in:
- lib/seapig/client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#destroyed ⇒ Object
Returns the value of attribute destroyed.
-
#object_id ⇒ Object
Returns the value of attribute object_id.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#received_at ⇒ Object
readonly
Returns the value of attribute received_at.
-
#stall ⇒ Object
Returns the value of attribute stall.
-
#version ⇒ Object
Returns the value of attribute version.
-
#version_requested ⇒ Object
Returns the value of attribute version_requested.
Instance Method Summary collapse
-
#initialize(server, object_id, parent = nil) ⇒ SeapigObject
constructor
A new instance of SeapigObject.
- #matches?(id) ⇒ Boolean
- #onchange(&block) ⇒ Object
- #onproduce(&block) ⇒ Object
- #patch(message) ⇒ Object
- #produce(object_id, version) ⇒ Object
- #reset ⇒ Object
- #sanitized ⇒ Object
- #send(new_version = nil) ⇒ Object
- #set(data) ⇒ Object
- #upload(old_version, old_object, object_id) ⇒ Object
- #valid? ⇒ Boolean
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
#destroyed ⇒ Object
Returns the value of attribute destroyed.
164 165 166 |
# File 'lib/seapig/client.rb', line 164 def destroyed @destroyed end |
#object_id ⇒ Object
Returns the value of attribute object_id.
164 165 166 |
# File 'lib/seapig/client.rb', line 164 def object_id @object_id end |
#parent ⇒ Object
Returns the value of attribute parent.
164 165 166 |
# File 'lib/seapig/client.rb', line 164 def parent @parent end |
#received_at ⇒ Object (readonly)
Returns the value of attribute received_at.
165 166 167 |
# File 'lib/seapig/client.rb', line 165 def received_at @received_at end |
#stall ⇒ Object
Returns the value of attribute stall.
164 165 166 |
# File 'lib/seapig/client.rb', line 164 def stall @stall end |
#version ⇒ Object
Returns the value of attribute version.
164 165 166 |
# File 'lib/seapig/client.rb', line 164 def version @version end |
#version_requested ⇒ Object
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
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() @received_at = Time.new old_data = @shadow if (not ['old_version']) or (['old_version'] == 0) or (['value']) self.clear elsif not @version == ['old_version'] p @version, puts "Seapig lost some updates, this should never happen" exit 2 end if ['value'] self.merge!(['value']) else Hana::Patch.new(['patch']).apply(self) end @version = ['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 |
#reset ⇒ Object
219 220 221 222 223 |
# File 'lib/seapig/client.rb', line 219 def reset() @version = 0 @shadow = {} self end |
#sanitized ⇒ Object
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) = { id: object_id, action: 'object-patch', old_version: old_version, new_version: @version, } if old_version == 0 or @stall .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? .merge!(patch: diff) else .merge!(old_version: 0, value: @shadow) end end @server.socket.send JSON.dump() self end |
#valid? ⇒ Boolean
285 286 287 |
# File 'lib/seapig/client.rb', line 285 def valid? @valid end |