json_api_ruby 0.0.3 → 0.1.0

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a42efcc7659bcd6df482b2f0a82f9434e2e65d7e
4
- data.tar.gz: 3b111dbabfd9f66e0893af2c0b21f53a480d5711
3
+ metadata.gz: 91293b5565faf0ff2b9cbd1260c8e2da3a7b774b
4
+ data.tar.gz: e21ee8f390cd1fc36f0abea84201c240685b7f3a
5
5
  SHA512:
6
- metadata.gz: 78d370d9fb0cbecbb2e46265610d7b8eee525d953bd0c786954d603cebe2800934a8995d944ff4fa9554888d697d43cc360dca217ab1ba5fe891a781f0c9be4f
7
- data.tar.gz: ccdf63438c842cd0c6964633b755b45465246be9fb963f670e94af24dcb59334f3f2784cbf5d9a3ff2f7d07294748dc72bf33ebecaac18c67be3fd29ca455190
6
+ metadata.gz: 84c0dcd1ff3fec00ff7708a2db101448dad9b10abdba31dc0fbdecfc112a5ca2a9484ecaacb4636c384ec39b4acdad956330a1027d255cc11bb389d3c80c9e71
7
+ data.tar.gz: 05fe188da17d76795ce11e90074b21aa0e0046d9d359b82b1cfadf12f5907ee5669680aac1056f261a1c4ccae15ca72d18da1b5d9052c436cb4db16b286433f4
@@ -3,8 +3,12 @@ module JsonApi
3
3
  class Discovery
4
4
  def self.resource_for_name(model, options={})
5
5
  @discovered_classes ||= {}
6
- namespace = options.fetch(:namespace, nil)
7
6
  klass = options.fetch(:resource_class, nil)
7
+
8
+ # Duck typing. If klass responds to :new, we assume it's a class
9
+ return klass if klass.respond_to?(:new)
10
+
11
+ namespace = options.fetch(:namespace, nil)
8
12
  parent = options.fetch(:parent_resource, nil)
9
13
 
10
14
  if klass.blank?
@@ -14,16 +14,19 @@ module JsonApi
14
14
 
15
15
  attr_reader :cardinality
16
16
 
17
+ attr_reader :explicit_resource_class
18
+
17
19
  def initialize(name, options)
18
20
  @name = name.to_s
19
21
  @cardinality = options.fetch(:cardinality)
22
+ @explicit_resource_class = options.fetch(:resource_class, nil)
20
23
  end
21
24
 
22
25
  def build_resources(options)
23
26
  if cardinality == :one
24
- relationship = ToOneRelationship.new(name, options)
27
+ relationship = ToOneRelationship.new(name, options.merge(explicit_resource_class: explicit_resource_class))
25
28
  else
26
- relationship = ToManyRelationship.new(name, options)
29
+ relationship = ToManyRelationship.new(name, options.merge(explicit_resource_class: explicit_resource_class))
27
30
  end
28
31
  relationship.build_resources(options)
29
32
  relationship
@@ -46,6 +49,8 @@ module JsonApi
46
49
  # included
47
50
  attr_reader :included
48
51
  attr_reader :name
52
+ attr_reader :explicit_resource_class
53
+
49
54
 
50
55
  # The resource object that represents this relationship
51
56
  attr_reader :resources
@@ -56,6 +61,7 @@ module JsonApi
56
61
  @parent = options.fetch(:parent_resource)
57
62
  @parent_model = parent._model
58
63
  @included = options.fetch(:included, false)
64
+ @explicit_resource_class = options.fetch(:explicit_resource_class)
59
65
  end
60
66
 
61
67
  def included?
@@ -90,7 +96,7 @@ module JsonApi
90
96
  resource_model = parent_model.send(name)
91
97
  return if resource_model.blank?
92
98
 
93
- resource_class = Discovery.resource_for_name(resource_model, options.merge(parent_resource: parent))
99
+ resource_class = Discovery.resource_for_name(resource_model, options.merge(parent_resource: parent, resource_class: explicit_resource_class))
94
100
  @resources << resource_class.new(resource_model)
95
101
  end
96
102
 
@@ -112,7 +118,7 @@ module JsonApi
112
118
  return unless included?
113
119
 
114
120
  parent_model.send(name).each do |resource_model|
115
- resource_class = Discovery.resource_for_name(resource_model, options.merge(parent_resource: parent))
121
+ resource_class = Discovery.resource_for_name(resource_model, options.merge(parent_resource: parent, resource_class: explicit_resource_class))
116
122
  @resources << resource_class.new(resource_model)
117
123
  end
118
124
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApi
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -10,10 +10,14 @@ RSpec.describe JsonApi::Resources::Discovery do
10
10
  expect(JsonApi::Resources::Discovery.resource_for_name(Three.new, namespace: 'different_namespace')).to eq DifferentNamespace::ThreeResource
11
11
  end
12
12
 
13
- it 'allows explicitly providing a resource class' do
13
+ it 'allows explicitly providing a resource class as a string' do
14
14
  expect(JsonApi::Resources::Discovery.resource_for_name(Two.new, resource_class: 'DifferentNamespace::ThreeResource')).to eq DifferentNamespace::ThreeResource
15
15
  end
16
16
 
17
+ it 'allows explicitly providing a resource class as an constant' do
18
+ expect(JsonApi::Resources::Discovery.resource_for_name(Two.new, resource_class: DifferentNamespace::ThreeResource)).to eq DifferentNamespace::ThreeResource
19
+ end
20
+
17
21
  it "raises an error if the resource can't be found" do
18
22
  expect {
19
23
  JsonApi::Resources::Discovery.resource_for_name(nil)
@@ -107,4 +107,32 @@ RSpec.describe JsonApi::Resources::RelationshipMeta do
107
107
  end
108
108
  end
109
109
  end
110
+
111
+ describe 'passing in a resource class' do
112
+ let(:simple_article_resource) do
113
+ SimpleArticleResource.new(article, include: ['author', 'comments'])
114
+ end
115
+
116
+ let(:author_relation) do
117
+ simple_article_resource.relationships.find do |rel|
118
+ rel.name == 'author'
119
+ end
120
+ end
121
+
122
+ let(:comment_relation) do
123
+ simple_article_resource.relationships.find do |rel|
124
+ rel.name == 'comments'
125
+ end
126
+ end
127
+
128
+ it 'uses the passed in resource_class for author' do
129
+ expect(author_relation.resource_object).to be_an_instance_of(SimplePersonResource)
130
+ end
131
+
132
+ it 'uses the passed in resource_class for each comment' do
133
+ comment_relation.resource_objects.each do |resource_object|
134
+ expect(resource_object).to be_an_instance_of(SimpleCommentResource)
135
+ end
136
+ end
137
+ end
110
138
  end
@@ -84,6 +84,22 @@ class ArticleResource < JsonApi::Resource
84
84
  has_many :comments
85
85
  end
86
86
 
87
+ class SimplePersonResource < JsonApi::Resource
88
+ attribute :name
89
+ end
90
+
91
+ class SimpleCommentResource < JsonApi::Resource
92
+ id_field :uuid
93
+ attribute :comment_text
94
+ end
95
+
96
+ class SimpleArticleResource < JsonApi::Resource
97
+ attribute :title
98
+
99
+ has_one :author, resource_class: SimplePersonResource
100
+ has_many :comments, resource_class: 'SimpleCommentResource'
101
+ end
102
+
87
103
  class CommentResource < JsonApi::Resource
88
104
  id_field :uuid
89
105
  attribute :author
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tracey Eubanks
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-14 00:00:00.000000000 Z
11
+ date: 2016-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport