railsful 0.1.2 → 0.1.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,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30e8f5c72f7b2752718a8d4724c28b5852f8dd8e5aaac4c206f8309a63cc34e8
4
- data.tar.gz: 0c74fe0fe436cdf061a8c86d54a007718dcffd7863681458bed90c4b2bb9d35a
3
+ metadata.gz: c4eaf1c781940b63c1fbce747b329bde6762e2cea0b9be799101cdc5ec0ce77d
4
+ data.tar.gz: 91f44d2f8794a764520106c956401c02f0c2fef52157e625be7665c9017fa601
5
5
  SHA512:
6
- metadata.gz: c0fb4853f6763cdabfe55a4799ba1647055f658e71c60b40fe7026b3d782b1140c3f289e443b93160089423a8131722cccf91dc2ebd82efd67ee3552764e2cec
7
- data.tar.gz: c39bed75213fdc2b6651474491c43777e6937fc36133e696cdde1b65296106561dc8b4d84bd7a436e40b8156141744fde9a7c379505b1e2553a1d8cee10e6aff
6
+ metadata.gz: 0e7799d17276f86313a788945cdfbf4bd28faaec669b613c3890b4139830064d6e486ea40e35bda96c96569d3012333b907e0ac8c67429e205b86181b2fb5ce8
7
+ data.tar.gz: ae72958a087df3b94f648045730fb5450a1369c9608236534ee30ac76825efc9f8b8f05f04dfb2d2067d9d479a4768dcac3cd60b34e7f333638c6ef9c8a96a3a
@@ -29,6 +29,8 @@ module Railsful
29
29
 
30
30
  # First level attributes from data object.
31
31
  #
32
+ # @return [Hash]
33
+ #
32
34
  # :reek:FeatureEnvy
33
35
  def attributes(params)
34
36
  data = params.fetch(:data, {})
@@ -48,32 +50,62 @@ module Railsful
48
50
  # Fetches all included associations/relationships from the
49
51
  # included hash.
50
52
  #
53
+ # @return [Hash]
54
+ #
51
55
  # :reek:UtilityFunction
56
+ # :reek:FeatureEnvy
52
57
  def included_hash(params)
53
- included_hash = {}
58
+ # Gather all necessary data we are working on.
59
+ included = params.fetch(:included, [])
60
+ relationships = params.fetch(:data, {}).fetch(:relationships, {})
54
61
 
55
- params.fetch(:included, []).each do |inc|
56
- type = inc[:type].to_sym
57
- attrs = inc[:attributes]
62
+ result = {}
58
63
 
59
- if params.dig(:data, :relationships, type, :data).is_a?(Array)
60
- # We pluralize the type since we are dealing with a
61
- # +has_many+ relationship.
62
- plural = ActiveSupport::Inflector.pluralize(type)
64
+ # Make sure that both +included+ and +relationships+ are given.
65
+ # Otherwise we can't do anything and return an empty hash.
66
+ return result if included.empty? || relationships.empty?
63
67
 
64
- included_hash["#{plural}_attributes"] ||= []
65
- included_hash["#{plural}_attributes"] << attrs
66
- else
67
- # When the data value is not an Array we are assuming that we
68
- # deal with a +has_one+ association. To be on the safe side we also
69
- # call singularize on the type.
70
- singular = ActiveSupport::Inflector.singularize(type)
68
+ # Iterate over all relationships.
69
+ relationships.each do |type, payload|
70
+ # Get the data value.
71
+ data = payload[:data]
71
72
 
72
- included_hash["#{singular}_attributes"] = attrs
73
+ # Check if we are dealing with a +has_many+ (Array) or +belongs_to+
74
+ # (Hash) relationship.
75
+ if data.is_a?(Array)
76
+ result["#{type}_attributes"] = []
77
+
78
+ data.each do |element|
79
+ result["#{type}_attributes"] << get_included(element, included)
80
+ end
81
+
82
+ # Remove all nil includes.
83
+ result["#{type}_attributes"].compact!
84
+ else
85
+ result["#{type}_attributes"] = get_included(data, included)
73
86
  end
74
87
  end
75
88
 
76
- included_hash
89
+ # Remove all nil includes.
90
+ result.compact
91
+ end
92
+
93
+ # Fetch the included object for a given relationship.
94
+ #
95
+ # @return [Hash, NilClass] The extracted included hash.
96
+ #
97
+ # :reek:UtilityFunction
98
+ def get_included(relation, included)
99
+ # Return the attributes of the last found element. But there SHOULD only
100
+ # be one element with the same tempid. If there is a mistake by the client
101
+ # we always take the last.
102
+ found = included.reverse
103
+ .detect { |inc| inc[:tempid] == relation[:tempid] }
104
+
105
+ return nil unless found
106
+
107
+ # Return the attributes of the found include hash or an empty hash.
108
+ found.fetch(:attributes, {})
77
109
  end
78
110
 
79
111
  def relationship(type, payload)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Railsful
4
- VERSION = '0.1.2'.freeze
4
+ VERSION = '0.1.3'.freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railsful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henning Vogt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2019-01-16 00:00:00.000000000 Z
12
+ date: 2019-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: deep_merge