conglomerate 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/conglomerate/serializer.rb +24 -5
- data/lib/conglomerate/version.rb +1 -1
- data/spec/conglomerate_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4fc3f553eb19fcaae033b0e4f282b90a02ebe76
|
4
|
+
data.tar.gz: d103df8833f9900ed1b563a83899b8986bd1ac22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b70a06cf3cf2d5c9ddcb0f7be710070999933c577a50e4d15b0b235bdfe1025d1cc55c0d6e8260af40e51d6667e94438386bf5bd91702ff13aebe04e3f4cbcf4
|
7
|
+
data.tar.gz: 6200528508229a70be96a79f75d93120ce349b3a209bf5129a2f238cc6a8e4b1ba7dfa6f1f744328ef485da73eddc23a832a7e9bac8f4277bebe830b049fbba2
|
@@ -41,12 +41,15 @@ module Conglomerate
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
def apply_data(collection, data: [], object: nil)
|
44
|
+
def apply_data(collection, data: [], object: nil, default_value: nil)
|
45
45
|
data = data.map do |datum|
|
46
46
|
name = datum[:name]
|
47
|
-
|
47
|
+
type = datum.fetch(:type, :value)
|
48
|
+
value = sanitize_value(
|
49
|
+
object, :name => name, :type => type, :default_value => default_value
|
50
|
+
)
|
48
51
|
|
49
|
-
{"name" => name.to_s,
|
52
|
+
{"name" => name.to_s, type.to_s => value}
|
50
53
|
end
|
51
54
|
|
52
55
|
if data.empty?
|
@@ -105,7 +108,9 @@ module Conglomerate
|
|
105
108
|
if attrs.empty?
|
106
109
|
collection
|
107
110
|
else
|
108
|
-
collection.merge(
|
111
|
+
collection.merge(
|
112
|
+
{"template" => apply_data({}, :data => attrs, :default_value => "")}
|
113
|
+
)
|
109
114
|
end
|
110
115
|
end
|
111
116
|
|
@@ -145,7 +150,7 @@ module Conglomerate
|
|
145
150
|
def build_query(rel, data, block)
|
146
151
|
query = {"rel" => rel.to_s}
|
147
152
|
query = apply_href(query, :proc => block)
|
148
|
-
apply_data(query, :data => data)
|
153
|
+
apply_data(query, :data => data, :default_value => "")
|
149
154
|
end
|
150
155
|
|
151
156
|
def build_item_link(rel, proc: nil, object: nil)
|
@@ -153,6 +158,20 @@ module Conglomerate
|
|
153
158
|
apply_href(link, :proc => proc, :object => object)
|
154
159
|
end
|
155
160
|
|
161
|
+
def sanitize_value(object, name:, type: :value, default_value: nil)
|
162
|
+
if blank?(object) || blank?(object.send(name))
|
163
|
+
if type == :array
|
164
|
+
[]
|
165
|
+
elsif type == :object
|
166
|
+
{}
|
167
|
+
else
|
168
|
+
default_value
|
169
|
+
end
|
170
|
+
else
|
171
|
+
object.send(name)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
156
175
|
def blank?(value)
|
157
176
|
if value.is_a?(String)
|
158
177
|
value !~ /[^[:space:]]/
|
data/lib/conglomerate/version.rb
CHANGED
data/spec/conglomerate_spec.rb
CHANGED
@@ -18,7 +18,7 @@ class ConglomerateTestSerializer
|
|
18
18
|
attribute :team_ids, :rel => :teams, :type => :array do |item|
|
19
19
|
team_url(item.team_ids.join(","))
|
20
20
|
end
|
21
|
-
attribute :user_ids, :rel => :users do |item|
|
21
|
+
attribute :user_ids, :rel => :users, :type => :array do |item|
|
22
22
|
user_url(item.user_ids.join(","))
|
23
23
|
end
|
24
24
|
|
@@ -157,7 +157,7 @@ describe Conglomerate do
|
|
157
157
|
{"name" => "event_id", "value" => 2},
|
158
158
|
{"name" => "roster_id", "value" => nil},
|
159
159
|
{"name" => "team_ids", "array" => [1,2]},
|
160
|
-
{"name" => "user_ids", "
|
160
|
+
{"name" => "user_ids", "array" => []}
|
161
161
|
],
|
162
162
|
"links" => [
|
163
163
|
{"rel" => "event", "href" => "https://example.com/events/2"},
|