lhs 25.0.3 → 25.0.4

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
  SHA256:
3
- metadata.gz: 7aa5e0e072a78d7029c968c255ac6c41e5425504b7e121fc5c205a672c59d4a7
4
- data.tar.gz: 59a90ded930e12b7cc54842db415d5d0f2c09ade777eaccc44d9703d4bc1f46a
3
+ metadata.gz: cbf1bd4d4d3bec5d8264176e1b7806a4b04f56bceabd6b72f51efb50a0a49b46
4
+ data.tar.gz: f60fffae1148244ddb5d52c379668dbac292a3868baa1862f52362ecb7537ea5
5
5
  SHA512:
6
- metadata.gz: e953b0d8278c209f1a398298d6bbf85b91469cf285bc7c0470b401cacfce2db7b692822e7d13488c4b36c823cef1c093bc8ffb4a5d2d53ab5782765589711b07
7
- data.tar.gz: d7124e16eca63583fb58922354253c902bbd86e0079c22e3d8b864a7a80b8acd098128a67c2e32cfe45c6ed28d19158def5a063fec174bdba364435b11054d92
6
+ metadata.gz: 22e0045d22ae49e4537329ffb53d1db8ef070da54822ffda08315e85ed7fe6c6e66656e65f61d7d06a4aa2010f34a8a52d6f614c1da8ad13e88628538ebc0b4f
7
+ data.tar.gz: e297eabef047eed5cc42b102d13b6043c678ea7f2fc8c620d04a4b8b794ee0028459e65d80a3361cc7ea43ad2e17aa966323859b735a632847e77e376266fb9c
@@ -39,7 +39,7 @@ class LHS::Collection < LHS::Proxy
39
39
  def compact
40
40
  dup.tap do |collection|
41
41
  collection.compact! if collection.raw.present?
42
- end
42
+ end.as_json # do not return an internal collection!
43
43
  end
44
44
 
45
45
  def compact!
@@ -9,11 +9,11 @@ class LHS::Data
9
9
 
10
10
  # Extends already fetched data (self) with additionally
11
11
  # fetched data (addition) using the given key
12
- def extend!(addition, key)
12
+ def extend!(addition, key = nil)
13
13
  addition = cast_relation_class_for_extension(addition, key)
14
14
  if collection?
15
15
  extend_collection!(addition, key)
16
- elsif self[key]._raw.is_a? Array
16
+ elsif _raw.is_a?(Array) || self[key]._raw.is_a?(Array)
17
17
  extend_array!(addition, key)
18
18
  elsif item?
19
19
  extend_item!(addition, key)
@@ -22,14 +22,14 @@ class LHS::Data
22
22
 
23
23
  private
24
24
 
25
- def cast_relation_class_for_extension(addition, key)
26
- return addition if _record.nil? || _record._relations.nil? || _record._relations[key].nil?
25
+ def cast_relation_class_for_extension(addition, key = nil)
26
+ return addition if _record.nil? || key.nil? || _record._relations.nil? || _record._relations[key].nil?
27
27
  addition.becomes(_record._relations[key][:record_class_name].constantize, errors: addition.errors, warnings: addition.warnings)
28
28
  end
29
29
 
30
- def extend_collection!(addition, key)
30
+ def extend_collection!(addition, key = nil)
31
31
  map do |item|
32
- item_raw = item._raw[key]
32
+ item_raw = key ? item._raw[key] : item._raw
33
33
  item_raw.blank? ? [nil] : item_raw
34
34
  end
35
35
  .flatten
@@ -45,25 +45,25 @@ class LHS::Data
45
45
  end
46
46
  end
47
47
 
48
- def extend_array!(addition, key)
49
- self[key].zip(addition) do |item, additional_item|
48
+ def extend_array!(addition, key = nil)
49
+ (key ? self[key] : self).zip(addition) do |item, additional_item|
50
50
  item._raw.merge!(additional_item._raw) if additional_item.present?
51
51
  end
52
52
  end
53
53
 
54
- def extend_item!(addition, key)
54
+ def extend_item!(addition, key = nil)
55
55
  return if addition.nil?
56
56
  if addition.collection?
57
57
  extend_item_with_collection!(addition, key)
58
58
  else # simple case merges hash into hash
59
- _raw[key.to_sym].merge!(addition._raw)
59
+ (key ? _raw[key.to_sym] : _raw).merge!(addition._raw)
60
60
  end
61
61
  end
62
62
 
63
- def extend_item_with_collection!(addition, key)
64
- target = self[key]
63
+ def extend_item_with_collection!(addition, key = nil)
64
+ target = (key ? self[key] : self)
65
65
  if target._raw.is_a? Array
66
- self[key] = addition.map(&:_raw)
66
+ self[key] = addition.map(&:_raw) if key
67
67
  else # hash with items
68
68
  extend_item_with_hash_containing_items!(target, addition)
69
69
  end
@@ -72,7 +72,11 @@ class LHS::Data
72
72
  def extend_item_with_hash_containing_items!(target, addition)
73
73
  LHS::Collection.nest(input: target._raw, value: [], record: self) # inits the nested collection
74
74
  if LHS::Collection.access(input: target._raw, record: self).empty?
75
- LHS::Collection.nest(input: target._raw, value: addition.reject { |item| item.nil? }, record: self)
75
+ LHS::Collection.nest(
76
+ input: target._raw,
77
+ value: addition.reject { |item| item.nil? },
78
+ record: self
79
+ )
76
80
  else
77
81
  LHS::Collection.access(input: target._raw, record: self).each_with_index do |item, index|
78
82
  item.merge!(addition[index])
@@ -321,11 +321,22 @@ class LHS::Record
321
321
  end
322
322
 
323
323
  # Continues loading included resources after one complete batch/level has been fetched
324
- def continue_including(data, including, referencing)
325
- handle_includes(including, data, referencing) if including.present? && data.present?
324
+ def continue_including(data, included, reference)
325
+ return data if included.blank? || data.blank?
326
+ expand_data!(data, included, reference) unless expanded_data?(data)
327
+ handle_includes(included, data, reference)
326
328
  data
327
329
  end
328
330
 
331
+ def expand_data!(data, _included, reference)
332
+ options = options_for_data(data)
333
+ options = extend_with_reference(options, reference)
334
+ record = record_for_options(options) || self
335
+ options = convert_options_to_endpoints(options) if record_for_options(options)
336
+ expanded_data = record.request(options)
337
+ data.extend!(expanded_data)
338
+ end
339
+
329
340
  # Loads all included/linked resources,
330
341
  # paginates itself to ensure all records are fetched
331
342
  def load_all_included!(record, options)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHS
4
- VERSION = '25.0.3'
4
+ VERSION = '25.0.4'
5
5
  end
@@ -66,6 +66,11 @@ describe LHS::Record do
66
66
  end
67
67
 
68
68
  context '.compact' do
69
+
70
+ it 'does NOT return an internal data type, but the Record class' do
71
+ expect(places.compact.class).to eq User
72
+ end
73
+
69
74
  it 'removes linked resouces which could not get fetched' do
70
75
  expect(places.compact.length).to eq 1
71
76
  expect(places.length).not_to eq 1 # leaves the original intact
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHS::Record do
6
+ context 'includes records after expansion' do
7
+
8
+ before do
9
+ class User < LHS::Record
10
+ endpoint 'http://users/{id}'
11
+ end
12
+
13
+ class Places < LHS::Record
14
+ endpoint 'http://users/{id}/places'
15
+ endpoint 'http://places/{id}'
16
+ end
17
+
18
+ class Contracts < LHS::Record
19
+ endpoint 'http://places/{place_id}/contracts'
20
+ end
21
+
22
+ stub_request(:get, 'http://users/1')
23
+ .to_return(
24
+ body: {
25
+ places: {
26
+ href: 'http://users/1/places'
27
+ }
28
+ }.to_json
29
+ )
30
+
31
+ stub_request(:get, 'http://users/1/places?limit=100')
32
+ .to_return(
33
+ body: {
34
+ items: [
35
+ { href: 'http://places/345' }
36
+ ],
37
+ total: 1,
38
+ offset: 0,
39
+ limit: 10
40
+ }.to_json
41
+ )
42
+
43
+ stub_request(:get, 'http://places/345')
44
+ .to_return(
45
+ body: {
46
+ contracts: {
47
+ href: "http://places/345/contracts?offset=0&limit=10"
48
+ }
49
+ }.to_json
50
+ )
51
+
52
+ stub_request(:get, 'http://places/345/contracts?offset=0&limit=10')
53
+ .to_return(
54
+ body: {
55
+ items: [
56
+ {
57
+ product: { name: 'OPL' }
58
+ }
59
+ ]
60
+ }.to_json
61
+ )
62
+
63
+ end
64
+
65
+ it 'includes resources after expanding plain links' do
66
+ user = User.includes(places: :contracts).find(1)
67
+ expect(
68
+ user.places.first.contracts.first.product.name
69
+ ).to eq 'OPL'
70
+ end
71
+ end
72
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 25.0.3
4
+ version: 25.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-10 00:00:00.000000000 Z
11
+ date: 2020-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -505,6 +505,7 @@ files:
505
505
  - spec/record/href_for_spec.rb
506
506
  - spec/record/ignore_errors_spec.rb
507
507
  - spec/record/immutable_chains_spec.rb
508
+ - spec/record/includes_after_expansion_spec.rb
508
509
  - spec/record/includes_expanded_spec.rb
509
510
  - spec/record/includes_first_page_spec.rb
510
511
  - spec/record/includes_missing_spec.rb
@@ -737,6 +738,7 @@ test_files:
737
738
  - spec/record/href_for_spec.rb
738
739
  - spec/record/ignore_errors_spec.rb
739
740
  - spec/record/immutable_chains_spec.rb
741
+ - spec/record/includes_after_expansion_spec.rb
740
742
  - spec/record/includes_expanded_spec.rb
741
743
  - spec/record/includes_first_page_spec.rb
742
744
  - spec/record/includes_missing_spec.rb