cohort_scope 0.2.1 → 0.2.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/cohort_scope.gemspec CHANGED
@@ -19,9 +19,10 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_dependency "activesupport", "~> 3.0"
23
- s.add_dependency "activerecord", "~> 3.0"
22
+ s.add_runtime_dependency "activesupport", '>=3'
23
+ s.add_runtime_dependency "activerecord", '>=3'
24
24
  s.add_development_dependency 'test-unit'
25
- s.add_development_dependency 'mysql'
25
+ s.add_development_dependency 'mysql2'
26
+ s.add_development_dependency 'rake'
26
27
  # s.add_development_dependency 'ruby-debug'
27
28
  end
@@ -2,13 +2,13 @@ require 'delegate'
2
2
 
3
3
  module CohortScope
4
4
  class Cohort < ::Delegator
5
-
6
5
  class << self
7
6
  # Recursively look for a scope that meets the constraints and is at least <tt>minimum_cohort_size</tt>.
8
7
  def create(active_record, constraints, minimum_cohort_size)
9
8
  if constraints.none? # failing base case
10
- empty_scope = active_record.scoped.where Arel::Nodes::Equality.new(1,2)
11
- return new(empty_scope)
9
+ impossible_cohort = new(active_record.scoped.where(IMPOSSIBLE_CONDITION))
10
+ impossible_cohort.impossible!
11
+ return impossible_cohort
12
12
  end
13
13
 
14
14
  constrained_scope = active_record.scoped.where CohortScope.conditions_for(constraints)
@@ -20,6 +20,8 @@ module CohortScope
20
20
  end
21
21
  end
22
22
  end
23
+
24
+ IMPOSSIBLE_CONDITION = ::Arel::Nodes::Equality.new(1,2)
23
25
 
24
26
  def initialize(obj)
25
27
  super
@@ -40,9 +42,23 @@ module CohortScope
40
42
  def as_json(*)
41
43
  { :members => count }
42
44
  end
45
+
46
+ def impossible!
47
+ @impossible = true
48
+ end
49
+
50
+ def impossible?
51
+ @impossible == true
52
+ end
53
+
54
+ def any?
55
+ return false if impossible?
56
+ super
57
+ end
43
58
 
44
59
  # sabshere 2/1/11 ActiveRecord does this for #any? but not for #none?
45
60
  def none?(&blk)
61
+ return true if impossible?
46
62
  if block_given?
47
63
  to_a.none? &blk
48
64
  else
@@ -75,7 +91,7 @@ module CohortScope
75
91
  end
76
92
 
77
93
  def inspect
78
- "<Cohort scope with #{count} members>"
94
+ "#<#{self.class.name} with #{count} members>"
79
95
  end
80
96
  end
81
97
  end
@@ -1,3 +1,3 @@
1
1
  module CohortScope
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
data/lib/cohort_scope.rb CHANGED
@@ -1,17 +1,11 @@
1
1
  require 'active_record'
2
+ require 'active_support/core_ext'
2
3
 
3
- require 'active_support'
4
- require 'active_support/version'
5
- if ActiveSupport::VERSION::MAJOR == 3
6
- require 'active_support/json'
7
- require 'active_support/core_ext/hash'
8
- end
4
+ require 'cohort_scope/cohort'
5
+ require 'cohort_scope/big_cohort'
6
+ require 'cohort_scope/strict_cohort'
9
7
 
10
8
  module CohortScope
11
- autoload :Cohort, 'cohort_scope/cohort'
12
- autoload :BigCohort, 'cohort_scope/big_cohort'
13
- autoload :StrictCohort, 'cohort_scope/strict_cohort'
14
-
15
9
  def self.extended(klass)
16
10
  klass.class_eval do
17
11
  class << self
data/test/helper.rb CHANGED
@@ -10,10 +10,10 @@ class Test::Unit::TestCase
10
10
  end
11
11
 
12
12
  # require 'logger'
13
- # ActiveRecord::Base.logger = Logger.new($stderr)
13
+ # ActiveRecord::Base.logger = Logger.new($stdout)
14
14
 
