Module: Caliper::Entities::Jsonable

Included in:
Entity, EntityBase
Defined in:
lib/caliper/entities/jsonable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



28
29
30
# File 'lib/caliper/entities/jsonable.rb', line 28

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/caliper/entities/jsonable.rb', line 74

def eql?(other)
  @context == other.context && @id == other.id && @type == other.type && @name == other.name && @description == other.description && @dateCreated == other.dateCreated && @dateModified == other.dateModified
end

#from_json(json_hash) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/caliper/entities/jsonable.rb', line 59

def from_json json_hash
  data = json_hash
  # puts "Jsonable: from_json: json_hash = #{json_hash}"
  self.context = data['@context']
  self.id = data['@id']
  self.type = data['@type']
  self.name = data['name']
  json_hash.each do | key, value |
    next if (key[1..-1] == 'context' || key[1..-1] == 'id' || key[1..-1] == 'type')
    # puts "Jsonable - adding #{key} : #{value}"
    self.instance_variable_set "@#{key}", value
  end
  return self
end

#to_json(*a) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/caliper/entities/jsonable.rb', line 35

def to_json(*a)
  # puts 'Jsonable: to_json invoked'
  result = {}
  result['@context'] = self.context
  result['@id'] = self.id
  result['@type'] = self.type
  self.instance_variables.each do |key|
    # puts "got key = #{key}"
    next if (key[1..-1] == 'context' || key[1..-1] == 'id' || key[1..-1] == 'type')
    value = self.instance_variable_get key
    # puts "setting #{key}: #{value}"
    result[key[1..-1]] = value
  end

  # Filter out context for generic entities
  # A more generalized approach will be required for 1.1 to filter out all nulls/empties)
  if (result['@context'].nil?)
    #if (result['@type'] == Caliper::Entities::EntityType::ENTITY && result['@context'].nil?)
    result.delete('@context')
  end

  result.to_json(*a)
end