parascope 1.2.0 → 1.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: 6caa45deb0d23160ab7595714e4517eed4306e10
4
- data.tar.gz: e407dc5cb9e132c3c814ce43ad94ea64a1c57fb4
3
+ metadata.gz: 3066d6e82e5f23716dd07cea4c449017e70bbd9d
4
+ data.tar.gz: 56028229436dde9aeb953c6d2a8c3422481a92a4
5
5
  SHA512:
6
- metadata.gz: 6af3bf60ea924a9b8326f962424f08ce71b0623fdc5c6277109e0eee70077ccd368ca1b2193a2a5410dda1fc2f619232fcbd8039f18caee79c2e60d1effc3126
7
- data.tar.gz: baedb425a6571abd713856e34bd3859a75d4c38a2146ccaa96efc56ca9b56924f9c70f13bfb7c713cdaba3848132852c613f5f316d47d33e19988e2c64219904
6
+ metadata.gz: 3067309bd1f430f5aa295a76302b4cd16e23cfdc2a35ec0fccaa14f1a71df6d07663e6adbc38a90222de450c5d35df67b54f48391deb9e90580f2a0728029b93
7
+ data.tar.gz: 6ac678b8533decc7fabc77179d534f06e1e1c63060025572875bdfb23ca268ca90d1f36060f74f2061999044328d38f470eba184cedbae2c26a0cfc3d67682ff
data/README.md CHANGED
@@ -163,9 +163,10 @@ sifter :paginated do
163
163
  end
164
164
  ```
165
165
 
166
- - `guard(&block)` defines a guard instance method block (see instance methods
166
+ - `guard(message = nil, &block)` defines a guard instance method block (see instance methods
167
167
  bellow). All such blocks are executed before query object resolves scope via
168
- `resolve_scope` method.
168
+ `resolve_scope` method. Optional `message` may be supplied to provide more informative
169
+ error message.
169
170
 
170
171
  *Examples:*
171
172
 
@@ -173,12 +174,39 @@ end
173
174
  sift_by(:sort_col, :sort_dir) do |scol, sdir|
174
175
  # will raise Parascope::GuardViolationError on scope resolution if
175
176
  # params[:sort_dir] is not 'asc' or 'desc'
176
- guard { sdir.downcase.in?(%w(asc desc)) }
177
+ guard(':sort_dir should be "asc" or "desc"') do
178
+ sdir.downcase.in?(%w(asc desc))
179
+ end
177
180
 
178
181
  base_scope { |scope| scope.order(scol => sdir) }
179
182
  end
180
183
  ```
181
184
 
185
+ - `raise_on_guard_violation(value)` allows to specify whether or not exception should be raised
186
+ whenever any guard block is violated during scope resolution. When set to `false`, in case
187
+ of any violation, `resolved_scope` will return `nil`, and query will have `violation` property
188
+ set with value corresponding to the message of violated block. Default option value is `true`.
189
+
190
+ *Examples:*
191
+
192
+ ```ruby
193
+ raise_on_guard_violation false
194
+
195
+ sift_by(:sort_col, :sort_dir) do |scol, sdir|
196
+ guard(':sort_dir should be "asc" or "desc"') do
197
+ sdir.downcase.in?(%w(asc desc))
198
+ end
199
+
200
+ base_scope { |scope| scope.order(scol => sdir) }
201
+ end
202
+ ```
203
+
204
+ ```ruby
205
+ query = UsersQuery.new(sort_col: 'id', sort_dir: 'there')
206
+ query.resolved_scope # => nil
207
+ query.violation # => ":sort_dir should be \"asc\" or \"desc\""
208
+ ```
209
+
182
210
  - `build(scope: nil, **attributes)` initializes a query with empty params. Handy when
183
211
  query depends only passed attributes and internal logic. Also useful in specs.
184
212
 
@@ -6,12 +6,14 @@ require "parascope"
6
6
  require 'ostruct'
7
7
 
8
8
  class Query < Parascope::Query
9
+ raise_on_guard_violation false
10
+
9
11
  defaults barbak: 'barbak'
10
12
 
11
13
  base_scope { OpenStruct.new }
12
14
 
13
15
  query_by :foo do |foo|
14
- guard { foo == 'foo' }
16
+ guard('wrong value for :foo param. try "foo"') { foo == 'foo' }
15
17
 
16
18
  scope.tap{ scope.foo = foo }
17
19
  end
@@ -5,4 +5,6 @@ module Parascope
5
5
  GuardViolationError = Class.new(ArgumentError)
6
6
 
7
7
  autoload :Query, "parascope/query"
8
+
9
+ Query.raise_on_guard_violation true
8
10
  end
@@ -7,9 +7,10 @@ module Parascope
7
7
 
8
8
  extend ApiMethods
9
9
 
10
- attr_reader :params
10
+ attr_reader :params, :violation
11
11
 
12
12
  def self.inherited(subclass)
13
+ subclass.raise_on_guard_violation raise_on_guard_violation?
13
14
  subclass.query_blocks.replace query_blocks.dup
14
15
  subclass.sift_blocks.replace sift_blocks.dup
15
16
  subclass.guard_blocks.replace guard_blocks.dup
@@ -21,6 +22,14 @@ module Parascope
21
22
  new({}, **attrs)
22
23
  end
23
24
 
25
+ def self.raise_on_guard_violation(value)
26
+ @raise_on_guard_violation = !!value
27
+ end
28
+
29
+ def self.raise_on_guard_violation?
30
+ @raise_on_guard_violation
31
+ end
32
+
24
33
  def initialize(params, scope: nil, dataset: nil, **attrs)
25
34
  @params = Hashie::Mash.new(klass.defaults).merge(params || {})
26
35
  @scope = scope || dataset unless scope.nil? && dataset.nil?
@@ -51,10 +60,14 @@ module Parascope
51
60
  alias_method :base_dataset, :base_scope
52
61
 
53
62
  def resolved_scope(*args)
63
+ @violation = nil
54
64
  arg_params = args.pop if args.last.is_a?(Hash)
55
65
  return sifted_instance.resolved_scope! if arg_params.nil? && args.empty?
56
66
 
57
67
  clone_with_params(trues(args).merge(arg_params || {})).resolved_scope
68
+ rescue GuardViolationError => error
69
+ @violation = error.message
70
+ raise if self.class.raise_on_guard_violation?
58
71
  end
59
72
  alias_method :resolved_dataset, :resolved_scope
60
73
  alias_method :resolve, :resolved_scope
@@ -138,13 +151,15 @@ module Parascope
138
151
  private
139
152
 
140
153
  def guard_all
141
- klass.guard_blocks.each{ |block| guard(&block) }
154
+ klass.guard_blocks.each{ |message, block| guard(message, &block) }
142
155
  end
143
156
 
144
- def guard(&block)
145
- unless instance_exec(&block)
146
- fail GuardViolationError, "guard block violated on #{block.source_location.join(':')}"
147
- end
157
+ def guard(message = nil, &block)
158
+ return if instance_exec(&block)
159
+
160
+ violation = message || "guard block violated on #{block.source_location.join(':')}"
161
+
162
+ fail GuardViolationError, violation
148
163
  end
149
164
 
150
165
  def sifted_instance_for(blocks)
@@ -55,8 +55,8 @@ module Parascope
55
55
  alias_method :sifter!, :sift_by!
56
56
  alias_method :query!, :query_by!
57
57
 
58
- def guard(&block)
59
- guard_blocks.push block
58
+ def guard(message = nil, &block)
59
+ guard_blocks.push([message, block])
60
60
  end
61
61
 
62
62
  def sift_blocks
@@ -1,3 +1,3 @@
1
1
  module Parascope
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.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.2.0
4
+ version: 1.3.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: 2018-01-13 00:00:00.000000000 Z
11
+ date: 2019-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie