Class: Babik::QuerySet::Condition::Conjunction

Inherits:
Object
  • Object
show all
Defined in:
lib/babik/queryset/lib/condition.rb

Overview

AND-based condition, also known as conjunction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, filter) ⇒ Conjunction

Construct a conjunction condition.

Parameters:

  • model (ActiveRecord::Base)

    Model owner of this condition.

  • filter (Hash)

    a hash where the key identify field paths and the values the values they must take.



30
31
32
33
34
35
36
37
# File 'lib/babik/queryset/lib/condition.rb', line 30

def initialize(model, filter)
  @model = model
  @selections = []
  # filter is a Hash composed by :selection_path => value
  filter.each do |selection_path, value|
    @selections << Babik::Selection::Base.factory(@model, selection_path, value)
  end
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



25
26
27
# File 'lib/babik/queryset/lib/condition.rb', line 25

def model
  @model
end

#selectionsObject (readonly)

Returns the value of attribute selections.



25
26
27
# File 'lib/babik/queryset/lib/condition.rb', line 25

def selections
  @selections
end

Instance Method Details

#left_joins_by_aliasHash

Return a hash with the joins grouped by alias

Returns:

  • (Hash)

    alias: SQL::Join object



41
42
43
44
45
46
47
# File 'lib/babik/queryset/lib/condition.rb', line 41

def left_joins_by_alias
  left_joins_by_alias_ = {}
  @selections.each do |selection|
    left_joins_by_alias_.merge!(selection.left_joins_by_alias)
  end
  left_joins_by_alias_
end

#sqlString

Return SQL code for this conjunction. e.g

(first_name = 'Julius' AND last_name = 'Caesar' AND zone = 'Rome')

Returns:

  • (String)

    SQL code that will be used in the WHERE part of SQL SELECT statements.



53
54
55
# File 'lib/babik/queryset/lib/condition.rb', line 53

def sql
  @selections.map(&:sql_where_condition).join(" AND\n")
end