qo 0.1.0 → 0.1.1

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: e9388601df794f4debc0449dad3374eb90247f92
4
- data.tar.gz: 3ecd58e702bad3521796dbc945fbd9900997a2c1
3
+ metadata.gz: 77e3ec860cc023f34436da2fc2177007574673bb
4
+ data.tar.gz: f7e09d7967b02518f8da0a7176ad655bc35a2395
5
5
  SHA512:
6
- metadata.gz: 95e58e25e8ff762010714537a6be43a545ac2d98887a669d4cf5ee665996a0d08ff982d57894a9e3f1d9dda05bec4046a6e16b949d5a8d642f9db574e14c205e
7
- data.tar.gz: f74cebc9d05612b384080aea19e84e1d3713ed2162734dcb6b875078d57d0058e16a280e5fa35d4a9f1e4ba67438cde13093eae5064d03c9da45e39d92c56b72
6
+ metadata.gz: 64b6464ddbf2eb4d00ece478673760d9ee33e0d7a6bb5777478756946a6943ea273a9fdb811143e4e960628239eaf381af37c28d5d314d5af90ba64f11fc671a
7
+ data.tar.gz: 9dc57edb473939227ed4863cbcd8a524e3afc3097186331ced7662792b235ba09df45e6e0322083b2d5131b164a01f47a74ebb4a5c8fae473800ea958a0ecb24
data/.gitignore CHANGED
@@ -10,3 +10,6 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+
14
+ # built gems
15
+ *.gem
@@ -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
- def call(other)
16
- self.to_proc.call(other)
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
- -> other_object {
24
- other_object.is_a?(::Array) ?
25
- matchers.each_with_index.public_send(@match_method, &array_matches_array_fn(other_object)) :
26
- matchers.public_send(@match_method, &array_matches_object_fn(other_object))
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
- -> other_object {
32
- other_object.is_a?(::Hash) ?
33
- matchers.public_send(@match_method, &hash_matches_hash_fn(other_object)) :
34
- matchers.public_send(@match_method, &hash_matches_object_fn(other_object))
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
- private def array_matches_array_fn(other_object)
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 === other_object[i]
89
+ matcher == WILDCARD_MATCH || matcher === match_target[i]
50
90
  }
51
91
  end
52
92
 
53
- private def array_matches_object_fn(other_object)
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 || other_object.public_send(matcher)
101
+ matcher == WILDCARD_MATCH || match_target.public_send(matcher)
56
102
  }
57
103
  end
58
104
 
59
- private def hash_matches_hash_fn(other_object)
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 === other_object[match_key] ||
63
- match_value === other_object[match_key.to_s]
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
- private def hash_matches_object_fn(other_object)
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 === other_object.public_send(match_key)
131
+ match_value == WILDCARD_MATCH || match_value === match_target.public_send(match_key)
70
132
  }
71
133
  end
72
134
  end
@@ -1,3 +1,3 @@
1
1
  module Qo
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Weaver