Class: DatawireQuarkCore::JSONObject

Inherits:
Object
  • Object
show all
Defined in:
lib/datawire-quark-core.rb

Constant Summary collapse

UNDEFINED =

Undefined

Class.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ JSONObject

Returns a new instance of JSONObject.



191
192
193
# File 'lib/datawire-quark-core.rb', line 191

def initialize(value=nil)
  @value = value
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



189
190
191
# File 'lib/datawire-quark-core.rb', line 189

def value
  @value
end

Class Method Details

.parse(json) ⇒ Object



195
196
197
# File 'lib/datawire-quark-core.rb', line 195

def self.parse(json)
  new JSON.parse("[#{json}]")[0]
end

Instance Method Details

#==(other) ⇒ Object



199
200
201
# File 'lib/datawire-quark-core.rb', line 199

def ==(other)
  not other.nil? and value == other.value
end

#getBoolObject

Bool



322
323
324
325
326
# File 'lib/datawire-quark-core.rb', line 322

def getBool
  return nil unless isBool

  value
end

#getListItem(index) ⇒ Object



279
280
281
282
283
# File 'lib/datawire-quark-core.rb', line 279

def getListItem(index)
  return undefined unless isList and (0...size).include? index

  self.class.new value[index]
end

#getNumberObject



312
313
314
315
316
# File 'lib/datawire-quark-core.rb', line 312

def getNumber
  return nil unless isNumber

  value
end

#getObjectItem(key) ⇒ Object



248
249
250
251
252
# File 'lib/datawire-quark-core.rb', line 248

def getObjectItem(key)
  return undefined unless isObject and value.key? key

  self.class.new value[key]
end

#getStringObject



298
299
300
301
302
# File 'lib/datawire-quark-core.rb', line 298

def getString
  return nil unless isString

  value
end

#getTypeObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/datawire-quark-core.rb', line 211

def getType
  case @value
    when Hash
      'object'
    when Array
      'list'
    when String
      'string'
    when Numeric
      'number'
    when true, false
      'bool'
    when nil
      'null'
    else
      raise RuntimeError, "Unknown JSONObject type #{@value.inspect}"
  end
end

#isBoolObject



328
329
330
# File 'lib/datawire-quark-core.rb', line 328

def isBool
  [true, false].include? value
end

#isDefinedObject



354
355
356
# File 'lib/datawire-quark-core.rb', line 354

def isDefined
  not isUndefined
end

#isListObject

List



269
270
271
# File 'lib/datawire-quark-core.rb', line 269

def isList
  value.is_a? Array
end

#isNullObject

Null



336
337
338
# File 'lib/datawire-quark-core.rb', line 336

def isNull
  value == nil
end

#isNumberObject

Number



308
309
310
# File 'lib/datawire-quark-core.rb', line 308

def isNumber
  value.is_a? Numeric
end

#isObjectObject

Object



238
239
240
# File 'lib/datawire-quark-core.rb', line 238

def isObject
  value.is_a? Hash
end

#isStringObject

String



294
295
296
# File 'lib/datawire-quark-core.rb', line 294

def isString
  value.is_a? String
end

#isUndefinedObject



350
351
352
# File 'lib/datawire-quark-core.rb', line 350

def isUndefined
  UNDEFINED.equal? value
end

#keysObject



261
262
263
264
265
# File 'lib/datawire-quark-core.rb', line 261

def keys
  return undefined unless isObject

  List.new value.keys
end

#set(value) ⇒ Object Also known as: setString, setNumber, setBool



230
231
232
233
234
# File 'lib/datawire-quark-core.rb', line 230

def set(value)
  @value = value

  self
end

#setListObject



273
274
275
276
277
# File 'lib/datawire-quark-core.rb', line 273

def setList
  @value = List.new

  self
end

#setListItem(index, item) ⇒ Object



285
286
287
288
289
290
# File 'lib/datawire-quark-core.rb', line 285

def setListItem(index, item)
  setList unless isList
  value[index] = item.value

  self
end

#setNullObject



340
341
342
343
344
# File 'lib/datawire-quark-core.rb', line 340

def setNull
  @value = nil

  self
end

#setObjectObject



242
243
244
245
246
# File 'lib/datawire-quark-core.rb', line 242

def setObject
  @value = {}

  self
end

#setObjectItem(key, item) ⇒ Object



254
255
256
257
258
259
# File 'lib/datawire-quark-core.rb', line 254

def setObjectItem(key, item)
  setObject unless isObject
  value[key] = item.value

  self
end

#sizeObject



207
208
209
# File 'lib/datawire-quark-core.rb', line 207

def size
  isList || isObject ? value.size : 1
end

#toStringObject



203
204
205
# File 'lib/datawire-quark-core.rb', line 203

def toString
  value.to_json
end

#undefinedObject



358
359
360
# File 'lib/datawire-quark-core.rb', line 358

def undefined
  self.class.new UNDEFINED
end