15
15
  ActiveRecord::Base.establish_connection(
16
- 'adapter' => 'mysql',
16
+ 'adapter' => 'mysql2',
17
17
  'database' => 'test_cohort_scope',
18
18
  'username' => 'root',
19
19
  'password' => 'password'
@@ -64,7 +64,7 @@ end
64
64
  [ '1985-03-02', nil, 27 ],
65
65
  [ '1982-05-01', nil, 28 ]
66
66
  ].each do |birthdate, favorite_color, teeth|
67
- Citizen.create! :birthdate => birthdate, :favorite_color => favorite_color, :teeth => teeth
67
+ c = Citizen.new; c.birthdate = birthdate; c.favorite_color = favorite_color; c.teeth = teeth; c.save!
68
68
  end
69
69
 
70
70
  class Period < ActiveRecord::Base
@@ -97,13 +97,13 @@ class Resident < ActiveRecord::Base
97
97
  belongs_to :house
98
98
  end
99
99
 
100
- p1 = Period.create! :name => 'arts and crafts'
101
- p2 = Period.create! :name => 'victorian'
102
- Style.create! :period => p1, :name => 'classical revival'
103
- Style.create! :period => p1, :name => 'gothic'
104
- Style.create! :period => p1, :name => 'art deco'
105
- Style.create! :period => p2, :name => 'stick-eastlake'
106
- Style.create! :period => p2, :name => 'queen anne'
100
+ p1 = Period.new; p1.name = 'arts and crafts'; p1.save!
101
+ p2 = Period.new; p2.name = 'victorian'; p2.save!
102
+ s1 = Style.new; s1.period = p1; s1.name = 'classical revival'; s1.save!
103
+ s2 = Style.new; s2.period = p1; s2.name = 'gothic'; s2.save!
104
+ s3 = Style.new; s3.period = p1; s3.name = 'art deco'; s3.save!
105
+ s4 = Style.new; s4.period = p2; s4.name = 'stick-eastlake'; s4.save!
106
+ s5 = Style.new; s5.period = p2; s5.name = 'queen anne'; s5.save!
107
107
  h1 = House.create! :period => p1, :address => '123 Maple', :storeys => 1
108
108
  h2 = House.create! :period => p1, :address => '223 Walnut', :storeys => 2
109
109
  h3 = House.create! :period => p2, :address => '323 Pine', :storeys => 2
@@ -28,10 +28,9 @@ class TestCohortScope < Test::Unit::TestCase
28
28
  end
29
29
 
30
30
  # should this even work in theory?
31
- def test_002b_simplified_joins
32
- flunk
33
- assert_equal 3, Style.big_cohort(:houses => [house1]).length
34
- end
31
+ # def test_002b_simplified_joins
32
+ # assert_equal 3, Style.big_cohort(:houses => [house1]).length
33
+ # end
35
34
 
36
35
  def test_003_redefine_any_query_method
37
36
  cohort = Citizen.big_cohort(:birthdate => @date_range)
@@ -69,7 +68,7 @@ class TestCohortScope < Test::Unit::TestCase
69
68
 
70
69
  def test_008_short_inspect
71
70
  cohort = Citizen.big_cohort :birthdate => @date_range, :favorite_color => 'heliotrope'
72
- assert_equal "<Cohort scope with 9 members>", cohort.inspect
71
+ assert_equal "#<CohortScope::BigCohort with 9 members>", cohort.inspect
73
72
  end
74
73
 
75
74
  def test_009_not_reveal_itself_in_to_hash
metadata CHANGED
@@ -1,74 +1,82 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: cohort_scope
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
4
5
  prerelease:
5
- version: 0.2.1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Seamus Abshere
9
9
  - Andy Rossmeissl
10
10
  - Derek Kastner
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
-
15
- date: 2011-06-03 00:00:00 -05:00
16
- default_executable:
17
- dependencies:
18
- - !ruby/object:Gem::Dependency
14
+ date: 2012-02-02 00:00:00.000000000Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
19
17
  name: activesupport
20
- prerelease: false
21
- requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirement: &2154898880 !ruby/object:Gem::Requirement
22
19
  none: false
