lhs 14.5.0 → 14.6.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: 66878335134271a8a2adadfb4a3318d029f0a854
4
- data.tar.gz: c579b0f2377f06413b247d58023f10ed1bb908b6
3
+ metadata.gz: 077d421d6966d805ebcd502e5a0c093cc6bcf4a4
4
+ data.tar.gz: c687c58f9bde359b6576812aa9eebd19b984c5cc
5
5
  SHA512:
6
- metadata.gz: 48df46b051c333e615122cf5fda68298b2bf3f4116631f93a987408c8942e7e295e52b4d24830861867f099bc74f473e28bd6a9c1c7ae16c43007d7be81f9c31
7
- data.tar.gz: f138b1a4a1d25b7b43de711b7bdf4038a13330cd34c94784123234cf9b0be62f25c9cd2c02593916f111542bb1c9c24b70c60d356be90fed82a87a3a5d908f1e
6
+ metadata.gz: a63875a01836b652d4b049df0c50b3c314fe2ff6306e37134e149defb1bdf1addc904455406991f974954c6de2ee0906c20205f4bd3ad4127fd583755c64afff
7
+ data.tar.gz: 78388e5606346ecbb96d944cf25285e2fbbbecdb1b9d6647dac8c940646186ae7a5ec88574cf1ec2d42b164f9f7fc75588dd4927d7d4689719f2515b775b8341
data/.rubocop.yml CHANGED
@@ -35,3 +35,6 @@ Style/EmptyLinesAroundBlockBody:
35
35
 
36
36
  RSpec/MultipleExpectations:
37
37
  Enabled: false
38
+
39
+ Naming/PredicateName:
40
+ Enabled: false
data/README.md CHANGED
@@ -266,6 +266,36 @@ After fetching [single](#find-single-records) or [multiple](#find-multiple-recor
266
266
  record.parent == records # true
267
267
  ```
268
268
 
269
+ ## Relations
270
+
271
+ Even though, nested data is automatically casted when accessed, see: [Nested records](#nested-records), sometimes api's don't provide dedicated endpoints to retrive these records.
272
+
273
+ As those records also don't have an href, nested records can not be casted automatically, when accessed.
274
+
275
+ Those kind of relations, you can still configure manually:
276
+
277
+ ```ruby
278
+
279
+ class Location < LHS::Record
280
+
281
+ endpoint 'http://uberall/locations/:id'
282
+
283
+ has_many :listings
284
+
285
+ end
286
+
287
+ class Listing < LHS::Record
288
+
289
+ def supported?
290
+ type == 'SUPPORTED'
291
+ end
292
+ end
293
+
294
+ Location.find(1).listings.first.supported? # true
295
+
296
+ ```
297
+
298
+
269
299
  ## Request based options
270
300
 
271
301
  You can apply options to the request chain. Those options will be forwarded to the request perfomed by the chain/query.
@@ -643,7 +673,7 @@ class LocalEntry < LHS::Record
643
673
  end
644
674
  ```
645
675
 
646
- ### Nested records
676
+ ## Nested records
647
677
 
648
678
  Nested records (in nested data) are automaticaly casted when the href matches any defined endpoint of any LHS::Record.
649
679
 
@@ -1,6 +1,6 @@
1
1
  require 'active_support'
2
2
 
3
- class LHS::Item < LHS::Proxy
3
+ class LHS::Data
4
4
 
5
5
  module Becomes
6
6
  extend ActiveSupport::Concern
@@ -67,8 +67,9 @@ class LHS::Proxy
67
67
  wrap_return(collection, record, name)
68
68
  end
69
69
 
70
- # Wraps with record and adds nested errors/warnings to data,
71
- # if errors are existing
70
+ # Wraps the return value with a record class.
71
+ # Adds errors and warnings if existing.
72
+ # Returns plain data if no record class was found.
72
73
  def wrap_return(value, record, name, args = nil)
73
74
  name = args.first if name == :[]
74
75
  return value unless worth_wrapping?(value)
@@ -76,6 +77,7 @@ class LHS::Proxy
76
77
  data.errors = LHS::Problems::Nested::Errors.new(errors, name) if errors.any?
77
78
  data.warnings = LHS::Problems::Nested::Warnings.new(warnings, name) if warnings.any?
78
79
  return record.new(data) if record && !value.is_a?(LHS::Record)
80
+ return data.becomes(_record._relations[name][:record_class_name].constantize) if _record && _record._relations[name]
79
81
  data
80
82
  end
81
83
 
@@ -0,0 +1,20 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext/class/attribute'
3
+
4
+ class LHS::Record
5
+
6
+ module Relations
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ class_attribute :_relations
11
+ self._relations = {}
12
+ end
13
+
14
+ module ClassMethods
15
+ def has_many(name)
16
+ _relations[name] = { record_class_name: name.to_s.singularize.classify }
17
+ end
18
+ end
19
+ end
20
+ end
data/lib/lhs/data.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # Data provides functionalities to accesses information
2
2
  class LHS::Data
3
+ autoload :Becomes,
4
+ 'lhs/concerns/data/becomes'
3
5
  autoload :Equality,
4
6
  'lhs/concerns/data/equality'
5
7
  autoload :Json,
@@ -7,6 +9,7 @@ class LHS::Data
7
9
  autoload :ToHash,
8
10
  'lhs/concerns/data/to_hash'
9
11
 
12
+ include Becomes
10
13
  include Equality
11
14
  include Json
12
15
  include ToHash
data/lib/lhs/item.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  # An item is a concrete record.
2
2
  # It can be part of another proxy like collection.
3
3
  class LHS::Item < LHS::Proxy
4
- autoload :Becomes,
5
- 'lhs/concerns/item/becomes'
6
4
  autoload :Destroy,
7
5
  'lhs/concerns/item/destroy'
8
6
  autoload :Save,
@@ -12,7 +10,6 @@ class LHS::Item < LHS::Proxy
12
10
  autoload :Validation,
13
11
  'lhs/concerns/item/validation'
14
12
 
15
- include Becomes
16
13
  include Create
17
14
  include Destroy
18
15
  include Save
data/lib/lhs/proxy.rb CHANGED
@@ -17,7 +17,7 @@ class LHS::Proxy
17
17
 
18
18
  # prevent clashing with attributes of underlying data
19
19
  attr_accessor :_data, :_loaded
20
- delegate :_record, to: :_data, allow_nil: true
20
+ delegate :_record, :becomes, to: :_data, allow_nil: true
21
21
 
22
22
  def initialize(data)
23
23
  self._data = data
data/lib/lhs/record.rb CHANGED
@@ -27,6 +27,8 @@ class LHS::Record
27
27
  'lhs/concerns/record/pagination'
28
28
  autoload :Request,
29
29
  'lhs/concerns/record/request'
30
+ autoload :Relations,
31
+ 'lhs/concerns/record/relations'
30
32
  autoload :Scope,
31
33
  'lhs/concerns/record/scope'
32
34
 
@@ -52,10 +54,11 @@ class LHS::Record
52
54
  include Model
53
55
  include Pagination
54
56
  include Request
57
+ include Relations
55
58
  include RequestCycleCache
56
59
  include Scope
57
60
 
58
- delegate :_proxy, :_endpoint, :merge_raw!, :select, to: :_data
61
+ delegate :_proxy, :_endpoint, :merge_raw!, :select, :becomes, to: :_data
59
62
 
60
63
  def initialize(data = nil, apply_customer_setters = true)
61
64
  data = LHS::Data.new({}, nil, self.class) unless data
data/lib/lhs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = '14.5.0'
2
+ VERSION = '14.6.0'
3
3
  end
@@ -0,0 +1,34 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHS::Record do
4
+ before(:each) do
5
+ class Location < LHS::Record
6
+ endpoint 'http://uberall/locations'
7
+ endpoint 'http://uberall/locations/:id'
8
+ has_many :listings
9
+ end
10
+
11
+ class Listing < LHS::Record
12
+
13
+ def supported?
14
+ true
15
+ end
16
+ end
17
+ end
18
+
19
+ context 'has_many' do
20
+ let(:location) { Location.find(1) }
21
+ let(:listing) { location.listings.first }
22
+
23
+ it 'casts the relation into the correct type' do
24
+ stub_request(:get, 'http://uberall/locations/1')
25
+ .to_return(body: {
26
+ listings: [{
27
+ directory: { name: 'Instagram' }
28
+ }]
29
+ }.to_json)
30
+ expect(listing).to be_kind_of(Listing)
31
+ expect(listing.supported?).to eq true
32
+ end
33
+ end
34
+ end
@@ -25,7 +25,7 @@ RSpec.configure do |config|
25
25
  LHC::Config.instance._cleanup
26
26
  LHS::Record::Endpoints.all = {}
27
27
  LHS::Record::CHILDREN.each do |child|
28
- child.endpoints = [] if !child.name['LHS']
28
+ child.endpoints = [] if !child.name['LHS'] && defined?(child.endpoints)
29
29
  child.configuration({}) if !child.name['LHS']
30
30
  end
31
31
  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: 14.5.0
4
+ version: 14.6.0
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: 2017-10-18 00:00:00.000000000 Z
11
+ date: 2017-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lhc
@@ -201,11 +201,11 @@ files:
201
201
  - lib/lhs/concerns/collection/handle_nested.rb
202
202
  - lib/lhs/concerns/collection/internal_collection.rb
203
203
  - lib/lhs/concerns/configuration.rb
204
+ - lib/lhs/concerns/data/becomes.rb
204
205
  - lib/lhs/concerns/data/equality.rb
205
206
  - lib/lhs/concerns/data/json.rb
206
207
  - lib/lhs/concerns/data/to_hash.rb
207
208
  - lib/lhs/concerns/inspect.rb
208
- - lib/lhs/concerns/item/becomes.rb
209
209
  - lib/lhs/concerns/item/destroy.rb
210
210
  - lib/lhs/concerns/item/save.rb
211
211
  - lib/lhs/concerns/item/update.rb
@@ -227,6 +227,7 @@ files:
227
227
  - lib/lhs/concerns/record/mapping.rb
228
228
  - lib/lhs/concerns/record/model.rb
229
229
  - lib/lhs/concerns/record/pagination.rb
230
+ - lib/lhs/concerns/record/relations.rb
230
231
  - lib/lhs/concerns/record/request.rb
231
232
  - lib/lhs/concerns/record/request_cycle_cache/interceptor.rb
232
233
  - lib/lhs/concerns/record/request_cycle_cache/request_cycle_thread_registry.rb
@@ -372,6 +373,7 @@ files:
372
373
  - spec/record/find_in_parallel_spec.rb
373
374
  - spec/record/find_spec.rb
374
375
  - spec/record/first_spec.rb
376
+ - spec/record/has_many_spec.rb
375
377
  - spec/record/ignore_errors_spec.rb
376
378
  - spec/record/immutable_chains_spec.rb
377
379
  - spec/record/includes_all_spec.rb
@@ -555,6 +557,7 @@ test_files:
555
557
  - spec/record/find_in_parallel_spec.rb
556
558
  - spec/record/find_spec.rb
557
559
  - spec/record/first_spec.rb
560
+ - spec/record/has_many_spec.rb
558
561
  - spec/record/ignore_errors_spec.rb
559
562
  - spec/record/immutable_chains_spec.rb
560
563
  - spec/record/includes_all_spec.rb