Class: String

Inherits:
Object show all
Includes:
Sequel::SQL::ColumnMethods
Defined in:
lib/sequel_core/core_ext.rb,
lib/sequel_core/core_sql.rb

Direct Known Subclasses

Sequel::LiteralString

Constant Summary

Constants included from Sequel::SQL::ColumnMethods

Sequel::SQL::ColumnMethods::AS, Sequel::SQL::ColumnMethods::ASC, Sequel::SQL::ColumnMethods::DESC

Instance Method Summary collapse

Methods included from Sequel::SQL::ColumnMethods

#as, #asc, #cast_as, #desc

Instance Method Details

#blank?Boolean

Strings are blank if they are empty or include only whitespace

Returns:

  • (Boolean)


164
165
166
# File 'lib/sequel_core/core_ext.rb', line 164

def blank?
  strip.empty?
end

#litObject Also known as: expr

Converts a string into an LiteralString, in order to override string literalization, e.g.:

DB[:items].filter(:abc => 'def').sql #=>
  "SELECT * FROM items WHERE (abc = 'def')"

DB[:items].filter(:abc => 'def'.lit).sql #=>
  "SELECT * FROM items WHERE (abc = def)"


115
116
117
# File 'lib/sequel_core/core_sql.rb', line 115

def lit
  Sequel::LiteralString.new(self)
end

#split_sqlObject

Splits a string into separate SQL statements, removing comments and excessive white-space.



122
123
124
# File 'lib/sequel_core/core_sql.rb', line 122

def split_sql
  to_sql.split(';').map {|s| s.strip}
end

#to_dateObject

Converts a string into a Date object.



169
170
171
172
173
174
175
# File 'lib/sequel_core/core_ext.rb', line 169

def to_date
  begin
    Date.parse(self)
  rescue => e
    raise Sequel::Error::InvalidValue, "Invalid date value '#{self}' (#{e.message})"
  end
end

#to_datetimeObject

Converts a string into a DateTime object.



178
179
180
181
182
183
184
# File 'lib/sequel_core/core_ext.rb', line 178

def to_datetime
  begin
    DateTime.parse(self)
  rescue => e
    raise Sequel::Error::InvalidValue, "Invalid date value '#{self}' (#{e.message})"
  end
end

#to_sequel_timeObject

Converts a string into a Time or DateTime object, depending on the value of Sequel.time_class



188
189
190
191
192
193
194
# File 'lib/sequel_core/core_ext.rb', line 188

def to_sequel_time
  begin
    Sequel.time_class.parse(self)
  rescue => e
    raise Sequel::Error::InvalidValue, "Invalid time value '#{self}' (#{e.message})"
  end
end

#to_sqlObject

Converts a string into an SQL string by removing comments. See also Array#to_sql.



128
129
130
# File 'lib/sequel_core/core_sql.rb', line 128

def to_sql
  split("\n").to_sql
end

#to_timeObject

Converts a string into a Time object.



197
198
199
200
201
202
203
# File 'lib/sequel_core/core_ext.rb', line 197

def to_time
  begin
    Time.parse(self)
  rescue => e
    raise Sequel::Error::InvalidValue, "Invalid time value '#{self}' (#{e.message})"
  end
end