Class: Where

Inherits:
Object
  • Object
show all
Defined in:
lib/where.rb

Defined Under Namespace

Classes: Clause

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(criteria_or_options = nil, *params) {|_self| ... } ⇒ Where

Constructs a new where clause

optionally, you can provide a criteria, like the following:

Where.initialize "joke_title = ?", "He says, 'Under there', to which I reply, 'under where?'"

Yields:

  • (_self)

Yield Parameters:

  • _self (Where)

    the object that the method was called on



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/where.rb', line 43

def initialize(criteria_or_options=nil, *params, &block)
  @clauses=Array.new
  @target = self
  if criteria_or_options.is_a?(Hash)
    criteria = nil
    options = criteria_or_options
  else
    criteria = criteria_or_options
    options = {}
  end
  self.and(criteria, *params) unless criteria.nil?
  self.default_params = options[:default_params] || {}
  
  yield(self) if block_given?
end

Instance Attribute Details

#clausesObject (readonly)

Returns the value of attribute clauses.



37
38
39
# File 'lib/where.rb', line 37

def clauses
  @clauses
end

#default_paramsObject

Returns the value of attribute default_params.



36
37
38
# File 'lib/where.rb', line 36

def default_params
  @default_params
end

#targetObject (readonly)

Returns the value of attribute target.



37
38
39
# File 'lib/where.rb', line 37

def target
  @target
end

Class Method Details

.&(params) ⇒ Object



110
111
112
# File 'lib/where.rb', line 110

def self.&(params)
  Where.new(*params)
end

.|(params) ⇒ Object



114
115
116
# File 'lib/where.rb', line 114

def self.|(params)
  Where.new.or(*params)
end

Instance Method Details

#&(params) ⇒ Object



102
103
104
# File 'lib/where.rb', line 102

def &(params)
  self.and(*params)
end

#and(*params, &block) ⇒ Object Also known as: <<

Appends an and expression to your where clause

Example:

where = Where.new
where.and("name = ?", "Tim O'brien")
where.to_s

# => "(name = 'Tim O''brien')


72
73
74
# File 'lib/where.rb', line 72

def and(*params, &block)
  @target.append_clause(params, "AND", &block)
end

#and_not(*params, &block) ⇒ Object

Same as and, but negates the whole expression



98
99
100
# File 'lib/where.rb', line 98

def and_not(*params, &block)
  @target.append_clause(params, "AND NOT", &block)
end

#blank?Boolean Also known as: empty?

Determines if any clauses have been added.

where = Where.new
where.blank?
# => true

where.and(nil)
where.blank?
# => true

where.and(Where.new(nil))
where.blank?
# => true

where.and("name=1")
where.blank?
# => false

Returns:

  • (Boolean)


153
154
155
# File 'lib/where.rb', line 153

def blank?
  @clauses.empty?
end

#initialize_copy(from) ⇒ Object



59
60
61
# File 'lib/where.rb', line 59

def initialize_copy(from)
  @clauses = from.instance_variable_get("@clauses").clone
end

#or(*params, &block) ⇒ Object

Appends an or expression to your where clause

Example:

where = Where.new
where.or("name = ?", "Tim O'brien")
where.or("name = ?", "Tim O'neal")
where.to_s

# => "(name = 'Tim O''brien') or (name = 'Tim O''neal')"


88
89
90
# File 'lib/where.rb', line 88

def or(*params, &block)
  @target.append_clause(params, "OR", &block)
end

#or_not(*params, &block) ⇒ Object

Same as or, but negates the whole expression



93
94
95
# File 'lib/where.rb', line 93

def or_not(*params, &block)
  @target.append_clause(params, "OR NOT", &block)
end

#to_s(format = nil) ⇒ Object Also known as: to_sql

Converts the where clause to a SQL string.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/where.rb', line 119

def to_s(format=nil)
  output=""
  
  @clauses.each_index{|index|
    omit_conjuction = (index==0)
    output << @clauses[index].to_s(omit_conjuction)  # Omit the clause if index=0
  }
  case format
  when :where
    output.empty? ? "" : " WHERE #{output}"
  else
    output.empty? ? "(true)" : output
  end
end

#|(params) ⇒ Object



106
107
108
# File 'lib/where.rb', line 106

def |(params)
  self.or(*params)
end