Class: Ivy::Serializers::Formats::JSON

Inherits:
Object
  • Object
show all
Defined in:
lib/ivy/serializers/formats/json.rb

Direct Known Subclasses

ActiveModelSerializers, JSONAPI

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ JSON

Returns a new instance of JSON.



8
9
10
11
# File 'lib/ivy/serializers/formats/json.rb', line 8

def initialize(document)
  @document = document
  @hash_gen = HashGenerator.new
end

Instance Method Details

#as_jsonObject



13
14
15
16
# File 'lib/ivy/serializers/formats/json.rb', line 13

def as_json(*)
  @document.generate(self)
  @hash_gen.to_h
end

#attribute(key, value) ⇒ Object



18
19
20
# File 'lib/ivy/serializers/formats/json.rb', line 18

def attribute(key, value)
  @hash_gen.store(key, value)
end

#belongs_to(name, resource, options = {}) ⇒ Object



22
23
24
# File 'lib/ivy/serializers/formats/json.rb', line 22

def belongs_to(name, resource, options={})
  id(name, resource)
end

#document(document) ⇒ Object



26
27
28
29
# File 'lib/ivy/serializers/formats/json.rb', line 26

def document(document)
  document.generate_primary_resource(self)
  document.generate_linked(self)
end

#has_many(name, resources, options = {}) ⇒ Object



31
32
33
# File 'lib/ivy/serializers/formats/json.rb', line 31

def has_many(name, resources, options={})
  ids(name, resources)
end

#id(key, resource) ⇒ Object



35
36
37
# File 'lib/ivy/serializers/formats/json.rb', line 35

def id(key, resource)
  attribute(key, extract_id(resource))
end

#ids(key, resources) ⇒ Object



39
40
41
# File 'lib/ivy/serializers/formats/json.rb', line 39

def ids(key, resources)
  attribute(key, resources.map { |resource| extract_id(resource) })
end

#linked(document) ⇒ Object



43
44
45
# File 'lib/ivy/serializers/formats/json.rb', line 43

def linked(document)
  document.generate_linked_resources(self)
end

#linked_resources(resource_class, resources) ⇒ Object



47
48
49
50
# File 'lib/ivy/serializers/formats/json.rb', line 47

def linked_resources(resource_class, resources)
  key = key_for_collection(resource_class).to_sym
  @hash_gen.store_array(key) { resources(resources) }
end


52
53
54
# File 'lib/ivy/serializers/formats/json.rb', line 52

def links(resource)
  @document.generate_links(self, resource)
end

#primary_resource(primary_resource_name, primary_resource) ⇒ Object



56
57
58
# File 'lib/ivy/serializers/formats/json.rb', line 56

def primary_resource(primary_resource_name, primary_resource)
  @hash_gen.store_object(primary_resource_name) { resource(primary_resource) }
end

#primary_resources(primary_resources_name, primary_resources) ⇒ Object



60
61
62
# File 'lib/ivy/serializers/formats/json.rb', line 60

def primary_resources(primary_resources_name, primary_resources)
  @hash_gen.store_array(primary_resources_name) { resources(primary_resources) }
end

#resource(resource) ⇒ Object



64
65
66
# File 'lib/ivy/serializers/formats/json.rb', line 64

def resource(resource)
  @document.generate_resource(self, resource)
end

#resources(resources) ⇒ Object



68
69
70
# File 'lib/ivy/serializers/formats/json.rb', line 68

def resources(resources)
  resources.each { |resource| @hash_gen.push_object { resource(resource) } }
end

#type(key, resource) ⇒ Object



72
73
74
# File 'lib/ivy/serializers/formats/json.rb', line 72

def type(key, resource)
  attribute(key, extract_type(resource))
end