Class: CohortScope::Cohort

Inherits:
Delegator
  • Object
show all
Defined in:
lib/cohort_scope/cohort.rb

Direct Known Subclasses

BigCohort, StrictCohort

Constant Summary collapse

IMPOSSIBLE_CONDITION =
::Arel::Nodes::Equality.new(1,2)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Cohort

Returns a new instance of Cohort.



26
27
28
29
# File 'lib/cohort_scope/cohort.rb', line 26

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
# File 'lib/cohort_scope/cohort.rb', line 7

def create(active_record, constraints, minimum_cohort_size)
  if constraints.none? # failing base case
    impossible_cohort = new(active_record.scoped.where(IMPOSSIBLE_CONDITION))
    impossible_cohort.impossible!
    return impossible_cohort
  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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cohort_scope/cohort.rb', line 77

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



30
31
32
# File 'lib/cohort_scope/cohort.rb', line 30

def __getobj__
  @_ch_obj
end

#__setobj__(obj) ⇒ Object



33
34
35
# File 'lib/cohort_scope/cohort.rb', line 33

def __setobj__(obj)
  @_ch_obj = obj
end

#active_recordObject



73
74
75
# File 'lib/cohort_scope/cohort.rb', line 73

def active_record
  __getobj__.klass
end

#any?Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/cohort_scope/cohort.rb', line 54

def any?
  return false if impossible?
  super
end

#as_jsonObject



42
43
44
# File 'lib/cohort_scope/cohort.rb', line 42

def as_json(*)
  { :members => count }
end

#impossible!Object



46
47
48
# File 'lib/cohort_scope/cohort.rb', line 46

def impossible!
  @impossible = true
end

#impossible?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cohort_scope/cohort.rb', line 50

def impossible?
  @impossible == true
end

#inspectObject



93
94
95
# File 'lib/cohort_scope/cohort.rb', line 93

def inspect
  "#<#{self.class.name} with #{count} members>"
end

#none?(&blk) ⇒ Boolean

sabshere 2/1/11 ActiveRecord does this for #any? but not for #none?

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/cohort_scope/cohort.rb', line 60

def none?(&blk)
  return true if impossible?
  if block_given?
    to_a.none? &blk
  else
    super
  end
end

#to_jsonObject

sabshere 2/1/11 overriding as_json per usual doesn’t seem to work



38
39
40
# File 'lib/cohort_scope/cohort.rb', line 38

def to_json(*)
  as_json.to_json
end

#where_value_nodesObject



69
70
71
# File 'lib/cohort_scope/cohort.rb', line 69

def where_value_nodes
  __getobj__.instance_variable_get(:@where_values)
end