Class: Seaquel::Bit

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

Overview

Represents a small bit of an SQL expression.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, precedence) ⇒ Bit

Returns a new instance of Bit.



11
12
13
14
# File 'lib/seaquel/bit.rb', line 11

def initialize str, precedence
  @str = str
  @precedence = precedence
end

Instance Attribute Details

#precedenceObject (readonly)

Returns the value of attribute precedence.



9
10
11
# File 'lib/seaquel/bit.rb', line 9

def precedence
  @precedence
end

#strObject (readonly)

Returns the value of attribute str.



8
9
10
# File 'lib/seaquel/bit.rb', line 8

def str
  @str
end

Instance Method Details

#==(other) ⇒ Object



34
35
36
37
38
# File 'lib/seaquel/bit.rb', line 34

def == other
  return toplevel == other if other.kind_of?(String)

  super
end

#at(target_precedence) ⇒ Object

Returns a string at given target_precedence. If the precedence of this bit is lower than target_precedence, it will not be put in parenthesis.



19
20
21
22
23
24
25
# File 'lib/seaquel/bit.rb', line 19

def at target_precedence
  if precedence == :inf || target_precedence <= precedence
    @str
  else
    "(#{@str})"
  end
end

#toplevelObject

Returns the SQL that you would insert toplevel, meaning at a level where parens aren’t needed anymore.



30
31
32
# File 'lib/seaquel/bit.rb', line 30

def toplevel
  str
end