lhs 6.0.0 → 6.1.0

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
  SHA1:
3
- metadata.gz: 1375879a321c15e499ad2871e21ab93cda9b386b
4
- data.tar.gz: dfa739b88f8c0f26bd47ce4722a9d145fb479ecd
3
+ metadata.gz: 5d7653bf52ccc6c331969c2b7f153014c05e30f3
4
+ data.tar.gz: 99ac462ec3a706a538693846ae0f7af83a49ba38
5
5
  SHA512:
6
- metadata.gz: b457566f62fc9aae5f60287eae881b2a5cf2cf46c2de118a283b301d20b6d98126d93f816d46a70ed4714bb485dd0a2d971f022846864e91eb58764802779232
7
- data.tar.gz: da2266a749937a72f6fb2510c054024c1f2171493a7a05c4cdf7321b4e2af45aaf67229adc756d9e4f7da7aedd360d47638a6a2fc56e7dadac947959561018bc
6
+ metadata.gz: 49afc23aef8a94f6ebe4d3da147f2275ec7e615712d426b927c01d49fbcf81f8454714e1615b1bf7fbf038dd651c7aab28b89420a8479634217aeedd7dfdc730
7
+ data.tar.gz: 471d79108df5e5c4e6fed9f05fa2d1db3befbf6038e6d866187bc1e6e850627d37d20475ea141b597828c771c80e5f9a7e8cfb1fa23d6c1cb3f62700148efffb
@@ -22,6 +22,12 @@ class LHS::Record
22
22
  end
23
23
  end
24
24
 
25
+ def without_including
26
+ class_clone_factory(rand.to_s.gsub(/\D/, '')).tap do |class_clone|
27
+ class_clone.including = nil
28
+ end
29
+ end
30
+
25
31
  private
26
32
 
27
33
  def unfold_args(args)
@@ -75,7 +75,18 @@ class LHS::Record
75
75
  if target._raw.is_a? Array
76
76
  data[key] = addition.map(&:_raw)
77
77
  else # hash with items
78
+ extend_base_item_with_hash_of_items!(target, addition)
79
+ end
80
+ end
81
+
82
+ def extend_base_item_with_hash_of_items!(target, addition)
83
+ target._raw[items_key] ||= []
84
+ if target._raw[items_key].empty?
78
85
  target._raw[items_key] = addition.map(&:_raw)
86
+ else
87
+ target._raw[items_key].each_with_index do |item, index|
88
+ item.merge!(addition[index])
89
+ end
79
90
  end
80
91
  end
81
92
 
@@ -92,17 +103,39 @@ class LHS::Record
92
103
 
93
104
  def handle_include(included, data, sub_includes = nil, references = nil)
94
105
  return if data.blank? || skip_loading_includes?(data, included)
95
- options =
96
- if data.collection?
97
- options_for_multiple(data, included)
98
- elsif data[included].collection?
99
- options_for_nested_items(data, included)
100
- else
101
- url_option_for(data, included)
102
- end
106
+ options = options_for_data(data, included)
103
107
  options = extend_with_references(options, references)
104
108
  addition = load_include(options, data, sub_includes)
105
109
  extend_raw_data!(data, addition, included)
110
+ expand_addition!(data, included) if no_expanded_data?(addition)
111
+ end
112
+
113
+ def options_for_data(data, included = nil)
114
+ return options_for_multiple(data, included) if data.collection?
115
+ return options_for_nested_items(data, included) if included && data[included].collection?
116
+ url_option_for(data, included)
117
+ end
118
+
119
+ def expand_addition!(data, included)
120
+ addition = data[included]
121
+ options = options_for_data(addition)
122
+ record = record_for_options(options) || self
123
+ options = convert_options_to_endpoints(options) if record_for_options(options)
124
+ expanded_data = begin
125
+ record.without_including.request(options)
126
+ rescue LHC::NotFound
127
+ LHS::Data.new({}, data, record)
128
+ end
129
+ extend_raw_data!(data, expanded_data, included)
130
+ end
131
+
132
+ def no_expanded_data?(addition)
133
+ return false if addition.blank?
134
+ if addition.item?
135
+ (addition._raw.keys - [:href]).empty?
136
+ elsif addition.collection?
137
+ addition.all? { |item| item && (item._raw.keys - [:href]).empty? }
138
+ end
106
139
  end
107
140
 
108
141
  # Extends request options with options provided for this reference
@@ -169,13 +202,13 @@ class LHS::Record
169
202
  array
170
203
  end
171
204
 
172
- def options_for_multiple(data, key)
205
+ def options_for_multiple(data, key = nil)
173
206
  data.map do |item|
174
207
  url_option_for(item, key)
175
208
  end
176
209
  end
177
210
 
178
- def options_for_nested_items(data, key)
211
+ def options_for_nested_items(data, key = nil)
179
212
  data[key].map do |item|
180
213
  url_option_for(item)
181
214
  end
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "6.0.0"
2
+ VERSION = "6.1.0"
3
3
  end
@@ -337,6 +337,35 @@ describe LHS::Record do
337
337
  end
338
338
  end
339
339
 
340
+ context 'unexpanded response when requesting the included collection' do
341
+ it 'loads the collection and the single items, if not already expanded' do
342
+ class Customer < LHS::Record
343
+ endpoint ':datastore/customer/:id'
344
+ end
345
+
346
+ stub_request(:get, "#{datastore}/customer/1")
347
+ .to_return(body: {
348
+ places: {
349
+ href: "#{datastore}/places"
350
+ }
351
+ }.to_json)
352
+
353
+ stub_request(:get, "#{datastore}/places")
354
+ .to_return(body: {
355
+ items: [{ href: "#{datastore}/places/1" }]
356
+ }.to_json)
357
+
358
+ item_request = stub_request(:get, "#{datastore}/places/1")
359
+ .to_return(body: {
360
+ name: 'Casa Ferlin'
361
+ }.to_json)
362
+
363
+ place = Customer.includes(:places).find(1).places.first
364
+ assert_requested(item_request)
365
+ expect(place.name).to eq 'Casa Ferlin'
366
+ end
367
+ end
368
+
340
369
  context 'includes with options' do
341
370
  before(:each) do
342
371
  class Customer < LHS::Record
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0
4
+ version: 6.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors