jsonapionify 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGU3ZWE5YTE4YTNiNjE1NTdiYzI2NTM2Y2UyYTA1ZTllNjVhZGQ3Ng==
4
+ ZGM2MDNiNjEzZTljODYzYWNlYWI3M2ViNmFkNGQ4NTU5OTU1NmZhYg==
5
5
  data.tar.gz: !binary |-
6
- OWViOTNhOGFmYmE2YmNhNGZhNDMzM2FhMzUzMmZmYTY2ZjY0NTNjZQ==
6
+ NDlmZjlkYWU2MzA0YTkwNTBmNGUwZGRlZDE5NjliYjExNjYzMTc2YQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MjhkNTFiZmZjNzc2ZjRmOTc2ZWQ2M2Y5ZGRkOWIyZTQzOTAxNTA5MDk0Yzg0
10
- YjgxYjJhYWNmOTg5M2E5MDZhZjc4ZTE4ODI5YTVjZjM0YTg1YzEzMjYyYjBh
11
- NmY2ZGRlMzAzY2Q5NTJkZGMwNGM1NjA2NzNhNmYxM2RmNGM1NGE=
9
+ Mzg3Y2I0NTk3ZTYxMmIxZDQxMmI2MzgzZWU5ZjQxZTZhYjRhYjA3ZGE3YWVh
10
+ NGIzZjBhMWNmNWVhZGUxZWEwNmMxN2YxZjcxMDI5N2RlYTI5NjU5ZDlkZmZi
11
+ MmFlNGZkN2ExN2JkODllNjI3ZGYxNGE5ZjQyMjRlNzUxM2U3YjI=
12
12
  data.tar.gz: !binary |-
13
- OWI4OWNkMWIyNTI3MDgzZGYyYzkxODZmMDNmNDI3MDYyYmZjYjZlZjI3YTFj
14
- NjRlNzQ0ODJkNGU4OTU1NGNhZTc4YmVhOTdlZTZjNDY0ZGVlMjZjODI3MDUw
15
- YzhkOTExMjViZTBiNjRlMmVkZTVjMjZlNWJmOGVkZTZhMjBhYjE=
13
+ OWM0ZmNhMmIxYmQzNTQ2ZTJmY2YzMWM4NThhZTYwOTMxOWQ3YzdhMDg5NTYy
14
+ MWQ3ODA3M2I5MmExMTc3YmM1ZjAyZWI2MGQ5ZmQ4ZWI5NmY3NDgzMjM2NDVm
15
+ ZDU4YmYyZDIyNzY1ZGE2OTBlMGZiZDc2YmQyNmEwYTk0NGMyZWE=
@@ -33,7 +33,6 @@ module JSONAPIonify::Api
33
33
  end
34
34
 
35
35
  def initialize_copy(new_instance)
36
- puts "DUPED!"
37
36
  super
38
37
  %i{@responses}.each do |ivar|
39
38
  value = instance_variable_get(ivar)
@@ -12,47 +12,52 @@ module JSONAPIonify::Api
12
12
  end
13
13
  end
14
14
 
15
- def initialize(request, instance, definitions, **overrides)
16
- memo = {}
17
- persisted_memo = {}
18
- delegate = self
19
- @definitions = definitions
20
-
21
- define_singleton_method :request do
22
- request
23
- end
15
+ attr_reader :request
16
+
17
+ def initialize(request, resource_instance, definitions, **overrides)
18
+ @memo = {}
19
+ @request = request
20
+ @persisted_memo = {}
21
+ @definitions = definitions
22
+ @overrides = overrides
23
+ @resource_instance = resource_instance
24
+ delegate = self
24
25
 
25
26
  %i{initialize_dup initialize_clone}.each do |method|
26
27
  define_singleton_method method do |copy|
27
- memo.each do |k, v|
28
+ @memo.each do |k, v|
28
29
  copy.public_send "#{k}=", v
29
30
  end
30
31
  end
31
32
  end
32
33
 
33
- define_singleton_method(:reset) do |key|
34
- memo.delete(key)
35
- end
36
-
37
- define_singleton_method(:clear) do
38
- memo.clear
39
- end
40
-
41
34
  definitions.each do |name, context|
35
+ raise Errors::ReservedContextName if respond_to? name
42
36
  define_singleton_method name do
43
- return persisted_memo[name] if persisted_memo.has_key? name
44
- (context.persisted? ? persisted_memo : memo)[name] ||=
45
- if overrides.has_key?(name)
46
- overrides[name]
47
- else
48
- context.call(instance, delegate)
49
- end
37
+ return @overrides[name] if @overrides.has_key? name
38
+ return @persisted_memo[name] if @persisted_memo.has_key? name
39
+ return @memo[name] if @memo.has_key? name
40
+ write_memo = (context.persisted? ? @persisted_memo : @memo)
41
+ write_memo[name] = context.call(@resource_instance, delegate)
50
42
  end
51
43
 
52
44
  define_singleton_method "#{name}=" do |value|
53
- persisted_memo[name] = value
45
+ @persisted_memo[name] = value
54
46
  end unless context.readonly?
55
47
  end
48
+ freeze
49
+ end
50
+
51
+ def reset key
52
+ @memo.delete(key)
53
+ end
54
+
55
+ def clear
56
+ @memo.clear
57
+ end
58
+
59
+ def inspect
60
+ to_s.chomp('>') << " memoed: #{@memo.keys.inspect}, persisted: #{@persisted_memo.keys.inspect}, overridden: #{@overrides.keys}" << '>'
56
61
  end
57
62
 
58
63
  end
@@ -9,5 +9,6 @@ module JSONAPIonify::Api
9
9
  DoubleRespondError = Class.new JSONAPIonifyError
10
10
  InvalidCursor = Class.new JSONAPIonifyError
11
11
  MissingContentType = Class.new JSONAPIonifyError
12
+ ReservedContextName = Class.new JSONAPIonifyError
12
13
  end
13
14
  end
@@ -30,8 +30,6 @@ module JSONAPIonify::Api
30
30
  example_input: :resource_identifier
31
31
  }
32
32
  define_action(:replace, 'PATCH', **options, &block).response status: 200 do |context|
33
- context.owner_context.reset(:instance)
34
- context.reset(:collection)
35
33
  context.response_object[:data] = build_identifier_collection(context.collection)
36
34
  context.response_object.to_json
37
35
  end
@@ -46,8 +44,6 @@ module JSONAPIonify::Api
46
44
  example_input: :resource_identifier
47
45
  }
48
46
  define_action(:add, 'POST', **options, &block).response status: 200 do |context|
49
- context.owner_context.reset(:instance)
50
- context.reset(:collection)
51
47
  context.response_object[:data] = build_identifier_collection(context.collection)
52
48
  context.response_object.to_json
53
49
  end
@@ -63,15 +59,13 @@ module JSONAPIonify::Api
63
59
  }
64
60
  options[:prepend] = 'relationships'
65
61
  define_action(:remove, 'DELETE', **options, &block).response status: 200 do |context|
66
- context.owner_context.reset(:instance)
67
- context.reset(:collection)
68
62
  context.response_object[:data] = build_identifier_collection(context.collection)
69
63
  context.response_object.to_json
70
64
  end
71
65
  end
72
66
 
73
67
  context :scope do |context|
74
- instance_exec rel.name, context.owner, context, &rel.resolve
68
+ instance_exec(rel.name, context.owner, context, &rel.resolve).dup
75
69
  end
76
70
 
77
71
  show
@@ -30,8 +30,6 @@ module JSONAPIonify::Api
30
30
  prepend: 'relationships'
31
31
  }
32
32
  define_action(:replace, 'PATCH', **options, &block).response status: 200 do |context|
33
- context.owner_context.reset(:instance)
34
- context.reset(:instance)
35
33
  context.response_object[:data] = build_resource_identifier(context.instance)
36
34
  context.response_object.to_json
37
35
  end
@@ -3,8 +3,6 @@ module JSONAPIonify::Api
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- before(:response) { |context| context.clear }
7
-
8
6
  context(:invalidate_cache?, readonly: true, persisted: true) { |c| c.includes.present? }
9
7
 
10
8
  # Response Objects
@@ -12,8 +12,7 @@ module JSONAPIonify::Api
12
12
  define_relationship(name, Relationship::Many, **opts, &block).tap do
13
13
  define_relationship_counter(
14
14
  name,
15
- count_attribute === true ? "#{name.to_s.singularize}_count" : count_attribute.to_s,
16
- include: include_count
15
+ count_attribute === true ? "#{name.to_s.singularize}_count" : count_attribute.to_s
17
16
  ) if count_attribute
18
17
  end
19
18
  end
@@ -24,11 +23,6 @@ module JSONAPIonify::Api
24
23
  end
25
24
 
26
25
  def define_relationship_counter(rel_name, name, include: true)
27
- before :response do |context|
28
- if (context.scope.is_a?(ActiveRecord::Relation) || context.scope.is_a?(ActiveRecord::Base)) && context.scope._reflect_on_association(rel_name)
29
- context.scope = context.scope.includes(rel_name)
30
- end if context.fields[type&.to_sym].include? name.to_sym
31
- end if include
32
26
  attribute name.to_sym, types.Integer, "The number of #{rel_name}.", write: false do |_, instance, context|
33
27
  rel = context.resource.class.relationship(rel_name)
34
28
  blank_fields = context.fields.map { |k, _| [k, {}] }.to_h
@@ -1,3 +1,3 @@
1
1
  module JSONAPIonify
2
- VERSION = "0.9.2"
2
+ VERSION = "0.9.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapionify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Waldrip
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-20 00:00:00.000000000 Z
11
+ date: 2016-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport