jsonapionify 0.12.8 → 0.12.9

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
- ODg1ZmQxYmM1MzlhOGYwZWU5MmRkZDY5MjM5MzYxZjMyNzk1N2UzOQ==
4
+ ZDY5OTI1YzBhMWMzMjE1NmNiYjM3NTM5NWI2Y2FhNWJmMDI3ZDYyMQ==
5
5
  data.tar.gz: !binary |-
6
- NzRiODY0OWUyZDJkNTAxMGYzYWJhNzI5NjZlNTc3M2JjYTRkYzEwZA==
6
+ MTRiNTI3NTI4ODhjZTk1NzNiNzExYzBmOTU5NjE2ZTliZjRmN2Y4Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MjI3NzFhZTcxZDM5MDVjZTZlZDhjY2VmZDgyZTVhNTgwYTU3OWRlMWFiOTVk
10
- MDI2ZjgwYTQ4ODI5ODM4MDJlM2UxZWFhMzNiZGIxNDBhOTgxZjNmMGY1Mjdm
11
- ZjM0ODMzN2M2NTI0YjhmN2RkNzJjZjJkYmQ3NTI0ODU2NTQxZDk=
9
+ ZGM4NTlkMDAxYWE5YjAwZDIwNjA4YjQ3NTE2MGU2MTM3MTI0ZTJlZTUzOWFk
10
+ OTFmODZjN2EzODhiYmU0ZjdjNWQ3YmU0YjdiNTIxZDI5NDRmMzRmMTAxYzFk
11
+ MjUxYjUyNjA3NTNlZjk1ZDIxNjgzNzJhM2RiNDM0MWU2ODkxNGY=
12
12
  data.tar.gz: !binary |-
13
- MTMyNmE0N2RkM2RlMzMzZDA4ZGY5NjUwNmQxYmMzOTE1OGMzMGZhMTNlZDMz
14
- ZTU4NGNhY2M1YjA0ZjYzNDI3NzQ2YTY3YmJkNDk4Y2VjZWFiZTkxNTgxMGEz
15
- MjJhZmY2MDhmN2M2YzdhNjc2YjE0OTY0MGUxYjM5NmQ4MzMwNjk=
13
+ MzI4NDUyZTQyZTgyOTdiNWJiZjZkNzE0Y2JiYWRhZTYzMzY0MDVjNjM2ZWMy
14
+ MTAwOWFkNmJjZmJkNmUwZmMzZmE0MjQ3NTI1MjkyNTQ2M2U3MjJlMzBmMDQ1
15
+ MmNkNjY2OGVhZDBkMzY3NmU0NWY3NDU5ZjM5ZDQ1ZmFkNzRlNDc=
@@ -16,26 +16,31 @@ module JSONAPIonify::Api
16
16
  request: request,
17
17
  context_definitions: sample_context(resource)
18
18
  ).exec { |c| c }
19
+ $pry = true if resource.type == 'organizations'
19
20
  case @example_input
20
21
  when :resource
21
22
  {
22
23
  'data' => resource.build_resource(
23
24
  context: context,
24
- instance: resource.example_instance_for_action(name, context),
25
- links: false
25
+ instance: resource.example_instance_for_action(name, context, true),
26
+ links: false,
27
+ write: true
26
28
  ).as_json
27
29
  }.to_json
28
30
  when :resource_identifier
29
31
  {
30
- 'data' => resource.build_resource_identifier(
31
- instance: resource.example_instance_for_action(name, context)
32
- ).as_json
32
+ 'data' => build_sample_resource_indentifier(name, resource, context).as_json
33
33
  }.to_json
34
34
  when Proc
35
35
  @example_input.call
36
36
  end
37
37
  end
38
38
 
39
+ def build_sample_resource_indentifier(name, resource, context)
40
+ rid = resource.build_resource_identifier(instance: resource.example_instance_for_action(name, context, true))
41
+ resource.respond_to?(:rel) && resource.rel.is_a?(JSONAPIonify::Api::Relationship::Many) ? [rid] : rid
42
+ end
43
+
39
44
  def example_requests(resource, url)
40
45
  responses.map do |response|
41
46
  opts = {}
@@ -57,6 +62,7 @@ module JSONAPIonify::Api
57
62
  end
58
63
 
59
64
  def sample_context(resource)
65
+ action = self
60
66
  resource.context_definitions.dup.tap do |defs|
61
67
  collection_context = proc do |context|
62
68
  3.times.map { resource.example_instance_for_action(action.name, context) }
@@ -66,6 +72,7 @@ module JSONAPIonify::Api
66
72
  defs[:paginated_collection] = Context.new(:paginated_collection) { |collection:| collection }
67
73
  defs[:instance] = Context.new(:instance, readonly: true) { |collection:| collection.first }
68
74
  defs[:owner_context] = Context.new(:owner_context, readonly: true) { ContextDelegate::Mock.new } if defs.has_key? :owner_context
75
+ defs[:action] = Context.new(:action, readonly: true) { action }
69
76
  end
70
77
  end
71
78
 
@@ -26,7 +26,7 @@ module JSONAPIonify::Api
26
26
  end
27
27
 
28
28
  def documentation_output(request)
29
- cache_store.fetch(resource_signature) do
29
+ cache_store.fetch(cache_key documentation: true) do
30
30
  JSONAPIonify::Documentation.new(documentation_object(request)).result
31
31
  end
32
32
  end
@@ -83,6 +83,10 @@ module JSONAPIonify::Api
83
83
  end
84
84
  end
85
85
 
86
+ context :instance do
87
+ nil
88
+ end
89
+
86
90
  context :scope do |context, owner:|
87
91
  instance_exec(rel.name, owner, context, &rel.resolve.destructure)
88
92
  end
@@ -0,0 +1,37 @@
1
+ module JSONAPIonify::Api
2
+ module Resource::Builders
3
+ class AttributeBuilder < BaseBuilder
4
+
5
+ UnsupportedError = Class.new(StandardError)
6
+
7
+ attr_reader :attribute, :write, :context, :instance, :example_id
8
+ delegate :action_name, to: :context
9
+
10
+ def initialize(resource, example_id:, instance:, attribute:, context:, write: false)
11
+ super(resource)
12
+ @instance = instance
13
+ @context = context
14
+ @attribute = attribute
15
+ @write = write
16
+ @example_id = example_id
17
+ end
18
+
19
+ def build
20
+ write ? build_writable : build_readable
21
+ end
22
+
23
+ private
24
+
25
+ def build_writable
26
+ raise UnsupportedError unless attribute&.supports_write_for_action?(action_name, context)
27
+ attribute.resolve(instance, context, example_id: example_id)
28
+ end
29
+
30
+ def build_readable
31
+ raise UnsupportedError unless attribute&.supports_read_for_action?(action_name, context)
32
+ attribute.resolve(instance, context, example_id: example_id)
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -2,14 +2,21 @@ module JSONAPIonify::Api
2
2
  module Resource::Builders
3
3
  class AttributesBuilder < FieldsBuilder
4
4
 
5
+ attr_reader :write
6
+
7
+ def initialize(*args, write: false, **opts)
8
+ super(*args, **opts)
9
+ @write = write
10
+ end
11
+
5
12
  delegate :attributes, to: :resource, prefix: true
6
13
 
7
14
  private
8
15
 
9
16
  def build_default
10
17
  resource_attributes.each_with_object(Objects::Attributes.new) do |attribute, attrs|
11
- if attribute.supports_read_for_action?(action_name, context) && !attribute.hidden_for_action?(action_name)
12
- attrs[attribute.name] = attribute.resolve(instance, context, example_id: example_id)
18
+ if !attribute.hidden_for_action?(action_name)
19
+ build_attribute(attribute, attrs)
13
20
  end
14
21
  end
15
22
  end
@@ -18,12 +25,22 @@ module JSONAPIonify::Api
18
25
  resource_fields.each_with_object(Objects::Attributes.new) do |field, attrs|
19
26
  field = field.to_sym
20
27
  attribute = resource_attributes.find { |attr| attr.name == field }
21
- if attribute&.supports_read_for_action?(action_name, context)
22
- attrs[field] = attribute.resolve(instance, context, example_id: example_id)
23
- end
28
+ build_attribute(attribute, attrs) if attribute
24
29
  end
25
30
  end
26
31
 
32
+ def build_attribute(attribute, attrs)
33
+ attrs[attribute.name] = AttributeBuilder.build(
34
+ resource,
35
+ instance: instance,
36
+ write: write,
37
+ attribute: attribute,
38
+ context: context,
39
+ example_id: example_id
40
+ )
41
+ rescue AttributeBuilder::UnsupportedError
42
+ end
43
+
27
44
  end
28
45
  end
29
46
  end
@@ -8,8 +8,8 @@ module JSONAPIonify::Api
8
8
 
9
9
  def initialize(resource, instance:, context:, example_id:)
10
10
  super(resource)
11
- @instance = instance
12
- @context = context
11
+ @instance = instance
12
+ @context = context
13
13
  @example_id = example_id
14
14
  end
15
15
 
@@ -4,13 +4,14 @@ module JSONAPIonify::Api
4
4
  delegate :attributes, :relationships, :type, :relationship, to: :resource, prefix: true
5
5
  delegate :params, :includes, :fields, :action_name, :request, to: :context
6
6
 
7
- attr_reader :context, :links, :include_cursor, :block
7
+ attr_reader :context, :links, :include_cursor, :block, :write
8
8
 
9
- def initialize(resource, context:, links: true, include_cursor: false, **opts, &block)
9
+ def initialize(resource, context:, links: true, include_cursor: false, write: false, **opts, &block)
10
10
  super(resource, **opts)
11
11
  @context = context
12
12
  @links = links
13
13
  @include_cursor = include_cursor
14
+ @write = write
14
15
  @block = block
15
16
  end
16
17
 
@@ -33,7 +34,8 @@ module JSONAPIonify::Api
33
34
  resource,
34
35
  instance: instance,
35
36
  context: context,
36
- example_id: example_id
37
+ example_id: example_id,
38
+ write: write
37
39
  )
38
40
  end
39
41
 
@@ -11,7 +11,9 @@ module JSONAPIonify::Api
11
11
  path_actions.map(&:request_method)
12
12
  end
13
13
 
14
- context(:action_name, persisted: true)
14
+ context(:action_name, persisted: true) do |action: nil|
15
+ action&.name
16
+ end
15
17
 
16
18
  define_action(:options, 'OPTIONS', '*', cacheable: true, callbacks: false) do
17
19
  cache 'options-request'
@@ -21,8 +21,7 @@ module JSONAPIonify::Api
21
21
  end
22
22
 
23
23
  new_instance do |scope|
24
- raise NotImplementedError, 'new_instance not implemented' unless scope.respond_to?(:new)
25
- scope.new
24
+ scope.new if scope.respond_to?(:new)
26
25
  end
27
26
 
28
27
  # Invoke validating contexts
@@ -17,5 +17,9 @@ module JSONAPIonify::Api
17
17
  )
18
18
  end
19
19
 
20
+ def remove_context name
21
+ self.context_definitions.delete(name)
22
+ end
23
+
20
24
  end
21
25
  end
@@ -17,9 +17,15 @@ module JSONAPIonify::Api
17
17
  define_singleton_method(:find_instance) do |id|
18
18
  instance_exec(current_scope, id, OpenStruct.new, &block.destructure(object: OpenStruct.new))
19
19
  end
20
- context :instance, persisted: true do |context, scope:, id:|
21
- $pry = true
22
- instance_exec(scope, id, context, &block.destructure(object: context))
20
+ context :instance, persisted: true do |context, scope:, id:, action: nil|
21
+ case action&.name
22
+ when :create
23
+ new_instance
24
+ when :list
25
+ nil
26
+ else
27
+ instance_exec(scope, id, context, &block.destructure(object: context))
28
+ end
23
29
  end
24
30
  end
25
31
 
@@ -36,12 +36,16 @@ module JSONAPIonify::Api
36
36
  end
37
37
  end
38
38
 
39
- def example_instance_for_action(action, context)
39
+ def example_instance_for_action(action, context, write = false)
40
40
  id = generate_id
41
41
  OpenStruct.new.tap do |instance|
42
42
  instance.send "#{id_attribute}=", id.to_s
43
43
  actionable_attributes = attributes.select do |attr|
44
- attr.supports_read_for_action?(action, context)
44
+ if write
45
+ attr.supports_write_for_action?(action, context)
46
+ else
47
+ attr.supports_read_for_action?(action, context)
48
+ end
45
49
  end
46
50
  actionable_attributes.each do |attribute|
47
51
  instance.send "#{attribute.name}=", attribute.example(id)
@@ -1,3 +1,3 @@
1
1
  module JSONAPIonify
2
- VERSION = "0.12.8"
2
+ VERSION = "0.12.9"
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.12.8
4
+ version: 0.12.9
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-07-27 00:00:00.000000000 Z
11
+ date: 2016-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -556,6 +556,7 @@ files:
556
556
  - lib/jsonapionify/api/relationship/one.rb
557
557
  - lib/jsonapionify/api/resource.rb
558
558
  - lib/jsonapionify/api/resource/builders.rb
559
+ - lib/jsonapionify/api/resource/builders/attribute_builder.rb
559
560
  - lib/jsonapionify/api/resource/builders/attributes_builder.rb
560
561
  - lib/jsonapionify/api/resource/builders/base_builder.rb
561
562
  - lib/jsonapionify/api/resource/builders/cursor_builder.rb