datasource 0.0.4 → 0.0.5

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: 23eea085a171b960e60cc14b7f7b23bb8d1841c1
4
- data.tar.gz: 682ae5dfcec5ddb222e4be281d130a26aa92f6d9
3
+ metadata.gz: 107812940a2c8fc80ebec710b0ebd63c5f710fd4
4
+ data.tar.gz: b0d4b77a9cace8c1f559d39738065cfe69930c55
5
5
  SHA512:
6
- metadata.gz: da326434c18cad2aab819d5cd7a79c187bf9d17353e820d0232e27d0e33cb3278e0d9ff91b054d42a0f603a996f239b1300f25ed1c639e49717cff4d5f6f191e
7
- data.tar.gz: e165f247e0f7f52a9e0668b89a18d078d6b0ae603627743475d852d90f30a0d4ad1e0a8d1aeefa9d6f79fd85edd9bb931132961211e2bbf964326d34ee59b13e
6
+ metadata.gz: 960aa77805c5fe4e0a2c1ac658956bf0c2b4d8e81769c96850a012b5e95facac053ba6407f1b8d88ec65749b7ecc813fb6229502302ea4d070873b4483d8113f
7
+ data.tar.gz: 22dad36f267317cb360ffc30d035b8efd3074faa3ae801e26bbef76c3b9c9d358f417f8a0dee31cf37ea9a49c92e33afe472a753b2d86276e13319cf72b057e8
data/README.md CHANGED
@@ -151,13 +151,13 @@ A loader will only be executed if a computed attribute depends on it. If an attr
151
151
  on multiple loaders, pass an array of loaders like so `computed :attr, loaders: [:loader1, :loader2]`.
152
152
 
153
153
  Be careful that if your hash does not contain a value for the object ID, the loaded value
154
- will be nil.
154
+ will be nil. However you can use the `default` option for such cases (see below example).
155
155
 
156
156
  ```ruby
157
157
  class User < ActiveRecord::Base
158
158
  datasource_module do
159
159
  computed :post_count, loaders: :post_counts
160
- loader :post_counts, array_to_hash: true do |user_ids|
160
+ loader :post_counts, array_to_hash: true, default: 0 do |user_ids|
161
161
  results = Post
162
162
  .where(user_id: user_ids)
163
163
  .group(:user_id)
@@ -171,7 +171,7 @@ class UserSerializer < ActiveModel::Serializer
171
171
 
172
172
  def post_count
173
173
  # Will automatically give you the value for this user's ID
174
- object.loaded_values[:post_counts] || 0
174
+ object.loaded_values[:post_counts]
175
175
  end
176
176
  end
177
177
  ```
@@ -48,7 +48,7 @@ module Datasource
48
48
  scope = if respond_to?(:use_datasource_serializer)
49
49
  self
50
50
  else
51
- self.extend(ScopeExtensions).use_datasource(default_datasource)
51
+ self.extend(ScopeExtensions).use_datasource(Adapters::Sequel.scope_to_class(self).default_datasource)
52
52
  end
53
53
  scope.use_datasource_serializer(serializer || Datasource::Base.consumer_adapter.get_serializer_for(Adapters::Sequel.scope_to_class(scope)))
54
54
  end
@@ -13,6 +13,10 @@ module Datasource
13
13
  self._options.merge!(hash)
14
14
  end
15
15
 
16
+ def default_value
17
+ self._options[:default]
18
+ end
19
+
16
20
  def load(*args, &block)
17
21
  args = args.slice(0, _load_proc.arity) if _load_proc.arity >= 0
18
22
  results = _load_proc.call(*args, &block)
@@ -177,7 +177,7 @@ module Datasource
177
177
  end
178
178
  end
179
179
  rows.each do |row|
180
- row.loaded_values[name] = loaded_values[row.send(primary_key)]
180
+ row.loaded_values[name] = loaded_values[row.send(primary_key)] || loader.default_value
181
181
  end
182
182
  end
183
183
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datasource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Berdajs