might 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +12 -0
- data/lib/might/filter_parameter_definition.rb +1 -1
- data/lib/might/filter_parameters_extractor.rb +2 -2
- data/lib/might/filter_predicates.rb +61 -18
- data/lib/might/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15a3ffb400a36a96c6479bd3717c94622b064437
|
4
|
+
data.tar.gz: fb6e300fedf6060ec38e7d2dab644a9faf8673cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2325e15a768c3726bb4a7d06ccdc7550999eb769fa9bc0f575828b88f2625826cacba54f76c97c74aadb7176a7628d1cfcf66f9899a6eb0c6019b603b4f8061
|
7
|
+
data.tar.gz: bcf3c17a88edeb04959dc1818e3951539b65c3bdf06b4fa1d85830bb4e2fe0a760299ab351ec6e9ea5e7300672832d283dcfa2491c5cd60f3a4a4f82803a674a
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -219,6 +219,18 @@ Predicate | Opposite Predicate | Meaning
|
|
219
219
|
`present` | `blank` | field is present (not null and not a blank string)
|
220
220
|
`null` | `not_null` | field is null
|
221
221
|
|
222
|
+
You may want to implement custom predicate. First of all you should implement predicate for [Ransack gem](https://github.com/activerecord-hackery/ransack/).
|
223
|
+
This process [documented here](https://github.com/activerecord-hackery/ransack/wiki/custom-predicates).
|
224
|
+
Then you have to register predicate.
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
# Predicate for a singular value (such as a string)
|
228
|
+
Might::FilterPredicates.register('is_upper_case', on: :value)
|
229
|
+
|
230
|
+
# Predicate for an array
|
231
|
+
Might::FilterPredicates.register('includes', on: :array)
|
232
|
+
```
|
233
|
+
|
222
234
|
|
223
235
|
### Configuring sorting
|
224
236
|
|
@@ -35,7 +35,7 @@ module Might
|
|
35
35
|
@as = options.fetch(:as, name).to_s
|
36
36
|
@name = name.to_s
|
37
37
|
@on = options[:on]
|
38
|
-
@predicates = Array(options.fetch(:predicates, FilterPredicates
|
38
|
+
@predicates = Array(options.fetch(:predicates, FilterPredicates.all)).map(&:to_s)
|
39
39
|
@coerce = options.fetch(:coerce, ->(v) { v })
|
40
40
|
@validations = options.fetch(:validates, {})
|
41
41
|
end
|
@@ -52,7 +52,7 @@ module Might
|
|
52
52
|
attr_reader :parameters_definition, :app
|
53
53
|
|
54
54
|
def name_and_predicate(name_with_predicate)
|
55
|
-
predicate = FilterPredicates
|
55
|
+
predicate = FilterPredicates.all.detect { |p| name_with_predicate.end_with?("_#{p}") }
|
56
56
|
[
|
57
57
|
name_with_predicate.chomp("_#{predicate}"),
|
58
58
|
predicate
|
@@ -69,7 +69,7 @@ module Might
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def type_cast_value(name, value)
|
72
|
-
if name.end_with?(*FilterPredicates
|
72
|
+
if name.end_with?(*FilterPredicates.array)
|
73
73
|
value.split(',')
|
74
74
|
else
|
75
75
|
value
|
@@ -1,5 +1,9 @@
|
|
1
1
|
module Might
|
2
2
|
# Contains contains with all supported predicates
|
3
|
+
# You can register your own predicates. Predicates should perform on array or on singular value:
|
4
|
+
#
|
5
|
+
# Might::FilterPredicates.register('includes', on: :array)
|
6
|
+
# Might::FilterPredicates.register('is_upper_case', on: :value)
|
3
7
|
#
|
4
8
|
module FilterPredicates
|
5
9
|
NOT_EQ = 'not_eq'.freeze
|
@@ -29,25 +33,64 @@ module Might
|
|
29
33
|
NOT_CONT_ANY = 'not_cont_any'.freeze
|
30
34
|
CONT_ANY = 'cont_any'.freeze
|
31
35
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
36
|
+
@predicates_on_value = Set.new([
|
37
|
+
NOT_EQ, EQ,
|
38
|
+
DOES_NOT_MATCH, MATCHES,
|
39
|
+
GT, LT,
|
40
|
+
GTEQ, LTEQ,
|
41
|
+
NOT_CONT, CONT,
|
42
|
+
NOT_START, START,
|
43
|
+
DOES_NOT_END, ENDS,
|
44
|
+
NOT_TRUE, TRUE,
|
45
|
+
NOT_FALSE, FALSE,
|
46
|
+
BLANK, PRESENT,
|
47
|
+
NOT_NULL, NULL
|
48
|
+
])
|
49
|
+
@predicates_on_array = Set.new([
|
50
|
+
NOT_IN, IN,
|
51
|
+
NOT_CONT_ANY, CONT_ANY
|
52
|
+
])
|
53
|
+
|
54
|
+
# Registers predicate on singular value or on array
|
55
|
+
# @param predicate [String, Symbol]
|
56
|
+
# @param on [:array, :value]
|
57
|
+
# @return [Might::FilterPredicates]
|
58
|
+
#
|
59
|
+
def register(predicate, on:)
|
60
|
+
case on
|
61
|
+
when :value
|
62
|
+
@predicates_on_value.add(predicate.to_s)
|
63
|
+
when :array
|
64
|
+
@predicates_on_array.add(predicate.to_s)
|
65
|
+
else
|
66
|
+
fail ArgumentError, 'on must be :array, or :value'
|
67
|
+
end
|
68
|
+
self
|
69
|
+
end
|
70
|
+
module_function :register
|
71
|
+
|
72
|
+
# Returns predicates for array
|
73
|
+
# @return [Set<String>]
|
74
|
+
#
|
75
|
+
def array
|
76
|
+
@predicates_on_array.dup
|
77
|
+
end
|
78
|
+
module_function :array
|
45
79
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
80
|
+
# Returns predicates for values
|
81
|
+
# @return [Set<String>]
|
82
|
+
#
|
83
|
+
def value
|
84
|
+
@predicates_on_value.dup
|
85
|
+
end
|
86
|
+
module_function :value
|
50
87
|
|
51
|
-
|
88
|
+
# Returns all predicates
|
89
|
+
# @return [Set<String>]
|
90
|
+
#
|
91
|
+
def all
|
92
|
+
@predicates_on_value + @predicates_on_array
|
93
|
+
end
|
94
|
+
module_function :all
|
52
95
|
end
|
53
96
|
end
|
data/lib/might/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: might
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tema Bolshakov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|