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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9da3788aca18b7373349738518eb060890f2bc0b
4
- data.tar.gz: 13ce8616d35fa4e79621b924bc58d3444cbd0fcb
3
+ metadata.gz: f4fc3f553eb19fcaae033b0e4f282b90a02ebe76
4
+ data.tar.gz: d103df8833f9900ed1b563a83899b8986bd1ac22
5
5
  SHA512:
6
- metadata.gz: b82efadf83f9b16cb46617c81995e7e5b1df6e9706c08406aa0c33e0a2d45b9a10cb5fe2bfca3d62606409743da4ce329b1869ccf53f4cb0dd2e35f12673832c
7
- data.tar.gz: 2647c169ae848cc2a0bfdd319b35e1dd2db0c30a2929ee1107f0a7e9bb9dec3b480fb35a9f8c63a740bb584859bf14ccd5fd25fc320659b7dacf3dbaaf0c1d53
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
- value = object.nil? ? "" : object.send(name)
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, datum.fetch(:type, "value").to_s => value}
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({"template" => apply_data({}, :data => attrs)})
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:]]/
@@ -1,3 +1,3 @@
1
1
  module Conglomerate
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -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", "value" => []}
160
+ {"name" => "user_ids", "array" => []}
161
161
  ],
162
162
  "links" => [
163
163
  {"rel" => "event", "href" => "https://example.com/events/2"},
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conglomerate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons