jsonapi-resources-matchers 0.2.0 → 0.3.0

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: ce61d44c20a946880e5ec4f83cac4b048d9dcf01
4
- data.tar.gz: 81e5d38ac48355c2e61a07e46fd477cd614acb5d
3
+ metadata.gz: 9cf29e748e8ceb2c88f705e41606add289595519
4
+ data.tar.gz: 8786354af24bb3c3e6b013f11f16014a39930b66
5
5
  SHA512:
6
- metadata.gz: fdb63eae9a2ff22abbc0289030dfb3547a82fd4fbe5ec1349a99c0f638bbe43742009535010979ecdbd10da2b960dd1947cf87eacc47de7d4de0107e58db00ee
7
- data.tar.gz: 665792e728af159ebb6746dfae7f6ae71009239056e5cd454654545bda238aad64403b5ef96d64f5d727b8a8059b99ba8fe2e86d889f082d96c66455df52de20
6
+ metadata.gz: e941507f83a4c9537bd3c7a69716efa3b1faafd242c926c61ba93a0236109c022a4334701fd4e7e01995838ae1f127d256c57a250acc41a6f19dda9df0e91e43
7
+ data.tar.gz: 1c5d9824e4024b1dd101aab935a19c4a918db045a76e40582e0c5e7fb4a6a33654a4ce2cf69636336aefc7a906605ca4b332177f6f125acf854455423683c162
@@ -1,3 +1,8 @@
1
+ # v0.3.0
2
+
3
+ - Add `with_relation_name` to `have_many` matcher
4
+ - Condense `HaveOne` and `HaveMany` into `Relationship`. No public API change.
5
+
1
6
  # v0.2.0
2
7
 
3
8
  - Add `have_one` matcher
@@ -1,8 +1,7 @@
1
1
  require "jsonapi/resources/matchers/version"
2
2
  require "jsonapi/resources/matchers/have_attribute"
3
3
  require "jsonapi/resources/matchers/filter"
4
- require "jsonapi/resources/matchers/have_many"
5
- require "jsonapi/resources/matchers/have_one"
4
+ require "jsonapi/resources/matchers/relationship"
6
5
  require "jsonapi/resources/matchers/have_model_name"
7
6
  require "jsonapi/resources/matchers/have_primary_key"
8
7
 
@@ -19,11 +18,11 @@ module JSONAPI
19
18
  end
20
19
 
21
20
  def have_many(name)
22
- HaveMany.new(name)
21
+ Relationship.new(:have_many, name)
23
22
  end
24
23
 
25
24
  def have_one(name)
26
- HaveOne.new(name)
25
+ Relationship.new(:have_one, name)
27
26
  end
28
27
 
29
28
  def have_model_name(name)
@@ -1,22 +1,33 @@
1
1
  module JSONAPI
2
2
  module Resources
3
3
  module Matchers
4
- class HaveMany
4
+ class Relationship
5
5
 
6
- attr_accessor :name, :resource, :expected_class_name
6
+ attr_accessor(:name,
7
+ :relationship_type,
8
+ :resource,
9
+ :expected_class_name,
10
+ :expected_relation_name)
7
11
 
8
- def initialize(name)
12
+ def initialize(relationship_type, name)
13
+ self.relationship_type = relationship_type
9
14
  self.name = name
10
15
  end
11
16
 
12
17
  def description
13
- "have many `#{name}`"
18
+ "#{humanized_relationship_type} `#{name}`"
19
+ end
20
+
21
+ def humanized_relationship_type
22
+ relationship_type.to_s.gsub('_',' ')
14
23
  end
15
24
 
16
25
  def matches?(resource)
17
26
  self.resource = resource
18
27
 
19
- has_key_in_relationships? && matches_class_name?
28
+ has_key_in_relationships? &&
29
+ matches_class_name? &&
30
+ matches_relation_name?
20
31
  end
21
32
 
22
33
  def has_key_in_relationships?
@@ -33,12 +44,20 @@ module JSONAPI
33
44
  self
34
45
  end
35
46
 
47
+ def with_relation_name(name)
48
+ self.expected_relation_name = name
49
+ self
50
+ end
51
+
36
52
  def failure_message
37
53
  resource_name = resource.class.name.demodulize
38
- message = ["expected `#{resource_name}` to have many `#{name}`"]
54
+ message = ["expected `#{resource_name}` to #{humanized_relationship_type} `#{name}`"]
39
55
  if self.expected_class_name
40
56
  message << "with class name `#{self.expected_class_name}`"
41
57
  end
58
+ if self.expected_relation_name
59
+ message << "with relation name `#{self.expected_relation_name}`"
60
+ end
42
61
  message.join(" ")
43
62
  end
44
63
 
@@ -49,6 +68,13 @@ module JSONAPI
49
68
  self.expected_class_name == actual_class_name
50
69
  end
51
70
 
71
+ def matches_relation_name?
72
+ return true if self.expected_relation_name.nil?
73
+ association = resource.class._relationships[name]
74
+ actual_relation_name = association.relation_name
75
+ self.expected_relation_name == actual_relation_name
76
+ end
77
+
52
78
  end
53
79
  end
54
80
  end
@@ -1,7 +1,7 @@
1
1
  module JSONAPI
2
2
  module Resources
3
3
  module Matchers
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-resources-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - G5
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-07-21 00:00:00.000000000 Z
12
+ date: 2015-07-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jsonapi-resources
@@ -106,11 +106,10 @@ files:
106
106
  - lib/jsonapi/resources/matchers.rb
107
107
  - lib/jsonapi/resources/matchers/filter.rb
108
108
  - lib/jsonapi/resources/matchers/have_attribute.rb
109
- - lib/jsonapi/resources/matchers/have_many.rb
110
109
  - lib/jsonapi/resources/matchers/have_model_name.rb
111
- - lib/jsonapi/resources/matchers/have_one.rb
112
110
  - lib/jsonapi/resources/matchers/have_primary_key.rb
113
111
  - lib/jsonapi/resources/matchers/integrations/rspec.rb
112
+ - lib/jsonapi/resources/matchers/relationship.rb
114
113
  - lib/jsonapi/resources/matchers/version.rb
115
114
  homepage: https://github.com/G5/jsonapi-resources-matchers
116
115
  licenses:
@@ -1,55 +0,0 @@
1
- module JSONAPI
2
- module Resources
3
- module Matchers
4
- class HaveOne
5
-
6
- attr_accessor :name, :resource, :expected_class_name
7
-
8
- def initialize(name)
9
- self.name = name
10
- end
11
-
12
- def description
13
- "have one `#{name}`"
14
- end
15
-
16
- def matches?(resource)
17
- self.resource = resource
18
-
19
- has_key_in_relationships? && matches_class_name?
20
- end
21
-
22
- def has_key_in_relationships?
23
- serialized_hash = JSONAPI::ResourceSerializer.new(resource.class).
24
- serialize_to_hash(resource).with_indifferent_access
25
- expected_key = name.to_s.dasherize
26
- relationships = serialized_hash["data"]["relationships"]
27
- return false if relationships.nil?
28
- relationships.has_key?(expected_key)
29
- end
30
-
31
- def with_class_name(name)
32
- self.expected_class_name = name
33
- self
34
- end
35
-
36
- def failure_message
37
- resource_name = resource.class.name.demodulize
38
- message = ["expected `#{resource_name}` to have one `#{name}`"]
39
- if self.expected_class_name
40
- message << "with class name `#{self.expected_class_name}`"
41
- end
42
- message.join(" ")
43
- end
44
-
45
- def matches_class_name?
46
- return true if self.expected_class_name.nil?
47
- association = resource.class._relationships[name]
48
- actual_class_name = association.class_name
49
- self.expected_class_name == actual_class_name
50
- end
51
-
52
- end
53
- end
54
- end
55
- end