qo 0.1.0 → 0.1.1
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/.gitignore +3 -0
- data/lib/qo/matcher.rb +81 -19
- data/lib/qo/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77e3ec860cc023f34436da2fc2177007574673bb
|
4
|
+
data.tar.gz: f7e09d7967b02518f8da0a7176ad655bc35a2395
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64b6464ddbf2eb4d00ece478673760d9ee33e0d7a6bb5777478756946a6943ea273a9fdb811143e4e960628239eaf381af37c28d5d314d5af90ba64f11fc671a
|
7
|
+
data.tar.gz: 9dc57edb473939227ed4863cbcd8a524e3afc3097186331ced7662792b235ba09df45e6e0322083b2d5131b164a01f47a74ebb4a5c8fae473800ea958a0ecb24
|
data/.gitignore
CHANGED
data/lib/qo/matcher.rb
CHANGED
@@ -6,35 +6,69 @@ module Qo
|
|
6
6
|
@keyword_matchers = keyword_matchers
|
7
7
|
end
|
8
8
|
|
9
|
+
# Converts a Matcher to a proc for use in querying, such as:
|
10
|
+
#
|
11
|
+
# data.select(&Qo[...])
|
12
|
+
#
|
13
|
+
# @return [Proc]
|
9
14
|
def to_proc
|
10
15
|
@array_matchers.empty? ?
|
11
16
|
match_against_hash(@keyword_matchers) :
|
12
17
|
match_against_array(@array_matchers)
|
13
18
|
end
|
14
19
|
|
15
|
-
|
16
|
-
|
20
|
+
# You can directly call a matcher as well, much like a Proc,
|
21
|
+
# using one of call, ===, or []
|
22
|
+
#
|
23
|
+
# @param match_target [Any] Object to match against
|
24
|
+
#
|
25
|
+
# @return [type] [description]
|
26
|
+
def call(match_target)
|
27
|
+
self.to_proc.call(match_target)
|
17
28
|
end
|
18
29
|
|
19
30
|
alias_method :===, :call
|
20
31
|
alias_method :[], :call
|
21
32
|
|
33
|
+
# Used to match against a matcher made from an Array, like:
|
34
|
+
#
|
35
|
+
# Qo['Foo', 'Bar']
|
36
|
+
#
|
37
|
+
# @param matchers [Array[respond_to?(===)]] indexed tuple to match the target object against
|
38
|
+
#
|
39
|
+
# @return [Proc[Any]]
|
40
|
+
# Array -> Bool # Tuple match against targets index
|
41
|
+
# Object -> Bool # Boolean public send
|
22
42
|
private def match_against_array(matchers)
|
23
|
-
->
|
24
|
-
|
25
|
-
matchers.each_with_index.public_send(@match_method, &array_matches_array_fn(
|
26
|
-
matchers.public_send(@match_method, &array_matches_object_fn(
|
43
|
+
-> match_target {
|
44
|
+
match_target.is_a?(::Array) ?
|
45
|
+
matchers.each_with_index.public_send(@match_method, &array_matches_array_fn(match_target)) :
|
46
|
+
matchers.public_send(@match_method, &array_matches_object_fn(match_target))
|
27
47
|
}
|
28
48
|
end
|
29
49
|
|
50
|
+
# Used to match against a matcher made from Keyword Arguments (a Hash)
|
51
|
+
#
|
52
|
+
# @param matchers [Hash[Any, respond_to?(===)]]
|
53
|
+
# Any key mapping to any value that responds to `===`. Notedly more
|
54
|
+
# satisfying when `===` does something fun.
|
55
|
+
#
|
56
|
+
# @return [Proc[Any]]
|
57
|
+
# Hash -> Bool # Value matching against similar keys, will attempt to coerce to_s because JSON
|
58
|
+
# Object -> Bool # Uses keys as methods with public send to `===` match against the value
|
30
59
|
private def match_against_hash(matchers)
|
31
|
-
->
|
32
|
-
|
33
|
-
matchers.public_send(@match_method, &hash_matches_hash_fn(
|
34
|
-
matchers.public_send(@match_method, &hash_matches_object_fn(
|
60
|
+
-> match_target {
|
61
|
+
match_target.is_a?(::Hash) ?
|
62
|
+
matchers.public_send(@match_method, &hash_matches_hash_fn(match_target)) :
|
63
|
+
matchers.public_send(@match_method, &hash_matches_object_fn(match_target))
|
35
64
|
}
|
36
65
|
end
|
37
66
|
|
67
|
+
# Used to map the nicety names against the actual Ruby methods they represent
|
68
|
+
#
|
69
|
+
# @param name [String] Query type name
|
70
|
+
#
|
71
|
+
# @return [Symbol] The method name to use to query with
|
38
72
|
private def get_match_method(name)
|
39
73
|
case name
|
40
74
|
when 'and' then :all?
|
@@ -44,29 +78,57 @@ module Qo
|
|
44
78
|
end
|
45
79
|
end
|
46
80
|
|
47
|
-
|
81
|
+
# A function to match against an indexed array tuple
|
82
|
+
#
|
83
|
+
# @param match_target [Array] Target array
|
84
|
+
#
|
85
|
+
# @return [Proc]
|
86
|
+
# Any -> Int -> Bool # Match against wildcard or same position in target array
|
87
|
+
private def array_matches_array_fn(match_target)
|
48
88
|
-> matcher, i {
|
49
|
-
matcher == WILDCARD_MATCH || matcher ===
|
89
|
+
matcher == WILDCARD_MATCH || matcher === match_target[i]
|
50
90
|
}
|
51
91
|
end
|
52
92
|
|
53
|
-
|
93
|
+
# A function to match against an object using an array of predicates
|
94
|
+
#
|
95
|
+
# @param match_target [Any] Target object
|
96
|
+
#
|
97
|
+
# @return [Proc]
|
98
|
+
# String | Symbol -> Bool # Match against wildcard or boolean return of a predicate method
|
99
|
+
private def array_matches_object_fn(match_target)
|
54
100
|
-> matcher {
|
55
|
-
matcher == WILDCARD_MATCH ||
|
101
|
+
matcher == WILDCARD_MATCH || match_target.public_send(matcher)
|
56
102
|
}
|
57
103
|
end
|
58
104
|
|
59
|
-
|
105
|
+
# A function to match against a Hash using a Hash
|
106
|
+
#
|
107
|
+
# @param match_target [Hash[Any, Any]] Target Hash
|
108
|
+
#
|
109
|
+
# @return [Proc]
|
110
|
+
# Any -> Any -> Bool # Matches against wildcard or a key and value. Coerces key to_s if no matches for JSON.
|
111
|
+
private def hash_matches_hash_fn(match_target)
|
60
112
|
-> match_key, match_value {
|
61
113
|
match_value == WILDCARD_MATCH ||
|
62
|
-
match_value ===
|
63
|
-
|
114
|
+
match_value === match_target[match_key] || (
|
115
|
+
# This is done for JSON responses, but as key can be `Any` we don't want to assume it knows how
|
116
|
+
# to coerce `to_s` either. It's more of a nicety function.
|
117
|
+
match_key.respond_to?(:to_s) &&
|
118
|
+
match_value === match_target[match_key.to_s]
|
119
|
+
)
|
64
120
|
}
|
65
121
|
end
|
66
122
|
|
67
|
-
|
123
|
+
# A function to match against an Object using a Hash
|
124
|
+
#
|
125
|
+
# @param match_target [Any] Target object
|
126
|
+
#
|
127
|
+
# @return [Proc]
|
128
|
+
# Any -> Any -> Bool # Matches against wildcard or match value versus the public send return of the target
|
129
|
+
private def hash_matches_object_fn(match_target)
|
68
130
|
-> match_key, match_value {
|
69
|
-
match_value == WILDCARD_MATCH || match_value ===
|
131
|
+
match_value == WILDCARD_MATCH || match_value === match_target.public_send(match_key)
|
70
132
|
}
|
71
133
|
end
|
72
134
|
end
|
data/lib/qo/version.rb
CHANGED