parascope 1.3.0 → 1.4.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: 3066d6e82e5f23716dd07cea4c449017e70bbd9d
4
- data.tar.gz: 56028229436dde9aeb953c6d2a8c3422481a92a4
3
+ metadata.gz: 34c9b159851be5b35aad5b3dee7a5d156a7a9c43
4
+ data.tar.gz: 4e105d70c12be7c2787e01b56cbcd9a9154388d0
5
5
  SHA512:
6
- metadata.gz: 3067309bd1f430f5aa295a76302b4cd16e23cfdc2a35ec0fccaa14f1a71df6d07663e6adbc38a90222de450c5d35df67b54f48391deb9e90580f2a0728029b93
7
- data.tar.gz: 6ac678b8533decc7fabc77179d534f06e1e1c63060025572875bdfb23ca268ca90d1f36060f74f2061999044328d38f470eba184cedbae2c26a0cfc3d67682ff
6
+ metadata.gz: 776eff3eec074bcafa312eb497698701cfb78dbee50efb62873398dcb25539dedc70fa13df10b6cdba5e6db45eff68c107ff7582ed418d094d40b0408fcd13ed
7
+ data.tar.gz: 91a03a0657bc4e99340adcc5cd48ae6d8f88e227f0a09e2d01eb47adac63ef3194a896330d8cdd3c99c77da5b1f5d61eee9dee0f39e57b44bcdd269698700a49
data/README.md CHANGED
@@ -148,14 +148,18 @@ sifter :with_department do
148
148
  end
149
149
  ```
150
150
 
151
- - `defaults(hash)` method is used to declare default query params that are reverse
152
- merged with params passed on query initialization. When used in `sift_by` block,
153
- hashes are merged altogether.
151
+ - `defaults(hash, &block)` method is used to declare default query params that are
152
+ reverse merged with params passed on query initialization. When used in `sift_by`
153
+ block, hashes are merged altogether. If `block` is passed, it's return value
154
+ will be evaluated and merged on query object instantiation, allowing to have
155
+ dynamic default params values. It is also allowed to have multiple `defaults`
156
+ method calls.
154
157
 
155
158
  *Examples:*
156
159
 
157
160
  ```ruby
158
161
  defaults only_active: true
162
+ defaults { {later_than: 1.week.ago} }
159
163
 
160
164
  sifter :paginated do
161
165
  # sifter defaults are merged with higher-level defaults:
@@ -163,6 +167,15 @@ sifter :paginated do
163
167
  end
164
168
  ```
165
169
 
170
+ It is also possible to use both static arguments and block in `defaults` method call.
171
+ Thus, the sample above can also be written with one `defaults` method call:
172
+
173
+ ```ruby
174
+ defaults only_active: true do
175
+ {later_than: 1.week.ago}
176
+ end
177
+ ```
178
+
166
179
  - `guard(message = nil, &block)` defines a guard instance method block (see instance methods
167
180
  bellow). All such blocks are executed before query object resolves scope via
168
181
  `resolve_scope` method. Optional `message` may be supplied to provide more informative
@@ -9,6 +9,7 @@ class Query < Parascope::Query
9
9
  raise_on_guard_violation false
10
10
 
11
11
  defaults barbak: 'barbak'
12
+ defaults { {time: Time.now} }
12
13
 
13
14
  base_scope { OpenStruct.new }
14
15
 
@@ -10,12 +10,12 @@ module Parascope
10
10
  attr_reader :params, :violation
11
11
 
12
12
  def self.inherited(subclass)
13
- subclass.raise_on_guard_violation raise_on_guard_violation?
14
- subclass.query_blocks.replace query_blocks.dup
15
- subclass.sift_blocks.replace sift_blocks.dup
16
- subclass.guard_blocks.replace guard_blocks.dup
13
+ subclass.raise_on_guard_violation(raise_on_guard_violation?)
14
+ subclass.query_blocks.replace(query_blocks.dup)
15
+ subclass.sift_blocks.replace(sift_blocks.dup)
16
+ subclass.guard_blocks.replace(guard_blocks.dup)
17
17
  subclass.base_scope(&base_scope)
18
- subclass.defaults defaults
18
+ subclass.defaults(defaults)
19
19
  end
20
20
 
21
21
  def self.build(**attrs)
@@ -31,7 +31,7 @@ module Parascope
31
31
  end
32
32
 
33
33
  def initialize(params, scope: nil, dataset: nil, **attrs)
34
- @params = Hashie::Mash.new(klass.defaults).merge(params || {})
34
+ @params = Hashie::Mash.new(klass.fetch_defaults).merge(params || {})
35
35
  @scope = scope || dataset unless scope.nil? && dataset.nil?
36
36
  @attrs = attrs.freeze
37
37
  @base_params = @params
@@ -106,13 +106,13 @@ module Parascope
106
106
  def sifted!(query, blocks)
107
107
  @attrs = query.attrs
108
108
  define_attr_readers
109
- singleton_class.query_blocks.replace query.klass.query_blocks.dup
110
- singleton_class.guard_blocks.replace query.klass.guard_blocks.dup
109
+ singleton_class.query_blocks.replace(query.klass.query_blocks.dup)
110
+ singleton_class.guard_blocks.replace(query.klass.guard_blocks.dup)
111
111
  singleton_class.base_scope(&query.klass.base_scope)
112
112
  blocks.each do |block|
113
113
  singleton_class.instance_exec(*block.values_for(params), &block.block)
114
114
  end
115
- params.replace(singleton_class.defaults.merge(params))
115
+ params.replace(singleton_class.fetch_defaults.merge(params))
116
116
  @sifted = true
117
117
  end
118
118
 
@@ -7,12 +7,26 @@ module Parascope
7
7
  end
8
8
  alias_method :base_dataset, :base_scope
9
9
 
10
- def defaults(params = nil)
11
- @defaults ||= {}
10
+ def defaults(params = nil, &block)
11
+ return @defaults if params.nil? && !block_given?
12
12
 
13
- return @defaults if params.nil?
13
+ if block_given? && !params.nil?
14
+ defaults(params)
15
+ defaults(&block)
16
+ elsif !block_given? && Proc === params
17
+ @defaults = params
18
+ elsif !block_given?
19
+ defaults { params }
20
+ elsif @defaults.nil?
21
+ @defaults = block
22
+ else
23
+ _defaults = @defaults
24
+ @defaults = -> { block.call.merge(_defaults.call) }
25
+ end
26
+ end
14
27
 
15
- @defaults = @defaults.merge(params)
28
+ def fetch_defaults
29
+ defaults.nil? ? {} : defaults.call
16
30
  end
17
31
 
18
32
  def sift_by(*presence_fields, **value_fields, &block)
@@ -1,3 +1,3 @@
1
1
  module Parascope
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parascope
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Kuzko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-24 00:00:00.000000000 Z
11
+ date: 2019-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie