Module: LimitDetectors

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

Constant Summary collapse

VERSION =
'1.0.4'

Instance Method Summary collapse

Instance Method Details

#at_least(limit, &block) ⇒ Object

Deprecated, use at_least? instead



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

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

#at_least?(limit, &block) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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

#at_most(limit, &block) ⇒ Object

Deprecated, use at_most? instead



7
8
9
10
# File 'lib/limit_detectors.rb', line 7

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

#at_most?(limit, &block) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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

#occurrences_ofObject

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



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

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