parascope 1.0.3 → 1.1.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: 9ae9437f9c91dc9a6f126c16b6f3c4a2c745634d
4
- data.tar.gz: 859be0e8b38ecc4e6af3d9fb0d379e464f54ab7e
3
+ metadata.gz: 17594187c5a94c9a523d77dba587407268717be5
4
+ data.tar.gz: 3f956433561618eac287109648fc34763227b6b2
5
5
  SHA512:
6
- metadata.gz: 49ad2eccded8195f771ad943704a9df0fbf56c62fc52ca00474311140b366f42a98e093c55f1a5ae4c89151640747286f93464f6790ccef1a52918afd7ee15ae
7
- data.tar.gz: 73c4988ec96e88e9973f6031d3ecd9bd48474777f3271d7f83ffd29c0ba7107dd278cc09c9dfe3ebd821684780d594b1647bd92ab2cc22670ce09c33a50e6a3d
6
+ metadata.gz: 705365aac8d26e59124e501142ef6858f808ffbe7a28d527bca39694dfcdb40ede594bc0a9b5510993357d6d4c40d4bbcc631c09122fdede9de74483738613fc
7
+ data.tar.gz: f3c944dae562511bd8b7951ce2b1fa02ecb91ead2b5a602ad638df0c06072fe0c55db8bd4e7f60c7f89023c7cd33ddd762f46633dca804f9d46eaae712d740db
data/CHANGELOG CHANGED
@@ -1,5 +1,7 @@
1
1
  === master
2
2
 
3
+ Improve :index option support, add query_by! and sift_by! methods
4
+
3
5
  === 1.0.3
4
6
 
5
7
  Explicitly apply query blocks in definition order
data/README.md CHANGED
@@ -5,7 +5,7 @@ Param-based scope generation.
5
5
  [![build status](https://secure.travis-ci.org/akuzko/parascope.png)](http://travis-ci.org/akuzko/parascope)
6
6
  [![github release](https://img.shields.io/github/release/akuzko/parascope.svg)](https://github.com/akuzko/parascope/releases)
7
7
 
8
- --
8
+ ---
9
9
 
10
10
  This gem provides a `Parascope::Query` class with a declarative and convenient API
11
11
  to build scopes (ActiveRecord relations or arbitrary objects) dynamically, based
@@ -51,15 +51,22 @@ scope manipulations using `query_by`, `sift_by` and other class methods bellow.
51
51
  returns a non-nil value, it becomes a new scope for subsequent processing. Of course,
52
52
  there can be multiple `query_by` block definitions. Methods accepts additional options:
53
53
  - `:index` - allows to specify order of query block applications. By default all query
54
- blocks have index of 0;
54
+ blocks have index of 0. This option also accepts special values `:first` and `:last` for
55
+ more convenient usage. Queries with the same value of `:index` option are applied in
56
+ order of declaration.
55
57
  - `:if` - specifies condition according to which query should be applied. If Symbol
56
58
  or String is passed, calls corresponding method. If Proc is passed, it is executed
57
59
  in context of query object. Note that this is optional condition, and does not
58
60
  overwrite original param-based condition for a query block that should always be met.
59
61
  - `:unless` - the same as `:if` option, but with reversed boolean check.
60
62
 
61
- - `query(&block)` declares scope-generation block that is always executed. As `query_by`,
62
- accepts `:index`, `:if` and `:unless` options.
63
+ - `query_by!(*fields, &block)` declares scope-generation block that is always executed
64
+ (unless `:if` and/or `:unless` options are used). All values in params at `fields` keys are
65
+ yielded to the block. As `query_by`, accepts `:index`, `:if` and `:unless` options.
66
+
67
+ - `query(&block)` declares scope-generation block that is always executed (unless `:if`
68
+ and/or `:unless` options are used). As `query_by`, accepts `:index`, `:if` and `:unless`
69
+ options.
63
70
 
64
71
  *Examples:*
65
72
 
@@ -90,8 +97,12 @@ end
90
97
  - `sift_by(*presence_fields, **value_fields, &block)` method is used to hoist sets of
91
98
  query definitions that should be applied if, and only if, all specified values
92
99
  match criteria in the same way as in `query_by` method. Just like `query_by` method,
93
- values of specified fields are yielded to the block. Such `sift_by` definitions
94
- may be nested in any depth.
100
+ values of specified fields are yielded to the block. Accepts the same options as
101
+ it's `query_by` counterpart. Such `sift_by` definitions may be nested in any depth.
102
+
103
+ - `sift_by!(*fields, &block)` declares a sifter block that is always applied (unless
104
+ `:if` and/or `:unless` options are used). All values in params at specified `fields`
105
+ are yielded to the block.
95
106
 
96
107
  - `sifter` alias for `sift_by`. Results in a more readable construct when a single
97
108
  presence field is passed. For example, `sifter(:paginated)`.
@@ -3,17 +3,18 @@ module Parascope
3
3
  OPTION_KEYS = %i[index if unless].freeze
4
4
  private_constant :OPTION_KEYS
5
5
 
6
- attr_reader :presence_fields, :value_fields, :block, :options
6
+ attr_reader :presence_fields, :value_fields, :block, :force, :options
7
7
 
8
- def initialize(presence_fields:, value_fields:, block:)
8
+ def initialize(presence_fields:, value_fields:, block:, force: false)
9
9
  @options = extract_options!(value_fields)
10
10
 
11
- @presence_fields, @value_fields, @block =
12
- presence_fields, value_fields, block
11
+ @presence_fields, @value_fields, @block, @force =
12
+ presence_fields, value_fields, block, force
13
13
  end
14
14
 
15
15
  def fits?(query)
16
16
  return false unless conditions_met_by?(query)
17
+ return true if force
17
18
 
18
19
  (presence_fields.size == 0 && value_fields.size == 0) ||
19
20
  values_for(query.params).all?{ |value| present?(value) }
@@ -28,7 +29,12 @@ module Parascope
28
29
  end
29
30
 
30
31
  def index
31
- options[:index] || 0
32
+ case options[:index]
33
+ when :first then -Float::INFINITY
34
+ when :last then Float::INFINITY
35
+ when Numeric then options[:index]
36
+ else 0
37
+ end
32
38
  end
33
39
 
34
40
  private
@@ -33,6 +33,27 @@ module Parascope
33
33
  alias_method :sifter, :sift_by
34
34
  alias_method :query, :query_by
35
35
 
36
+ def sift_by!(*presence_fields, &block)
37
+ sift_blocks.push Query::ApiBlock.new(
38
+ presence_fields: presence_fields,
39
+ value_fields: {},
40
+ block: block,
41
+ force: true
42
+ )
43
+ end
44
+
45
+ def query_by!(*presence_fields, &block)
46
+ query_blocks.push Query::ApiBlock.new(
47
+ presence_fields: presence_fields,
48
+ value_fields: {},
49
+ block: block,
50
+ force: true
51
+ )
52
+ end
53
+
54
+ alias_method :sifter!, :sift_by!
55
+ alias_method :query!, :query_by!
56
+
36
57
  def guard(&block)
37
58
  guard_blocks.push block
38
59
  end
@@ -1,3 +1,3 @@
1
1
  module Parascope
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.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.0.3
4
+ version: 1.1.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: 2017-07-09 00:00:00.000000000 Z
11
+ date: 2017-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  version: '0'
141
141
  requirements: []
142
142
  rubyforge_project:
143
- rubygems_version: 2.4.8
143
+ rubygems_version: 2.6.13
144
144
  signing_key:
145
145
  specification_version: 4
146
146
  summary: Builds a params-sifted scope