Class: CohortScope::Cohort
- Inherits:
-
Delegator
- Object
- Delegator
- CohortScope::Cohort
- Defined in:
- lib/cohort_scope/cohort.rb
Direct Known Subclasses
Constant Summary collapse
- IMPOSSIBLE_CONDITION =
::Arel::Nodes::Equality.new(1,2)
Class Method Summary collapse
-
.create(active_record, constraints, minimum_cohort_size) ⇒ Object
Recursively look for a scope that meets the constraints and is at least
minimum_cohort_size
.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #__getobj__ ⇒ Object
- #__setobj__(obj) ⇒ Object
- #active_record ⇒ Object
- #any? ⇒ Boolean
- #as_json ⇒ Object
- #count ⇒ Object
- #count=(int) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(obj) ⇒ Cohort
constructor
A new instance of Cohort.
- #inspect ⇒ Object
- #none?(&blk) ⇒ Boolean
-
#to_json ⇒ Object
sabshere 2/1/11 overriding as_json per usual doesn’t seem to work.
- #where_value_nodes ⇒ Object
Constructor Details
#initialize(obj) ⇒ Cohort
Returns a new instance of Cohort.
28 29 30 31 |
# File 'lib/cohort_scope/cohort.rb', line 28 def initialize(obj) super @_ch_obj = obj end |
Class Method Details
.create(active_record, constraints, minimum_cohort_size) ⇒ Object
Recursively look for a scope that meets the constraints and is at least minimum_cohort_size
.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cohort_scope/cohort.rb', line 7 def create(active_record, constraints, minimum_cohort_size) if constraints.none? # failing base case cohort = new active_record.scoped.where(IMPOSSIBLE_CONDITION) cohort.count = 0 return cohort end constrained_scope = active_record.scoped.where CohortScope.conditions_for(constraints) if (count = constrained_scope.count) >= minimum_cohort_size cohort = new constrained_scope cohort.count = count cohort else create active_record, reduce_constraints(active_record, constraints), minimum_cohort_size end end |
Instance Method Details
#+(other) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/cohort_scope/cohort.rb', line 85 def +(other) case other when Cohort combined_conditions = (where_value_nodes + other.where_value_nodes).inject(nil) do |memo, node| if memo.nil? node else memo.or(node) end end Cohort.new active_record.where(combined_conditions) else super end end |
#__getobj__ ⇒ Object
32 33 34 |
# File 'lib/cohort_scope/cohort.rb', line 32 def __getobj__ @_ch_obj end |
#__setobj__(obj) ⇒ Object
35 36 37 |
# File 'lib/cohort_scope/cohort.rb', line 35 def __setobj__(obj) @_ch_obj = obj end |
#active_record ⇒ Object
81 82 83 |
# File 'lib/cohort_scope/cohort.rb', line 81 def active_record __getobj__.klass end |
#any? ⇒ Boolean
60 61 62 63 64 |
# File 'lib/cohort_scope/cohort.rb', line 60 def any? return false if count == 0 return true if !block_given? and count > 0 super end |
#as_json ⇒ Object
52 53 54 |
# File 'lib/cohort_scope/cohort.rb', line 52 def as_json(*) { :members => count } end |
#count ⇒ Object
43 44 45 |
# File 'lib/cohort_scope/cohort.rb', line 43 def count @count ||= super end |
#count=(int) ⇒ Object
39 40 41 |
# File 'lib/cohort_scope/cohort.rb', line 39 def count=(int) @count = int end |
#empty? ⇒ Boolean
56 57 58 |
# File 'lib/cohort_scope/cohort.rb', line 56 def empty? count == 0 end |
#inspect ⇒ Object
101 102 103 |
# File 'lib/cohort_scope/cohort.rb', line 101 def inspect "#<#{self.class.name} with #{count} members>" end |
#none?(&blk) ⇒ Boolean
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cohort_scope/cohort.rb', line 66 def none?(&blk) return true if count == 0 return false if !block_given? and count > 0 if block_given? # sabshere 2/1/11 ActiveRecord does this for #any? but not for #none? to_a.none? &blk else super end end |
#to_json ⇒ Object
sabshere 2/1/11 overriding as_json per usual doesn’t seem to work
48 49 50 |
# File 'lib/cohort_scope/cohort.rb', line 48 def to_json(*) as_json.to_json end |
#where_value_nodes ⇒ Object
77 78 79 |
# File 'lib/cohort_scope/cohort.rb', line 77 def where_value_nodes __getobj__.instance_variable_get(:@where_values) end |