Module: Speculation::Predicates

Defined in:
lib/speculation/predicates.rb

Overview

Collection of predicate methods used within Speculation. These may appear as the value `:pred for the pred key in the return value of `Speculation.explain_data`.

Class Method Summary collapse

Class Method Details

.array?(x) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)

32
33
34
# File 'lib/speculation/predicates.rb', line 32

def self.count_eq?(count, coll)
  coll.count == count
end

.distinct?(xs) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)

44
45
46
# File 'lib/speculation/predicates.rb', line 44

def self.empty?(coll)
  coll.empty?
end

.hash?(x) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)

40
41
42
# File 'lib/speculation/predicates.rb', line 40

def self.key?(hash, key)
  hash.key?(key)
end