lhs 3.2.0 → 3.3.0

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: 3104a36287bf44989ceded81a998fd803298b14c
4
- data.tar.gz: 48c62d18f012d593731a5ec8f0ed3c69992a8512
3
+ metadata.gz: f547564cf8d74f5da48573a93611abfce3435c33
4
+ data.tar.gz: 6e2fa4f9d0e7fb231398c9e6fb316af72f247fb1
5
5
  SHA512:
6
- metadata.gz: bc6ac276747f1c18059f7ae120aa7672af03dec3976886175883ac8dbd4faea963b5029ac427eb5a80e9887f64d9c994fcca9210e4e829d5e13af5124d2e01be
7
- data.tar.gz: 4cf2de0d3f4965de5c236a48e3a5fc89b033df6cebb88d1de1f23aa3c7cd9f38480afc2091df6d9fe4e90562b83ff16d561da89ddd338599c0bcb73676e3cf61
6
+ metadata.gz: 128895810bed258280baca8b81c9c3d38f99b5f150e921852272ee053abff994396d45ee0cff617bc4678fcd4144b10612790e879f22cfcf6aedb631345ff656
7
+ data.tar.gz: dab3d9af9b8d4806ac443c7f2fc55f1f5a8b5692d34233275bb4a6c3750902ee4f0a1d92fac5219dc1f753d6d61c8da5044f7dc2d1abd7abe7e08e3f0b65acb9
data/README.md CHANGED
@@ -73,7 +73,7 @@ Uses the `:datastore/v2/content-ads/:campaign_id/feedbacks` endpoint.
73
73
  If no record is found an error is raised.
74
74
 
75
75
  ## Proxy
76
- Instead of mapping data when it arrives from the backend, the proxy makes data accessible when you access it, not when you fetch it. The proxy is used to access data and it is divided in `Collection` and `Item`.
76
+ Instead of mapping data when it arrives from the backend, the proxy makes data accessible when you access it, not when you fetch it. The proxy is used to access data and it is divided in `Collection` and `Item`.
77
77
 
78
78
  `find` can also be used to find a single uniqe record with parameters:
79
79
 
@@ -104,7 +104,7 @@ If no record is found, `nil` is returned.
104
104
 
105
105
  ## Batch processing
106
106
 
107
- **Be carefull using methods for batch processing. They could result in a lot of HTTP requests!**
107
+ **Be careful using methods for batch processing. They could result in a lot of HTTP requests!**
108
108
 
109
109
  `all` fetches all records from the backend by doing multiple requests if necessary.
110
110
 
@@ -119,7 +119,7 @@ data.total # 998
119
119
  ```ruby
120
120
  Feedback.find_each(start: 50, batch_size: 20, params: { has_reviews: true }) do |feedback|
121
121
  # Iterates over each record. Starts with record nr. 50 and fetches 20 records each batch.
122
- feedback
122
+ feedback
123
123
  break if feedback.some_attribute == some_value
124
124
  end
125
125
  ```
@@ -162,6 +162,40 @@ Build and persist new items from scratch are done either with `new` or it's alia
162
162
  feedback.save
163
163
  ```
164
164
 
165
+ ## Custom setters and getters
166
+
167
+ Sometimes it is the case that you want to have your custom getters and setters and convert the data to backend processable format behind the scenes. The initializer will now use custom setter if one is defined
168
+
169
+ ```ruby
170
+ class Feedback < LHS::Record
171
+ def ratings=(ratings)
172
+ _raw[:ratings] = ratings.map { |k, v| { name: k, value: v } }
173
+ end
174
+ end
175
+
176
+ feedback = Feedback.new(ratings: { quality: 3 }) # <Feedback{:ratings=>[{:name=>:quality, :value=>3}]}>
177
+ feedback.ratings # #<LHS::Data:0x007fc8fa6d4050 ... @_raw=[{:name=>:quality, :value=>3}]>
178
+
179
+ ```
180
+
181
+ If you have an accompanying getter the whole data manipulation would be internal only.
182
+
183
+ ```ruby
184
+ class Feedback < LHS::Record
185
+ def ratings=(ratings)
186
+ _raw[:ratings] = ratings.map { |k, v| { name: k, value: v } }
187
+ end
188
+
189
+ def ratings
190
+ Hash[_raw[:ratings].map { |r| [r[:name], r[:value]] }]
191
+ end
192
+ end
193
+
194
+ feedback = Feedback.new(ratings: { quality: 3 }) # <Feedback{:ratings=>[{:name=>:quality, :value=>3}]}>
195
+ feedback.ratings # {:quality=>3}
196
+
197
+ ```
198
+
165
199
  ## Include linked resources
166
200
 
167
201
  When fetching records, you can specify in advance all the linked resources that you want to include in the results. With `includes`, LHS ensures that all matching and explicitly linked resources are loaded and merged.
@@ -191,17 +225,17 @@ The implementation is heavily influenced by [http://guides.rubyonrails.org/activ
191
225
  ```ruby
192
226
  # list of includes
193
227
  claims = Claims.includes(:localch_account, :entry).where(place_id: 'huU90mB_6vAfUdVz_uDoyA')
194
-
228
+
195
229
  # array of includes
196
230
  claims = Claims.includes([:localch_account, :entry]).where(place_id: 'huU90mB_6vAfUdVz_uDoyA')
197
-
231
+
198
232
  # Two-level with array of includes
199
233
  feedbacks = Feedback.includes(campaign: [:entry, :user]).where(has_reviews: true)
200
234
  ```
201
235
 
202
236
  ### Known LHS::Records are used to request linked resources
203
237
 
204
- When including linked resources with `includes`, known/defined services and endpoints are used to make those requests.
238
+ When including linked resources with `includes`, known/defined services and endpoints are used to make those requests.
205
239
  That also means that options for endpoints of linked resources are applied when requesting those in addition.
206
240
  This allows you to include protected resources (e.g. OAuth) as endpoint options for oauth authentication get applied.
207
241
 
@@ -222,7 +256,7 @@ class Place < LHS::Record
222
256
 
223
257
  end
224
258
 
225
- Favorite.includes(:place).where(user_id: current_user.id)
259
+ Favorite.includes(:place).where(user_id: current_user.id)
226
260
  # Will include places and applies endpoint options to authenticate the request.
227
261
  ```
228
262
 
@@ -21,15 +21,7 @@ class LHS::Record
21
21
  data = LHS::Data.new({}, nil, self.class) unless data
22
22
  data = LHS::Data.new(data, nil, self.class) unless data.is_a?(LHS::Data)
23
23
  define_singleton_method(:_data) { data }
24
- instance_data =
25
- if data._proxy.is_a?(LHS::Item) && data._raw.is_a?(Hash)
26
- data._raw
27
- elsif data._proxy.is_a?(LHS::Collection) && data._raw.is_a?(Hash)
28
- data._raw.fetch(:items, [])
29
- else
30
- data._raw
31
- end
32
- instance_variable_set('@data', instance_data)
24
+ consider_custom_setters
33
25
  end
34
26
 
35
27
  def as_json(options = nil)
@@ -40,6 +32,10 @@ class LHS::Record
40
32
  new(data)
41
33
  end
42
34
 
35
+ def inspect
36
+ "<#{self.class}#{_data._raw}>"
37
+ end
38
+
43
39
  protected
44
40
 
45
41
  def method_missing(name, *args, &block)
@@ -49,4 +45,23 @@ class LHS::Record
49
45
  def respond_to_missing?(name, include_all = false)
50
46
  _data.respond_to_missing?(name, include_all)
51
47
  end
48
+
49
+ private
50
+
51
+ def consider_custom_setters
52
+ return if !instance_data.is_a?(Hash)
53
+ instance_data.each do |k, v|
54
+ if public_methods.include?("#{k}=".to_sym)
55
+ send("#{k}=", v)
56
+ end
57
+ end
58
+ end
59
+
60
+ def instance_data
61
+ if _data._proxy.is_a?(LHS::Collection) && _data._raw.is_a?(Hash)
62
+ _data._raw.fetch(:items, [])
63
+ else
64
+ _data._raw
65
+ end
66
+ end
52
67
  end
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "3.2.0"
2
+ VERSION = "3.3.0"
3
3
  end
@@ -22,7 +22,44 @@ describe LHS::Record do
22
22
  end
23
23
 
24
24
  it 'builds new items also with keys containing dashes' do
25
- Feedback.new('some-key' => [])
25
+ feedback = Feedback.new('some-key' => [])
26
+ expect(feedback._raw[:'some-key']).to eq([])
27
+ end
28
+
29
+ context 'custom setters' do
30
+ before(:each) do
31
+ class Feedback
32
+ def ratings=(ratings)
33
+ _raw[:ratings] = ratings.map { |k, v| { name: k, value: v } }
34
+ end
35
+ end
36
+ end
37
+
38
+ it 'are used by initializer' do
39
+ feedback = Feedback.new(ratings: { a: 1, b: 2 })
40
+ expect(feedback.ratings.raw).to eq([{ name: :a, value: 1 }, { name: :b, value: 2 }])
41
+ end
42
+
43
+ it 'can be used directly to change raw data' do
44
+ feedback = Feedback.new(ratings: { a: 1 })
45
+ feedback.ratings = { z: 3 }
46
+ expect(feedback.ratings.first.name).to eq :z
47
+ end
48
+
49
+ context 'and custom getters' do
50
+ before(:each) do
51
+ class Feedback
52
+ def ratings
53
+ Hash[_raw[:ratings].map { |r| [r[:name], r[:value]] }]
54
+ end
55
+ end
56
+ end
57
+
58
+ it 'uses custom getters to show data for exploration' do
59
+ feedback = Feedback.new(ratings: { a: 1, b: 2 })
60
+ expect(feedback.ratings).to eq(a: 1, b: 2)
61
+ end
62
+ end
26
63
  end
27
64
  end
28
65
  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: 3.2.0
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors
@@ -310,7 +310,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
310
310
  requirements:
311
311
  - Ruby >= 1.9.2
312
312
  rubyforge_project:
313
- rubygems_version: 2.2.2
313
+ rubygems_version: 2.5.1
314
314
  signing_key:
315
315
  specification_version: 4
316
316
  summary: Rails gem providing an easy, active-record-like interface to use http backend