might 0.5.2 → 0.6.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: 16724ee40371d468bd5a932a35a100ae39bff05b
4
- data.tar.gz: 1211d23fcfb3043dc2da35000d0c500da5b5c2bf
3
+ metadata.gz: 15a3ffb400a36a96c6479bd3717c94622b064437
4
+ data.tar.gz: fb6e300fedf6060ec38e7d2dab644a9faf8673cb
5
5
  SHA512:
6
- metadata.gz: 1f218d738eab33a02c2b62072fe1cb23cce557ccca6752386892877d41c33f96cabcd35b50161d6d6894b12ae8cd950ea7277a4e362b35ddf8a90f1b8e38684a
7
- data.tar.gz: eae9d9eb274fc4a539db1457e2f990adcedc73c1d4b2c8aa649c5345bb2e266653a17bb7e2cb91baae3b0406f52356e0ded177d44baa069dc793aec3ad0d529e
6
+ metadata.gz: b2325e15a768c3726bb4a7d06ccdc7550999eb769fa9bc0f575828b88f2625826cacba54f76c97c74aadb7176a7628d1cfcf66f9899a6eb0c6019b603b4f8061
7
+ data.tar.gz: bcf3c17a88edeb04959dc1818e3951539b65c3bdf06b4fa1d85830bb4e2fe0a760299ab351ec6e9ea5e7300672832d283dcfa2491c5cd60f3a4a4f82803a674a
@@ -1,6 +1,10 @@
1
+ **0.6.0**
2
+
3
+ * Implement public API for registering predicates
4
+
1
5
  **0.5.0**
2
6
 
3
- * Introduce Might::FilterParameters as subclass of Setq
7
+ * Introduce `Might::FilterParameters` as subclass of Set
4
8
 
5
9
  **0.4.0**
6
10
 
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::ALL)).map(&:to_s)
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::ALL.detect { |p| name_with_predicate.end_with?("_#{p}") }
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::ON_ARRAY)
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
- ON_VALUE = [
33
- NOT_EQ, EQ,
34
- DOES_NOT_MATCH, MATCHES,
35
- GT, LT,
36
- GTEQ, LTEQ,
37
- NOT_CONT, CONT,
38
- NOT_START, START,
39
- DOES_NOT_END, ENDS,
40
- NOT_TRUE, TRUE,
41
- NOT_FALSE, FALSE,
42
- BLANK, PRESENT,
43
- NOT_NULL, NULL
44
- ].freeze
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
- ON_ARRAY = [
47
- NOT_IN, IN,
48
- NOT_CONT_ANY, CONT_ANY
49
- ].freeze
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
- ALL = ON_VALUE + ON_ARRAY
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
@@ -1,4 +1,4 @@
1
1
  #
2
2
  module Might
3
- VERSION = '0.5.2'.freeze
3
+ VERSION = '0.6.0'.freeze
4
4
  end
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.5.2
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-16 00:00:00.000000000 Z
11
+ date: 2016-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport