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.



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_recordObject



81
82
83
# File 'lib/cohort_scope/cohort.rb', line 81

def active_record
  __getobj__.klass
end

#any?Boolean

Returns:

  • (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_jsonObject



52
53
54
# File 'lib/cohort_scope/cohort.rb', line 52

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

#countObject



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

Returns:

  • (Boolean)


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

def empty?
  count == 0
end

#inspectObject



101
102
103
# File 'lib/cohort_scope/cohort.rb', line 101

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

#none?(&blk) ⇒ Boolean

Returns:

  • (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_jsonObject

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_nodesObject



77
78
79
# File 'lib/cohort_scope/cohort.rb', line 77

def where_value_nodes
  __getobj__.instance_variable_get(:@where_values)
end