Class: CohortScope::Cohort
- Inherits:
-
Delegator
- Object
- Delegator
- CohortScope::Cohort
- Defined in:
- lib/cohort_scope/cohort.rb
Direct Known Subclasses
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
- #as_json ⇒ Object
-
#initialize(obj) ⇒ Cohort
constructor
A new instance of Cohort.
- #inspect ⇒ Object
-
#none?(&blk) ⇒ Boolean
sabshere 2/1/11 ActiveRecord does this for #any? but not for #none?.
-
#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.
24 25 26 27 |
# File 'lib/cohort_scope/cohort.rb', line 24 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
.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/cohort_scope/cohort.rb', line 8 def create(active_record, constraints, minimum_cohort_size) if constraints.none? # failing base case empty_scope = active_record.scoped.where Arel::Nodes::Equality.new(1,2) return new(empty_scope) end constrained_scope = active_record.scoped.where CohortScope.conditions_for(constraints) if constrained_scope.count >= minimum_cohort_size new constrained_scope else create active_record, reduce_constraints(active_record, constraints), minimum_cohort_size end end |
Instance Method Details
#+(other) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cohort_scope/cohort.rb', line 61 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
28 29 30 |
# File 'lib/cohort_scope/cohort.rb', line 28 def __getobj__ @_ch_obj end |
#__setobj__(obj) ⇒ Object
31 32 33 |
# File 'lib/cohort_scope/cohort.rb', line 31 def __setobj__(obj) @_ch_obj = obj end |
#active_record ⇒ Object
57 58 59 |
# File 'lib/cohort_scope/cohort.rb', line 57 def active_record __getobj__.klass end |
#as_json ⇒ Object
40 41 42 |
# File 'lib/cohort_scope/cohort.rb', line 40 def as_json(*) { :members => count } end |
#inspect ⇒ Object
77 78 79 |
# File 'lib/cohort_scope/cohort.rb', line 77 def inspect "<Cohort scope with #{count} members>" end |
#none?(&blk) ⇒ Boolean
sabshere 2/1/11 ActiveRecord does this for #any? but not for #none?
45 46 47 48 49 50 51 |
# File 'lib/cohort_scope/cohort.rb', line 45 def none?(&blk) if block_given? 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
36 37 38 |
# File 'lib/cohort_scope/cohort.rb', line 36 def to_json(*) as_json.to_json end |
#where_value_nodes ⇒ Object
53 54 55 |
# File 'lib/cohort_scope/cohort.rb', line 53 def where_value_nodes __getobj__.instance_variable_get(:@where_values) end |