graphiti 1.0.alpha.7 → 1.0.alpha.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fcbf70210b7de15182940d216ec6161f2f4bc639
4
- data.tar.gz: d70f215f37380a952424c8f4f56ab470fe3f9b53
3
+ metadata.gz: 68063fcedb7deeeb604ebb6c88e58fe525867ee8
4
+ data.tar.gz: cb720f2dcada7d2b92332e8b734ff9fcdc879c01
5
5
  SHA512:
6
- metadata.gz: e8f1118f4f3f7fb7a4761cdf7b664120ee88927f8a2c77bd4913930257f1f110eae512377c846e0d110bc8a73ab364dfae56705cff9444b33e7cee1230fb565d
7
- data.tar.gz: adb45fc6a42f3dae9eb2815b5cab2552a2ae77f15e8e4a9189ae5dd4a573b3d03e9b274c97c732f4a38e41e0cdb65a9cc40b9469b1625a9fbd3c989d2168d94c
6
+ metadata.gz: 9bba382cda2e598726ab189f9b38e256fce145cbe911c9342d706811930da89ae76947f011ff86dacb3e7265b1da305e7074e9471eb7e7d9bbf45ee1ac24484f
7
+ data.tar.gz: aa1ab4574c5bb2a51fb0c76d251c0061e44af47287e7906121d566697fa9eb8a8f4cddf65d9dfe7bd2c33f402761a9b2911f4ac37925175cfe55233535f4b1bb
@@ -14,22 +14,31 @@ module Graphiti
14
14
  end
15
15
 
16
16
  def to_jsonapi
17
- render(JSONAPI::Renderer.new).to_json
17
+ render(self.class.jsonapi_renderer).to_json
18
18
  end
19
19
 
20
20
  def to_json
21
- render(Graphiti::HashRenderer.new(@proxy.resource)).to_json
21
+ render(self.class.hash_renderer(@proxy)).to_json
22
22
  end
23
23
 
24
24
  def to_xml
25
- render(Graphiti::HashRenderer.new(@proxy.resource)).to_xml(root: :data)
25
+ render(self.class.hash_renderer(@proxy)).to_xml(root: :data)
26
+ end
27
+
28
+ def self.jsonapi_renderer
29
+ @jsonapi_renderer ||= JSONAPI::Serializable::Renderer
30
+ .new(JSONAPI::Renderer.new)
31
+ end
32
+
33
+ def self.hash_renderer(proxy)
34
+ implementation = Graphiti::HashRenderer.new(proxy.resource)
35
+ JSONAPI::Serializable::Renderer.new(implementation)
26
36
  end
27
37
 
28
38
  private
29
39
 
30
- def render(implementation)
40
+ def render(renderer)
31
41
  notify do
32
- instance = JSONAPI::Serializable::Renderer.new(implementation)
33
42
  options[:fields] = proxy.fields
34
43
  options[:expose] ||= {}
35
44
  options[:expose][:extra_fields] = proxy.extra_fields
@@ -37,7 +46,7 @@ module Graphiti
37
46
  options[:include] = proxy.include_hash
38
47
  options[:meta] ||= {}
39
48
  options[:meta].merge!(stats: proxy.stats) unless proxy.stats.empty?
40
- instance.render(records, options)
49
+ renderer.render(records, options)
41
50
  end
42
51
  end
43
52
 
@@ -61,7 +61,7 @@ module Graphiti
61
61
  end
62
62
 
63
63
  def typecast(name, value, flag)
64
- att = get_attr!(name, flag)
64
+ att = get_attr!(name, flag, request: true)
65
65
  type = Graphiti::Types[att[:type]]
66
66
  return if value.nil? && type[:kind] != 'array'
67
67
  begin
@@ -185,6 +185,10 @@ module Graphiti
185
185
  attributes.merge(extra_attributes)
186
186
  end
187
187
 
188
+ def attribute_cache
189
+ @attribute_cache ||= all_attributes
190
+ end
191
+
188
192
  def sideloads
189
193
  config[:sideloads]
190
194
  end
@@ -56,6 +56,10 @@ module Graphiti
56
56
  attribute[flag] != :required
57
57
  end
58
58
 
59
+ def cache?
60
+ !!@cache
61
+ end
62
+
59
63
  def error_class
60
64
  Errors::AttributeError
61
65
  end
@@ -64,8 +68,15 @@ module Graphiti
64
68
  attribute[flag] != false
65
69
  end
66
70
 
71
+ # If running in a request context, we've already loaded everything
72
+ # and there's no reason to perform logic, so go through attriubte cache
73
+ # Otherwise, this is a performance hit during typecasting
67
74
  def attribute
68
- resource.all_attributes[name]
75
+ @attribute ||= if request?
76
+ resource.class.attribute_cache[name]
77
+ else
78
+ resource.all_attributes[name]
79
+ end
69
80
  end
70
81
 
71
82
  def attribute?
@@ -1,3 +1,3 @@
1
1
  module Graphiti
2
- VERSION = "1.0.alpha.7"
2
+ VERSION = "1.0.alpha.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphiti
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.alpha.7
4
+ version: 1.0.alpha.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-21 00:00:00.000000000 Z
11
+ date: 2018-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializable