module Caliper::Jsonable

Public Class Methods

included(base) click to toggle source
# File lib/caliper/jsonable.rb, line 26
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

eql?(other) click to toggle source
# File lib/caliper/jsonable.rb, line 66
def eql?(other)
  @context == other.context && @apiKey == other.apiKey
  # @context == other.context && @sensorId == other.sensorId && @apiKey == other.apiKey
end
from_json(json_hash) click to toggle source
# File lib/caliper/jsonable.rb, line 53
def from_json json_hash
  data = json_hash
  # puts "Jsonable: from_json: json_hash = #{json_hash}"
  # self.context = data['@context']
  # self.type = data['@type']
  json_hash.each do | key, value |
    next if (key[1..-1] == 'context' || key[1..-1] == 'type')
    # puts "Jsonable - adding #{key} : #{value}"
    self.instance_variable_set "@#{key}", value
  end
  return self
end
to_json(*a) click to toggle source
# File lib/caliper/jsonable.rb, line 33
def to_json(*a)
  # puts 'Jsonable: to_json invoked'
  result = {}
  # result['@context'] = self.context
  # result['@type'] = self.type
  self.instance_variables.each do |key|
    # puts "got key = #{key}"
    next if (key[1..-1] == 'context' || key[1..-1] == 'type')
    value = self.instance_variable_get key
    # puts "setting #{key}: #{value}"
    attribute_key = key[1..-1]
    if (key[1..-1] == 'id' || key[1..-1] == 'type' || key[1..-1] == 'context')
            ## prefix with @ char for linked json data
            attribute_key = "@#{attribute_key}"
    end
    result[attribute_key] = value
  end
  result.to_json(*a)
end