Class: Ivy::Serializers::Formats::JSON
- Inherits:
-
Object
- Object
- Ivy::Serializers::Formats::JSON
show all
- Defined in:
- lib/ivy/serializers/formats/json.rb
Instance Method Summary
collapse
-
#as_json ⇒ Object
-
#attribute(key, value) ⇒ Object
-
#attributes(resource) ⇒ Object
-
#belongs_to(name, resource, options = {}) ⇒ Object
-
#document(document) ⇒ Object
-
#has_many(name, resources, options = {}) ⇒ Object
-
#id(key, resource) ⇒ Object
-
#ids(key, resources) ⇒ Object
-
#included(document) ⇒ Object
-
#included_resources(included_resources) ⇒ Object
-
#initialize(document) ⇒ JSON
constructor
-
#primary_resource(primary_resource_name, primary_resource) ⇒ Object
-
#primary_resources(primary_resources_name, primary_resources) ⇒ Object
-
#relationships(resource) ⇒ Object
-
#resource(resource) ⇒ Object
-
#resources(resources) ⇒ Object
-
#type(key, resource) ⇒ Object
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_json ⇒ Object
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, (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| (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(included_resources) ⇒ Object
51
52
53
54
55
56
|
# File 'lib/ivy/serializers/formats/json.rb', line 51
def included_resources(included_resources)
included_resources.each_pair do |resource_class, resources|
key = key_for_collection(resource_class).to_sym
@hash_gen.store_array(key) { resources(resources) }
end
end
|
#primary_resource(primary_resource_name, primary_resource) ⇒ Object
58
59
60
|
# File 'lib/ivy/serializers/formats/json.rb', line 58
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
62
63
64
|
# File 'lib/ivy/serializers/formats/json.rb', line 62
def primary_resources(primary_resources_name, primary_resources)
@hash_gen.store_array(primary_resources_name) { resources(primary_resources) }
end
|
#relationships(resource) ⇒ Object
66
67
68
|
# File 'lib/ivy/serializers/formats/json.rb', line 66
def relationships(resource)
@document.generate_relationships(self, resource)
end
|
#resource(resource) ⇒ Object
70
71
72
73
|
# File 'lib/ivy/serializers/formats/json.rb', line 70
def resource(resource)
id(:id, resource)
@document.generate_resource(self, resource)
end
|
#resources(resources) ⇒ Object
75
76
77
|
# File 'lib/ivy/serializers/formats/json.rb', line 75
def resources(resources)
resources.each { |resource| @hash_gen.push_object { resource(resource) } }
end
|
#type(key, resource) ⇒ Object
79
80
81
|
# File 'lib/ivy/serializers/formats/json.rb', line 79
def type(key, resource)
attribute(key, (resource))
end
|