Class: CohortAnalysis::Strategy

Inherits:
Arel::Nodes::Node
  • Object
show all
Defined in:
lib/cohort_analysis/strategy.rb,
lib/cohort_analysis/strategy/big.rb,
lib/cohort_analysis/strategy/strict.rb

Direct Known Subclasses

Big, Strict

Defined Under Namespace

Modules: AlwaysTrue, Impossible Classes: Big, Strict

Constant Summary collapse

DEFAULT_STRATEGY =
:big

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(select_manager, characteristics, options = {}) ⇒ Strategy

Returns a new instance of Strategy.



33
34
35
36
37
38
39
40
41
# File 'lib/cohort_analysis/strategy.rb', line 33

def initialize(select_manager, characteristics, options = {})
  @select_manager = select_manager
  @table_name = select_manager.source.left.name
  @table = Arel::Table.new table_name
  @original = characteristics.dup
  @current = characteristics.dup
  @minimum_size = options.fetch(:minimum_size, 1)
  @final_mutex = Mutex.new
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



28
29
30
# File 'lib/cohort_analysis/strategy.rb', line 28

def current
  @current
end

#minimum_sizeObject (readonly)

Returns the value of attribute minimum_size.



29
30
31
# File 'lib/cohort_analysis/strategy.rb', line 29

def minimum_size
  @minimum_size
end

#originalObject (readonly)

Returns the value of attribute original.



27
28
29
# File 'lib/cohort_analysis/strategy.rb', line 27

def original
  @original
end

#select_managerObject (readonly)

Returns the value of attribute select_manager.



26
27
28
# File 'lib/cohort_analysis/strategy.rb', line 26

def select_manager
  @select_manager
end

#tableObject (readonly)

Returns the value of attribute table.



31
32
33
# File 'lib/cohort_analysis/strategy.rb', line 31

def table
  @table
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



30
31
32
# File 'lib/cohort_analysis/strategy.rb', line 30

def table_name
  @table_name
end

Class Method Details

.create(select_manager, characteristics, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/cohort_analysis/strategy.rb', line 4

def create(select_manager, characteristics, options = {})
  options = options.symbolize_keys
  strategy = if options.has_key? :strategy
    options[:strategy]
  elsif options.has_key? :priority
    :strict
  else
    DEFAULT_STRATEGY
  end
  const_get(strategy.to_s.camelcase).new(select_manager, characteristics, options)
end

Instance Method Details

#==(other) ⇒ Object



59
60
61
62
63
64
# File 'lib/cohort_analysis/strategy.rb', line 59

def ==(other)
  other.is_a?(Strategy) and
    table_name == other.table_name and
    minimum_size = other.minimum_size and
    original == other.original
end

#exprObject



55
56
57
# File 'lib/cohort_analysis/strategy.rb', line 55

def expr
  final.to_sql
end

#finalObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cohort_analysis/strategy.rb', line 43

def final
  @final || if @final_mutex.try_lock
    begin
      @final ||= resolve!
    ensure
      @final_mutex.unlock
    end
  else
    Impossible
  end
end