Class: SqlBuilder

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

Instance Method Summary collapse

Constructor Details

#initializeSqlBuilder

Returns a new instance of SqlBuilder.



3
4
5
6
# File 'lib/sql_builder.rb', line 3

def initialize
  @sql = []
  @params = []
end

Instance Method Details

#and(*args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sql_builder.rb', line 24

def and(*args)
  append_string('and (')
  
  if block_given?
    yield self
  else
    append(*args)
  end
  
  append_string(')')
end

#append(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sql_builder.rb', line 8

def append(*args)
  if args.size == 1
    if args[0].is_a?(Array)
      append_array(args[0])
    elsif args[0].is_a?(SqlBuilder)
      append(args[0].to_a)
    else
      append_string(args[0])
    end
  else
    append_array(args)
  end

  self
end

#or(*args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sql_builder.rb', line 36

def or(*args)
  append_string('or (')

  if block_given?
    yield self
  else
    append(*args)
  end

  append_string(')')
end

#to_aObject



48
49
50
# File 'lib/sql_builder.rb', line 48

def to_a
  [@sql.join(' ')] + @params
end