lhs 5.6.4 → 5.6.5

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: 1d5463ddb8aecb066995d59a8ef055bafd5b98ea
4
- data.tar.gz: 13ff6360d6817f2615adde2c9a833abd3681e6d2
3
+ metadata.gz: c65a63aa10738241bdb3ba6c085025cee24281c2
4
+ data.tar.gz: 5e2dbd3c4d0c7860059728ea694a399b6d53eec4
5
5
  SHA512:
6
- metadata.gz: 153293dfe1a6c8619a50e43d2f64d4616bd5fae5045aa95052b719d10bac03735aa0fe81f184792c74ef29ab31d83423b6b6b6662fadc8e8ea0c880cff57493f
7
- data.tar.gz: 947150959ead6112b621ea868e8f4e3481195397e5cbc4277caacbefabaae31beb9558bb0f8bb6ef458eb7b2df1f1428e00740ee00d04d8b265ed456033953bb
6
+ metadata.gz: 37868d94b6f13f08fa2ef796b210e682e4f9162275b1a6770fa37b73984fd1718092dc7c799c81190d8cd74edb4885284ac043ebe33fdf2807cbaa4c91c59b7f
7
+ data.tar.gz: fb80f7203c93908654728ba73df28ba159509904d3c665546f6874f73079b6fc6be6d53c909d390b9ae062c87fe52ca9bb8c76401139cd58bfe5963365be2572
@@ -38,23 +38,48 @@ class LHS::Record
38
38
  end
39
39
 
40
40
  # Extends existing raw data with additionaly fetched data
41
- def extend_raw_data(data, addition, key)
41
+ def extend_raw_data!(data, addition, key)
42
42
  return if addition.empty?
43
43
  if data.collection?
44
- data.each_with_index do |item, i|
45
- item = item[i] if item.is_a? LHS::Collection
46
- link = item[key.to_sym]
47
- link.merge_raw!(addition[i]) if link.present?
48
- end
44
+ extend_base_collection!(data, addition, key)
49
45
  elsif data[key]._raw.is_a? Array
50
- data[key].zip(addition) do |item, additional_item|
51
- item._raw.merge!(additional_item._raw)
52
- end
53
- elsif data._proxy.is_a? LHS::Item
46
+ extend_base_array!(data, addition, key)
47
+ elsif data.item?
48
+ extend_base_item!(data, addition, key)
49
+ end
50
+ end
51
+
52
+ def extend_base_collection!(data, addition, key)
53
+ data.each_with_index do |item, i|
54
+ item = item[i] if item.is_a? LHS::Collection
55
+ link = item[key.to_sym]
56
+ link.merge_raw!(addition[i]) if link.present?
57
+ end
58
+ end
59
+
60
+ def extend_base_array!(data, addition, key)
61
+ data[key].zip(addition) do |item, additional_item|
62
+ item._raw.merge!(additional_item._raw)
63
+ end
64
+ end
65
+
66
+ def extend_base_item!(data, addition, key)
67
+ if addition.collection?
68
+ extend_base_item_with_collection!(data, addition, key)
69
+ else # simple case merges hash into hash
54
70
  data._raw[key.to_sym].merge!(addition._raw)
55
71
  end
56
72
  end
57
73
 
74
+ def extend_base_item_with_collection!(data, addition, key)
75
+ target = data[key]
76
+ if target._raw.is_a? Array
77
+ data[key] = addition.map(&:_raw)
78
+ else # hash with items
79
+ target._raw[items_key] = addition.map(&:_raw)
80
+ end
81
+ end
82
+
58
83
  def handle_includes(includes, data)
59
84
  if includes.is_a? Hash
60
85
  includes.each { |included, sub_includes| handle_include(included, data, sub_includes) }
@@ -76,7 +101,7 @@ class LHS::Record
76
101
  url_option_for(data, included)
77
102
  end
78
103
  addition = load_include(options, data, sub_includes)
79
- extend_raw_data(data, addition, included)
104
+ extend_raw_data!(data, addition, included)
80
105
  end
81
106
 
82
107
  def skip_loading_includes?(data, included)
data/lib/lhs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "5.6.4"
2
+ VERSION = "5.6.5"
3
3
  end
@@ -312,4 +312,28 @@ describe LHS::Record do
312
312
  expect(place.available_products.empty?).to eq true
313
313
  end
314
314
  end
315
+
316
+ context 'extend items with arrays' do
317
+ it 'extends base items with arrays' do
318
+ class Place < LHS::Record
319
+ endpoint ':datastore/place'
320
+ endpoint ':datastore/place/:id'
321
+ end
322
+
323
+ stub_request(:get, "#{datastore}/place/1")
324
+ .to_return(body: {
325
+ 'contracts' => {
326
+ 'items' => [{ 'href' => "#{datastore}/place/1/contacts/1" }]
327
+ }
328
+ }.to_json)
329
+
330
+ stub_request(:get, "#{datastore}/place/1/contacts/1")
331
+ .to_return(body: {
332
+ 'products' => { 'href' => "#{datastore}/place/1/contacts/1/products" }
333
+ }.to_json)
334
+
335
+ place = Place.includes(:contracts).find(1)
336
+ expect(place.contracts.first.products.href).to eq "#{datastore}/place/1/contacts/1/products"
337
+ end
338
+ end
315
339
  end
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: 5.6.4
4
+ version: 5.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors