Module: LimitDetectors

Defined in:
lib/limit_detectors.rb,
lib/limit_detectors/version.rb

Overview

LimitDetectors provides methods (that depend on ‘Enumerable` being present) to detect if an `Enumerable` object complies to the specified limitation. For example, there’s a method #at_most that returns ‘true`if the given enumerable object contains _at most_ the given number of elements that match the given constrains (provided as a proc/lambda).

Constant Summary collapse

VERSION =
'1.0.9'

Instance Method Summary collapse

Instance Method Details

#at_least(limit) ⇒ Object

Deprecated, use at_least? instead



19
20
21
22
# File 'lib/limit_detectors.rb', line 19

def at_least(limit, &)
  Kernel.warn "'at_least' is deprecated, use 'at_least?' instead"
  at_least? limit, &
end

#at_least?(limit) ⇒ Boolean

Check whether the condition given in the block occurs at least limit times in the collection

Returns:

  • (Boolean)


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

def at_least?(limit, &)
  occurrences_of(&) >= limit
end

#at_most(limit) ⇒ Object

Deprecated, use at_most? instead



13
14
15
16
# File 'lib/limit_detectors.rb', line 13

def at_most(limit, &)
  Kernel.warn "'at_most' is deprecated, use 'at_most?' instead"
  at_most? limit, &
end

#at_most?(limit) ⇒ Boolean

Check whether the condition given in the block occurs at most limit times in the collection

Returns:

  • (Boolean)


26
27
28
# File 'lib/limit_detectors.rb', line 26

def at_most?(limit, &)
  occurrences_of(&) <= limit
end

#occurrences_ofObject

Count how often the condition given in the block is met for the collection



38
39
40
41
42
43
# File 'lib/limit_detectors.rb', line 38

def occurrences_of
  inject(0) do |res, el|
    res += 1 if yield el
    res
  end
end