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 +4 -4
- data/lib/railsful/deserializer.rb +49 -17
- data/lib/railsful/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4eaf1c781940b63c1fbce747b329bde6762e2cea0b9be799101cdc5ec0ce77d
|
4
|
+
data.tar.gz: 91f44d2f8794a764520106c956401c02f0c2fef52157e625be7665c9017fa601
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
58
|
+
# Gather all necessary data we are working on.
|
59
|
+
included = params.fetch(:included, [])
|
60
|
+
relationships = params.fetch(:data, {}).fetch(:relationships, {})
|
54
61
|
|
55
|
-
|
56
|
-
type = inc[:type].to_sym
|
57
|
-
attrs = inc[:attributes]
|
62
|
+
result = {}
|
58
63
|
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
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
|
-
|
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)
|
data/lib/railsful/version.rb
CHANGED
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.
|
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-
|
12
|
+
date: 2019-01-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: deep_merge
|