cohort_scope 0.0.3 → 0.0.4
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 +20 -34
- data/test/helper.rb +2 -2
- data/test/test_cohort_scope.rb +12 -0
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/lib/cohort_scope.rb
CHANGED
@@ -8,34 +8,18 @@ require 'active_support/version'
|
|
8
8
|
end if ActiveSupport::VERSION::MAJOR == 3
|
9
9
|
|
10
10
|
module ActiveRecord
|
11
|
-
class
|
12
|
-
def
|
13
|
-
@
|
14
|
-
finder_needs_type_condition? ? @massive_unscoped.where(type_condition) : @massive_unscoped
|
11
|
+
class Relation
|
12
|
+
def inspect_count_only!
|
13
|
+
@_inspect_count_only = true
|
15
14
|
end
|
16
|
-
|
17
|
-
|
18
|
-
module ClassMethods
|
19
|
-
# Work with a MassiveRelation, which, when inspected, does not generate a huge string.
|
20
|
-
def massive_scoped(options = {}, &block)
|
21
|
-
if options.present?
|
22
|
-
scoped = current_scoped_methods ? massive_unscoped.merge(current_scoped_methods) : unscoped.clone
|
23
|
-
relation = scoped.apply_finder_options(options)
|
24
|
-
block_given? ? relation.extending(Module.new(&block)) : relation
|
25
|
-
else
|
26
|
-
raise "MassiveScopes should be created with options"
|
27
|
-
end
|
28
|
-
end
|
15
|
+
def inspect_count_only?
|
16
|
+
@_inspect_count_only == true
|
29
17
|
end
|
30
|
-
|
31
|
-
|
32
|
-
# Don't try to output a massive string.
|
33
|
-
def inspect
|
34
|
-
"<Massive relation: #{count} members>"
|
18
|
+
def as_json(*args)
|
19
|
+
inspect_count_only? ? { :members => count }.as_json : to_a.as_json
|
35
20
|
end
|
36
|
-
|
37
|
-
|
38
|
-
{ :members => count }.to_json
|
21
|
+
def inspect
|
22
|
+
inspect_count_only? ? "<Massive ActiveRecord scope with #{count} members>" : to_a.inspect
|
39
23
|
end
|
40
24
|
end
|
41
25
|
end
|
@@ -51,7 +35,7 @@ module CohortScope
|
|
51
35
|
# Returns an empty scope if it can't meet the minimum scope size.
|
52
36
|
def big_cohort(constraints = {}, custom_minimum_cohort_size = nil)
|
53
37
|
raise ArgumentError, "You can't give a big_cohort an OrderedHash; do you want strict_cohort?" if constraints.is_a?(ActiveSupport::OrderedHash)
|
54
|
-
|
38
|
+
_cohort_scope constraints, custom_minimum_cohort_size
|
55
39
|
end
|
56
40
|
|
57
41
|
# Find the first acceptable scope by removing constraints <b>in strict order</b>, starting with the last constraint.
|
@@ -72,27 +56,29 @@ module CohortScope
|
|
72
56
|
# In other words, this would never return a scope that was constrained on birthdate but not on favorite_color.
|
73
57
|
def strict_cohort(constraints, custom_minimum_cohort_size = nil)
|
74
58
|
raise ArgumentError, "You need to give strict_cohort an OrderedHash" unless constraints.is_a?(ActiveSupport::OrderedHash)
|
75
|
-
|
59
|
+
_cohort_scope constraints, custom_minimum_cohort_size
|
76
60
|
end
|
77
61
|
|
78
62
|
protected
|
79
63
|
|
80
64
|
# Recursively look for a scope that meets the constraints and is at least <tt>minimum_cohort_size</tt>.
|
81
|
-
def
|
65
|
+
def _cohort_scope(constraints, custom_minimum_cohort_size)
|
82
66
|
raise RuntimeError, "You need to set #{name}.minimum_cohort_size = X" unless minimum_cohort_size.present?
|
83
67
|
|
84
68
|
if constraints.values.none? # failing base case
|
85
|
-
return
|
69
|
+
return scoped.where('false')
|
86
70
|
end
|
87
71
|
|
88
72
|
this_hash = _cohort_constraints constraints
|
89
|
-
this_count = scoped(this_hash).count
|
73
|
+
this_count = scoped.where(this_hash).count
|
90
74
|
|
91
75
|
if this_count >= (custom_minimum_cohort_size || minimum_cohort_size) # successful base case
|
92
|
-
|
76
|
+
cohort = scoped.where this_hash
|
93
77
|
else
|
94
|
-
|
78
|
+
cohort = _cohort_scope _cohort_reduce_constraints(constraints), custom_minimum_cohort_size
|
95
79
|
end
|
80
|
+
cohort.inspect_count_only!
|
81
|
+
cohort
|
96
82
|
end
|
97
83
|
|
98
84
|
# Sanitize constraints by
|
@@ -110,7 +96,7 @@ module CohortScope
|
|
110
96
|
memo.merge! condition if condition.is_a? Hash
|
111
97
|
memo
|
112
98
|
end
|
113
|
-
|
99
|
+
conditions
|
114
100
|
end
|
115
101
|
|
116
102
|
# Convert constraints that are provided as ActiveRecord::Base objects into their corresponding integer primary keys.
|
@@ -152,7 +138,7 @@ module CohortScope
|
|
152
138
|
losing_key = nil
|
153
139
|
constraints.keys.each do |key|
|
154
140
|
test_constraints = constraints.except(key)
|
155
|
-
count_after_removal = scoped(_cohort_constraints(test_constraints)).count
|
141
|
+
count_after_removal = scoped.where(_cohort_constraints(test_constraints)).count
|
156
142
|
if highest_count_after_removal.nil? or count_after_removal > highest_count_after_removal
|
157
143
|
highest_count_after_removal = count_after_removal
|
158
144
|
losing_key = key
|
data/test/helper.rb
CHANGED
@@ -11,7 +11,7 @@ require 'cohort_scope'
|
|
11
11
|
class Test::Unit::TestCase
|
12
12
|
end
|
13
13
|
|
14
|
-
$logger = Logger.new
|
14
|
+
$logger = Logger.new 'test/test.log' #STDOUT
|
15
15
|
ActiveSupport::Notifications.subscribe do |*args|
|
16
16
|
event = ActiveSupport::Notifications::Event.new(*args)
|
17
17
|
$logger.debug "#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}"
|
@@ -21,7 +21,7 @@ ActiveRecord::Base.establish_connection(
|
|
21
21
|
'adapter' => 'mysql',
|
22
22
|
'database' => 'cohort_scope_test',
|
23
23
|
'username' => 'root',
|
24
|
-
'password' => ''
|
24
|
+
'password' => 'password'
|
25
25
|
)
|
26
26
|
|
27
27
|
ActiveRecord::Schema.define(:version => 20090819143429) do
|
data/test/test_cohort_scope.rb
CHANGED
@@ -6,6 +6,18 @@ class TestCohortScope < Test::Unit::TestCase
|
|
6
6
|
@date_range = (Date.parse('1980-01-01')..Date.parse('1990-01-01'))
|
7
7
|
end
|
8
8
|
|
9
|
+
should "not raise on to_json" do
|
10
|
+
cohort = Citizen.big_cohort :birthdate => @date_range, :favorite_color => 'heliotrope'
|
11
|
+
assert_nothing_raised do
|
12
|
+
cohort.to_json
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
should "inspect to a short string" do
|
17
|
+
cohort = Citizen.big_cohort :birthdate => @date_range, :favorite_color => 'heliotrope'
|
18
|
+
assert_equal "<Massive ActiveRecord scope with 9 members>", cohort.inspect
|
19
|
+
end
|
20
|
+
|
9
21
|
should "raise if no minimum_cohort_size is specified" do
|
10
22
|
Citizen.minimum_cohort_size = nil
|
11
23
|
assert_raises(RuntimeError) {
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Seamus Abshere
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-
|
19
|
+
date: 2010-05-10 00:00:00 -04:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|