limit_detectors 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/limit_detectors.rb +17 -3
- data/lib/limit_detectors/version.rb +1 -1
- data/spec/limit_detectors_spec.rb +31 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19b534365562333e42bee1656992c44a58935616
|
4
|
+
data.tar.gz: 4e4eedd3a948fdbdd494d8b9ad4be99741822b84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 551125436affcdaf9720eadeec53ee228b9d217ab298e6553bd6c498f8aa5b642179c50c32ac26a3b1c67cebf1feeeed26acf7ae3ac0f74a95862d1aaa877e10
|
7
|
+
data.tar.gz: 21eed54f9b88dcfec2b964b17c8afe80c086e37e4a41c7a36f2cb0d10cbce57f377460a5c4281736f718779b6c0022266aec27004625fbea8c527db3eea44a4c
|
data/README.md
CHANGED
@@ -4,8 +4,10 @@ Some methods to detect whether an Enumberable object contains a constrained numb
|
|
4
4
|
|
5
5
|
## Stati
|
6
6
|
|
7
|
+
* Version: [![Gem Version](https://badge.fury.io/rb/limit_detectors.svg)](http://badge.fury.io/rb/limit_detectors)
|
7
8
|
* Travis CI: [![Build Status](https://travis-ci.org/s2k/limit_detectors.svg?branch=master)](https://travis-ci.org/s2k/limit_detectors)
|
8
9
|
* Maintenance: [![Project Status](http://stillmaintained.com/s2k/limit_detectors.png)](http://stillmaintained.com/s2k/limit_detectors)
|
10
|
+
* Code Climate: [![Code Climate](https://codeclimate.com/github/s2k/limit_detectors.png)](https://codeclimate.com/github/s2k/limit_detectors)
|
9
11
|
|
10
12
|
## Installation
|
11
13
|
|
data/lib/limit_detectors.rb
CHANGED
@@ -1,12 +1,26 @@
|
|
1
1
|
require 'limit_detectors/version'
|
2
2
|
|
3
3
|
module LimitDetectors
|
4
|
-
def at_most(limit)
|
5
4
|
|
6
|
-
|
5
|
+
# Check whether the condition given in the block
|
6
|
+
# occurs at most limit times in the collection
|
7
|
+
def at_most(limit, &block)
|
8
|
+
ocurrences_of(&block) <= limit
|
9
|
+
end
|
10
|
+
|
11
|
+
# Check whether the condition given in the block
|
12
|
+
# occurs at least limit times in the collection
|
13
|
+
def at_least(limit, &block)
|
14
|
+
ocurrences_of(&block) >= limit
|
15
|
+
end
|
16
|
+
|
17
|
+
# Count how often the condition given in the block
|
18
|
+
# is met for the collection
|
19
|
+
def ocurrences_of &block
|
20
|
+
inject(0) { |res, el|
|
7
21
|
res += 1 if yield el
|
8
22
|
res
|
9
23
|
}
|
10
|
-
count <= limit
|
11
24
|
end
|
25
|
+
|
12
26
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
Array.send :include, LimitDetectors
|
3
4
|
|
4
|
-
describe '
|
5
|
-
Array.send :include, LimitDetectors
|
5
|
+
describe '#at_most' do
|
6
6
|
|
7
7
|
it 'is true for an empty Array' do
|
8
8
|
expect([].at_most(5){ true }).to be_true
|
@@ -25,17 +25,40 @@ describe 'Array#at_most' do
|
|
25
25
|
expect(r.at_most(0){ |i| i > 2 }).to be_true
|
26
26
|
end
|
27
27
|
|
28
|
+
describe 'Hash#at_most' do
|
29
|
+
Hash.send :include, LimitDetectors
|
30
|
+
it 'detects a condition based on key as well as value properties' do
|
31
|
+
h = { 'foo' => 1, 'bar' => 4, 'baz' => 5, 'bum' => 1, 'fum' => 0}
|
32
|
+
expect( h.at_most(3){|ky,vl| ky.match(/^b/) || vl > 1 }).to be_true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
28
36
|
end
|
29
37
|
|
38
|
+
describe '#at_least' do
|
39
|
+
it 'is true for an empty Array' do
|
40
|
+
expect([].at_least(1){ true }).to be_false
|
41
|
+
end
|
30
42
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
43
|
+
it 'is true if the criterion is met once' do
|
44
|
+
expect(["it's there"].at_least(1){ |el| el == "it's there"}).to be_true
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'is true if all elements meet the criterion and the size is the given maximum number' do
|
48
|
+
expect([1,1,1].at_least(3){|e| e == 1})
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'is false if not enough elements meet the criterion' do
|
52
|
+
expect([1, 2, 4].at_least(1){|e| e.even?}).to be_true
|
36
53
|
end
|
37
|
-
end
|
38
54
|
|
55
|
+
it 'is true if 0 elements are expected to match' do
|
56
|
+
r = Array.new(10){|i|i}
|
57
|
+
expect(r.at_least(7){ |i| i > 2 }).to be_true
|
58
|
+
expect(r.at_least(8){ |i| i > 2 }).to be_false
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
39
62
|
|
40
63
|
describe 'Using an object that doesn\'t respond to #inject will raise an exception' do
|
41
64
|
object = Object.new
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: limit_detectors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephan Kämper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|