cohort_scope 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/cohort_scope.rb +7 -7
- data/test/test_cohort_scope.rb +18 -5
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/cohort_scope.rb
CHANGED
@@ -36,9 +36,9 @@ module CohortScope
|
|
36
36
|
|
37
37
|
# Find the biggest scope possible by removing constraints <b>in any order</b>.
|
38
38
|
# Returns an empty scope if it can't meet the minimum scope size.
|
39
|
-
def big_cohort(constraints)
|
39
|
+
def big_cohort(constraints = {}, custom_minimum_cohort_size = nil)
|
40
40
|
raise ArgumentError, "You can't give a big_cohort an OrderedHash; do you want strict_cohort?" if constraints.is_a?(ActiveSupport::OrderedHash)
|
41
|
-
_cohort_massive_scope constraints
|
41
|
+
_cohort_massive_scope constraints, custom_minimum_cohort_size
|
42
42
|
end
|
43
43
|
|
44
44
|
# Find the first acceptable scope by removing constraints <b>in strict order</b>, starting with the last constraint.
|
@@ -57,15 +57,15 @@ module CohortScope
|
|
57
57
|
#
|
58
58
|
# If the original constraints don't meet the minimum scope size, then the only constraint that can be removed is birthdate.
|
59
59
|
# In other words, this would never return a scope that was constrained on birthdate but not on favorite_color.
|
60
|
-
def strict_cohort(constraints)
|
60
|
+
def strict_cohort(constraints, custom_minimum_cohort_size = nil)
|
61
61
|
raise ArgumentError, "You need to give strict_cohort an OrderedHash" unless constraints.is_a?(ActiveSupport::OrderedHash)
|
62
|
-
_cohort_massive_scope constraints
|
62
|
+
_cohort_massive_scope constraints, custom_minimum_cohort_size
|
63
63
|
end
|
64
64
|
|
65
65
|
protected
|
66
66
|
|
67
67
|
# Recursively look for a scope that meets the constraints and is at least <tt>minimum_cohort_size</tt>.
|
68
|
-
def _cohort_massive_scope(constraints)
|
68
|
+
def _cohort_massive_scope(constraints, custom_minimum_cohort_size)
|
69
69
|
raise RuntimeError, "You need to set #{name}.minimum_cohort_size = X" unless minimum_cohort_size.present?
|
70
70
|
|
71
71
|
if constraints.values.none? # failing base case
|
@@ -75,10 +75,10 @@ module CohortScope
|
|
75
75
|
this_hash = _cohort_constraints constraints
|
76
76
|
this_count = scoped(this_hash).count
|
77
77
|
|
78
|
-
if this_count >= minimum_cohort_size # successful base case
|
78
|
+
if this_count >= (custom_minimum_cohort_size || minimum_cohort_size) # successful base case
|
79
79
|
massive_scoped this_hash
|
80
80
|
else
|
81
|
-
_cohort_massive_scope _cohort_reduce_constraints(constraints)
|
81
|
+
_cohort_massive_scope _cohort_reduce_constraints(constraints), custom_minimum_cohort_size
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
data/test/test_cohort_scope.rb
CHANGED
@@ -9,10 +9,10 @@ class TestCohortScope < Test::Unit::TestCase
|
|
9
9
|
should "raise if no minimum_cohort_size is specified" do
|
10
10
|
Citizen.minimum_cohort_size = nil
|
11
11
|
assert_raises(RuntimeError) {
|
12
|
-
|
12
|
+
Citizen.big_cohort Hash.new
|
13
13
|
}
|
14
14
|
assert_raises(RuntimeError) {
|
15
|
-
|
15
|
+
Citizen.strict_cohort ActiveSupport::OrderedHash.new
|
16
16
|
}
|
17
17
|
end
|
18
18
|
|
@@ -21,6 +21,11 @@ class TestCohortScope < Test::Unit::TestCase
|
|
21
21
|
cohort = Citizen.big_cohort :favorite_color => 'heliotrope'
|
22
22
|
assert_equal 0, cohort.count
|
23
23
|
end
|
24
|
+
|
25
|
+
should "take minimum_cohort_size as an optional argument" do
|
26
|
+
cohort = Citizen.big_cohort({:favorite_color => 'heliotrope'}, 0)
|
27
|
+
assert_equal 1, cohort.count
|
28
|
+
end
|
24
29
|
|
25
30
|
should "seek a cohort of maximum size" do
|
26
31
|
cohort = Citizen.big_cohort :birthdate => @date_range, :favorite_color => 'heliotrope'
|
@@ -31,7 +36,7 @@ class TestCohortScope < Test::Unit::TestCase
|
|
31
36
|
|
32
37
|
should "raise if an OrderedHash is given to big_cohort" do
|
33
38
|
assert_raises(ArgumentError) {
|
34
|
-
|
39
|
+
Citizen.big_cohort ActiveSupport::OrderedHash.new
|
35
40
|
}
|
36
41
|
end
|
37
42
|
end
|
@@ -39,11 +44,19 @@ class TestCohortScope < Test::Unit::TestCase
|
|
39
44
|
context "strict_cohort" do
|
40
45
|
should "raise if a non-OrderedHash is given to strict_cohort" do
|
41
46
|
assert_raises(ArgumentError) {
|
42
|
-
|
47
|
+
Citizen.strict_cohort Hash.new
|
43
48
|
}
|
44
49
|
end
|
50
|
+
|
51
|
+
should "take minimum_cohort_size as an optional argument" do
|
52
|
+
ordered_attributes = ActiveSupport::OrderedHash.new
|
53
|
+
ordered_attributes[:favorite_color] = 'heliotrope'
|
54
|
+
|
55
|
+
cohort = Citizen.strict_cohort ordered_attributes, 0
|
56
|
+
assert_equal 1, cohort.count
|
57
|
+
end
|
45
58
|
|
46
|
-
should "return an empty
|
59
|
+
should "return an empty cohort if it can't find one that meets size requirements" do
|
47
60
|
ordered_attributes = ActiveSupport::OrderedHash.new
|
48
61
|
ordered_attributes[:favorite_color] = 'heliotrope'
|
49
62
|
|