Module: CohortScope
- Defined in:
- lib/cohort_scope.rb,
lib/cohort_scope/cohort.rb,
lib/cohort_scope/version.rb,
lib/cohort_scope/big_cohort.rb,
lib/cohort_scope/strict_cohort.rb
Defined Under Namespace
Modules: ActiveRecordRelationExt Classes: BigCohort, Cohort, StrictCohort
Constant Summary collapse
- VERSION =
'0.2.3'
Class Method Summary collapse
Instance Method Summary collapse
-
#big_cohort(constraints, options = {}) ⇒ Object
Find the biggest scope possible by removing constraints in any order.
-
#strict_cohort(*args) ⇒ Object
Find the first acceptable scope by removing constraints in strict order, starting with the last constraint.
Class Method Details
.conditions_for(constraints) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/cohort_scope.rb', line 17 def self.conditions_for(constraints) case constraints when ::Array constraints.inject({}) { |memo, (k, v)| memo[k] = v; memo } when ::Hash constraints.dup end end |
.extended(klass) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/cohort_scope.rb', line 9 def self.extended(klass) klass.class_eval do class << self attr_accessor :minimum_cohort_size end end end |
Instance Method Details
#big_cohort(constraints, options = {}) ⇒ Object
Find the biggest scope possible by removing constraints in any order. Returns an empty scope if it can’t meet the minimum scope size.
28 29 30 |
# File 'lib/cohort_scope.rb', line 28 def big_cohort(constraints, = {}) BigCohort.create self, constraints, ([:minimum_cohort_size] || minimum_cohort_size) end |
#strict_cohort(*args) ⇒ Object
Find the first acceptable scope by removing constraints in strict order, starting with the last constraint. Returns an empty scope if it can’t meet the minimum scope size.
constraints
must be key/value pairs (splat if it’s an array)
Note that the first constraint is implicitly required.
Take this example, where favorite color is considered to be “more important” than birthdate:
ordered_constraints = [ [:favorite_color, 'heliotrope'], [:birthdate, '1999-01-01'] ]
Citizen.strict_cohort(*ordered_constraints) #=> [...]
If the original constraints don’t meet the minimum scope size, then the only constraint that can be removed is birthdate. In other words, this would never return a scope that was constrained on birthdate but not on favorite_color.
46 47 48 49 50 51 |
# File 'lib/cohort_scope.rb', line 46 def strict_cohort(*args) args = args.dup = args.last.is_a?(::Hash) ? args.pop : {} constraints = args StrictCohort.create self, constraints, ([:minimum_cohort_size] || minimum_cohort_size) end |