Class: String

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

Direct Known Subclasses

Sequel::LiteralString, Sequel::SQL::Blob

Instance Method Summary collapse

Methods included from Sequel::SQL::CastMethods

#cast, #cast_numeric, #cast_string

Methods included from Sequel::SQL::AliasMethods

#as

Instance Method Details

#blank?Boolean

Strings are blank if they are empty or include only whitespace

Returns:

  • (Boolean)


150
151
152
# File 'lib/sequel_core/core_ext.rb', line 150

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)"


126
127
128
# File 'lib/sequel_core/core_sql.rb', line 126

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

#split_sqlObject

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



133
134
135
# File 'lib/sequel_core/core_sql.rb', line 133

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

#to_blobObject

Returns a Blob that holds the same data as this string. Blobs provide proper escaping of binary data.



145
146
147
# File 'lib/sequel_core/core_sql.rb', line 145

def to_blob
  ::Sequel::SQL::Blob.new self
end

#to_dateObject

Converts a string into a Date object.



155
156
157
158
159
160
161
# File 'lib/sequel_core/core_ext.rb', line 155

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.



164
165
166
167
168
169
170
# File 'lib/sequel_core/core_ext.rb', line 164

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.datetime_class



174
175
176
177
178
179
180
# File 'lib/sequel_core/core_ext.rb', line 174

def to_sequel_time
  begin
    Sequel.datetime_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.



139
140
141
# File 'lib/sequel_core/core_sql.rb', line 139

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

#to_timeObject

Converts a string into a Time object.



183
184
185
186
187
188
189
# File 'lib/sequel_core/core_ext.rb', line 183

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