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.1'
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
23 24 25 26 27 28 29 30 |
# File 'lib/cohort_scope.rb', line 23 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
15 16 17 18 19 20 21 |
# File 'lib/cohort_scope.rb', line 15 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.
34 35 36 |
# File 'lib/cohort_scope.rb', line 34 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.
52 53 54 55 56 57 |
# File 'lib/cohort_scope.rb', line 52 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 |