cohort_scope 0.2.2 → 0.2.3

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.
@@ -6,15 +6,17 @@ module CohortScope
6
6
  # Recursively look for a scope that meets the constraints and is at least <tt>minimum_cohort_size</tt>.
7
7
  def create(active_record, constraints, minimum_cohort_size)
8
8
  if constraints.none? # failing base case
9
- impossible_cohort = new(active_record.scoped.where(IMPOSSIBLE_CONDITION))
10
- impossible_cohort.impossible!
11
- return impossible_cohort
9
+ cohort = new active_record.scoped.where(IMPOSSIBLE_CONDITION)
10
+ cohort.count = 0
11
+ return cohort
12
12
  end
13
13
 
14
14
  constrained_scope = active_record.scoped.where CohortScope.conditions_for(constraints)
15
15
 
16
- if constrained_scope.count >= minimum_cohort_size
17
- new constrained_scope
16
+ if (count = constrained_scope.count) >= minimum_cohort_size
17
+ cohort = new constrained_scope
18
+ cohort.count = count
19
+ cohort
18
20
  else
19
21
  create active_record, reduce_constraints(active_record, constraints), minimum_cohort_size
20
22
  end
@@ -34,6 +36,14 @@ module CohortScope
34
36
  @_ch_obj = obj
35
37
  end
36
38
 
39
+ def count=(int)
40
+ @count = int
41
+ end
42
+
43
+ def count
44
+ @count ||= super
45
+ end
46
+
37
47
  # sabshere 2/1/11 overriding as_json per usual doesn't seem to work
38
48
  def to_json(*)
39
49
  as_json.to_json
@@ -43,23 +53,21 @@ module CohortScope
43
53
  { :members => count }
44
54
  end
45
55
 
46
- def impossible!
47
- @impossible = true
56
+ def empty?
57
+ count == 0
48
58
  end
49
-
50
- def impossible?
51
- @impossible == true
52
- end
53
-
59
+
54
60
  def any?
55
- return false if impossible?
61
+ return false if count == 0
62
+ return true if !block_given? and count > 0
56
63
  super
57
64
  end
58
65
 
59
- # sabshere 2/1/11 ActiveRecord does this for #any? but not for #none?
60
66
  def none?(&blk)
61
- return true if impossible?
67
+ return true if count == 0
68
+ return false if !block_given? and count > 0
62
69
  if block_given?
70
+ # sabshere 2/1/11 ActiveRecord does this for #any? but not for #none?
63
71
  to_a.none? &blk
64
72
  else
65
73
  super
@@ -1,3 +1,3 @@
1
1
  module CohortScope
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -101,6 +101,20 @@ class TestCohortScope < Test::Unit::TestCase
101
101
  assert_equal({ :members => 0 }, relation.to_cohort.as_json)
102
102
  end
103
103
 
104
+ def test_013_count_is_forced
105
+ cohort = Citizen.big_cohort(:birthdate => @date_range)
106
+
107
+ cohort.count = 0
108
+ assert !cohort.any?
109
+ assert cohort.none?
110
+ assert cohort.empty?
111
+
112
+ cohort.count = 1
113
+ assert cohort.any?
114
+ assert !cohort.none?
115
+ assert !cohort.empty?
116
+ end
117
+
104
118
  private
105
119
 
106
120
  def style
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cohort_scope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,11 +11,11 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-02-02 00:00:00.000000000Z
14
+ date: 2012-02-22 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
18
- requirement: &2154898880 !ruby/object:Gem::Requirement
18
+ requirement: &2152803180 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ! '>='
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: '3'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *2154898880
26
+ version_requirements: *2152803180
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
- requirement: &2154898100 !ruby/object:Gem::Requirement
29
+ requirement: &2152802420 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ! '>='
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: '3'
35
35
  type: :runtime
36
36
  prerelease: false
37
- version_requirements: *2154898100
37
+ version_requirements: *2152802420
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: test-unit
40
- requirement: &2154897700 !ruby/object:Gem::Requirement
40
+ requirement: &2152801900 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ! '>='
@@ -45,10 +45,10 @@ dependencies:
45
45
  version: '0'
46
46
  type: :development
47
47
  prerelease: false
48
- version_requirements: *2154897700
48
+ version_requirements: *2152801900
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: mysql2
51
- requirement: &2154891040 !ruby/object:Gem::Requirement
51
+ requirement: &2152801240 !ruby/object:Gem::Requirement
52
52
  none: false
53
53
  requirements:
54
54
  - - ! '>='
@@ -56,10 +56,10 @@ dependencies:
56
56
  version: '0'
57
57
  type: :development
58
58
  prerelease: false
59
- version_requirements: *2154891040
59
+ version_requirements: *2152801240
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rake
62
- requirement: &2154890620 !ruby/object:Gem::Requirement
62
+ requirement: &2152800640 !ruby/object:Gem::Requirement
63
63
  none: false
64
64
  requirements:
65
65
  - - ! '>='
@@ -67,7 +67,7 @@ dependencies:
67
67
  version: '0'
68
68
  type: :development
69
69
  prerelease: false
70
- version_requirements: *2154890620
70
+ version_requirements: *2152800640
71
71
  description: Provides big_cohort, which widens by finding the constraint that eliminates
72
72
  the most records and removing it. Also provides strict_cohort, which widens by eliminating
73
73
  constraints in order.
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  version: '0'
114
114
  requirements: []
115
115
  rubyforge_project: cohort_scope
116
- rubygems_version: 1.8.10
116
+ rubygems_version: 1.8.15
117
117
  signing_key:
118
118
  specification_version: 3
119
119
  summary: Provides cohorts (in the form of ActiveRecord scopes) that dynamically widen