Module: Speculation::Predicates
- Defined in:
- lib/speculation/predicates.rb
Overview
Collection of predicate methods used within Speculation. These may appear as the value for the ‘:pred` key in the return value of `Speculation.explain_data`.
Class Method Summary collapse
- .array?(x) ⇒ Boolean
- .collection?(xs) ⇒ Boolean
- .count_between?(min_count, max_count, coll) ⇒ Boolean
- .count_eq?(count, coll) ⇒ Boolean
- .distinct?(xs) ⇒ Boolean
- .empty?(coll) ⇒ Boolean
- .hash?(x) ⇒ Boolean
- .key?(hash, key) ⇒ Boolean
Class Method Details
.array?(x) ⇒ Boolean
10 11 12 |
# File 'lib/speculation/predicates.rb', line 10 def self.array?(x) x.respond_to?(:at) && x.respond_to?(:[]) end |
.collection?(xs) ⇒ Boolean
14 15 16 |
# File 'lib/speculation/predicates.rb', line 14 def self.collection?(xs) xs.respond_to?(:each) end |
.count_between?(min_count, max_count, coll) ⇒ Boolean
36 37 38 |
# File 'lib/speculation/predicates.rb', line 36 def self.count_between?(min_count, max_count, coll) coll.count.between?(min_count, max_count) end |
.count_eq?(count, coll) ⇒ Boolean
32 33 34 |
# File 'lib/speculation/predicates.rb', line 32 def self.count_eq?(count, coll) coll.count == count end |
.distinct?(xs) ⇒ Boolean
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/speculation/predicates.rb', line 18 def self.distinct?(xs) seen = Set[] xs.each do |x| if seen.include?(x) return false else seen << x end end true end |
.empty?(coll) ⇒ Boolean
44 45 46 |
# File 'lib/speculation/predicates.rb', line 44 def self.empty?(coll) coll.empty? end |
.hash?(x) ⇒ Boolean
6 7 8 |
# File 'lib/speculation/predicates.rb', line 6 def self.hash?(x) x.respond_to?(:store) && x.respond_to?(:key?) && x.respond_to?(:[]) end |
.key?(hash, key) ⇒ Boolean
40 41 42 |
# File 'lib/speculation/predicates.rb', line 40 def self.key?(hash, key) hash.key?(key) end |