Class: Babik::QuerySet::Disjunction

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

Overview

Disjunction in Disjunctive Normal Form i.e OR-based condition of AND-based conditions (disjunction of conjunctions)

See en.wikipedia.org/wiki/Disjunctive_normal_form

e.g.

(users.filter_name = 'Julius' AND posts.title = 'Stabbed to death: My story') OR
(users.filter_name = 'Marcus Antonius' AND posts.title = 'A sword in my belly button')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, conjunctions) ⇒ Disjunction

Construct a conjunction condition.

Parameters:

  • model (ActiveRecord::Base)

    Model owner of this condition.

  • conjunctions (Array)

    array of conjunctions that will be joined in a disjunction (hence the name Disjunctive Normal Form).



76
77
78
79
# File 'lib/babik/queryset/lib/condition.rb', line 76

def initialize(model, conjunctions)
  @model = model
  @conjunctions = conjunctions
end

Instance Attribute Details

#conjunctionsObject (readonly)

Returns the value of attribute conjunctions.



70
71
72
# File 'lib/babik/queryset/lib/condition.rb', line 70

def conjunctions
  @conjunctions
end

#modelObject (readonly)

Returns the value of attribute model.



70
71
72
# File 'lib/babik/queryset/lib/condition.rb', line 70

def model
  @model
end

Instance Method Details

#left_joins_by_aliasHash

Return a hash with the joins grouped by alias

Returns:

  • (Hash)

    alias: SQL::Join object



83
84
85
86
87
88
89
# File 'lib/babik/queryset/lib/condition.rb', line 83

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

#sqlString

Return SQL code for this disjunction. e.g

(first_name = 'Julius' AND last_name = 'Caesar') OR (zone.name = 'Rome')

Returns:

  • (String)

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



95
96
97
# File 'lib/babik/queryset/lib/condition.rb', line 95

def sql
  "(\n#{@conjunctions.map(&:sql).join(" OR\n")}\n)"
end