23
- requirements:
24
- - - ~>
25
- - !ruby/object:Gem::Version
26
- version: "3.0"
20
+ requirements:
21
+ - - ! '>='
22
+ - !ruby/object:Gem::Version
23
+ version: '3'
27
24
  type: :runtime
28
- version_requirements: *id001
29
- - !ruby/object:Gem::Dependency
30
- name: activerecord
31
25
  prerelease: false
32
- requirement: &id002 !ruby/object:Gem::Requirement
26
+ version_requirements: *2154898880
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: &2154898100 !ruby/object:Gem::Requirement
33
30
  none: false
34
- requirements:
35
- - - ~>
36
- - !ruby/object:Gem::Version
37
- version: "3.0"
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '3'
38
35
  type: :runtime
39
- version_requirements: *id002
40
- - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ version_requirements: *2154898100
38
+ - !ruby/object:Gem::Dependency
41
39
  name: test-unit
40
+ requirement: &2154897700 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ type: :development
42
47
  prerelease: false
43
- requirement: &id003 !ruby/object:Gem::Requirement
48
+ version_requirements: *2154897700
49
+ - !ruby/object:Gem::Dependency
50
+ name: mysql2
51
+ requirement: &2154891040 !ruby/object:Gem::Requirement
44
52
  none: false
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- version: "0"
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
49
57
  type: :development
50
- version_requirements: *id003
51
- - !ruby/object:Gem::Dependency
52
- name: mysql
53
58
  prerelease: false
54
- requirement: &id004 !ruby/object:Gem::Requirement
59
+ version_requirements: *2154891040
60
+ - !ruby/object:Gem::Dependency
61
+ name: rake
62
+ requirement: &2154890620 !ruby/object:Gem::Requirement
55
63
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- version: "0"
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
60
68
  type: :development
61
- version_requirements: *id004
62
- description: Provides big_cohort, which widens by finding the constraint that eliminates the most records and removing it. Also provides strict_cohort, which widens by eliminating constraints in order.
63
- email:
69
+ prerelease: false
70
+ version_requirements: *2154890620
71
+ description: Provides big_cohort, which widens by finding the constraint that eliminates
72
+ the most records and removing it. Also provides strict_cohort, which widens by eliminating
73
+ constraints in order.
74
+ email:
64
75
  - seamus@abshere.net
65
76
  executables: []
66
-
67
77
  extensions: []
68
-
69
78
  extra_rdoc_files: []
70
-
71
- files:
79
+ files:
72
80
  - .document
73
81
  - .gitignore
74
82
  - Gemfile
@@ -85,36 +93,34 @@ files:
85
93
  - test/test_big_cohort.rb
86
94
  - test/test_cohort_scope.rb
87
95
  - test/test_strict_cohort.rb
88
- has_rdoc: true
89
96
  homepage: https://github.com/seamusabshere/cohort_scope
90
97
  licenses: []
91
-
92
98
  post_install_message:
93
99
  rdoc_options: []
94
-
95
- require_paths:
100
+ require_paths:
96
101
  - lib
97
- required_ruby_version: !ruby/object:Gem::Requirement
102
+ required_ruby_version: !ruby/object:Gem::Requirement
98
103
  none: false
99
- requirements:
100
- - - ">="
101
- - !ruby/object:Gem::Version
102
- version: "0"
103
- required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
109
  none: false
105
- requirements:
106
- - - ">="
107
- - !ruby/object:Gem::Version
108
- version: "0"
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
109
114
  requirements: []
110
-
111
115
  rubyforge_project: cohort_scope
112
- rubygems_version: 1.6.2
116
+ rubygems_version: 1.8.10
113
117
  signing_key:
114
118
  specification_version: 3
115
- summary: Provides cohorts (in the form of ActiveRecord scopes) that dynamically widen until they contain a certain number of records.
116
- test_files:
119
+ summary: Provides cohorts (in the form of ActiveRecord scopes) that dynamically widen
120
+ until they contain a certain number of records.
121
+ test_files:
117
122
  - test/helper.rb
118
123
  - test/test_big_cohort.rb
119
124
  - test/test_cohort_scope.rb
120
125
  - test/test_strict_cohort.rb
126
+ has_rdoc: