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

#attributes(resource) ⇒ Object



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

def attributes(resource)
  @document.generate_attributes(self, resource)
end

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



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

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

#document(document) ⇒ Object



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

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

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



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

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

#id(key, resource) ⇒ Object



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

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

#ids(key, resources) ⇒ Object



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

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

#included(document) ⇒ Object



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

def included(document)
  document.generate_included_resources(self)
end

#included_resources(resource_class, resources) ⇒ Object



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

def included_resources(resource_class, resources)
  key = key_for_collection(resource_class).to_sym
  @hash_gen.store_array(key) { resources(resources) }
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

#relationships(resource) ⇒ Object



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

def relationships(resource)
  @document.generate_relationships(self, resource)
end

#resource(resource) ⇒ Object



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

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

#resources(resources) ⇒ Object



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

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

#type(key, resource) ⇒ Object



77
78
79
# File 'lib/ivy/serializers/formats/json.rb', line 77

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