Class: HomuraSqlBuffer

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

Overview

A mutable string-like buffer used by Sequel’s SQL generator so it can ‘buf << ’SELECT ‘` without hitting Opal’s immutable-string NotImplementedError. Exposes the subset of String methods that Sequel actually calls on the sql accumulator.

freeze / frozen? follow Ruby semantics: after ‘freeze` returns, every mutating method (`<<`, `concat`, `sub`, `gsub`, `chomp!`, `slice!`, `clear`) raises FrozenError, matching what native String does. This avoids silent mutation bugs if Sequel (or a future patch) freezes a cached SQL buffer.

Instance Method Summary collapse

Constructor Details

#initialize(init = '') ⇒ HomuraSqlBuffer

Returns a new instance of HomuraSqlBuffer.



200
201
202
203
# File 'lib/sequel_opal_patches.rb', line 200

def initialize(init = '')
  @chunks = init.empty? ? [] : [init.to_s]
  @frozen = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



318
319
320
321
322
323
324
325
# File 'lib/sequel_opal_patches.rb', line 318

def method_missing(name, *args, &block)
  s = to_s
  if s.respond_to?(name)
    s.public_send(name, *args, &block)
  else
    super
  end
end

Instance Method Details

#<<(other) ⇒ Object Also known as: concat



205
206
207
208
209
# File 'lib/sequel_opal_patches.rb', line 205

def <<(other)
  ensure_not_frozen!
  @chunks << other.to_s
  self
end

#==(other) ⇒ Object



230
231
232
# File 'lib/sequel_opal_patches.rb', line 230

def ==(other)
  to_s == other.to_s
end

#[](*args) ⇒ Object



226
227
228
# File 'lib/sequel_opal_patches.rb', line 226

def [](*args)
  to_s[*args]
end

#chomp!(*args) ⇒ Object



264
265
266
267
268
269
# File 'lib/sequel_opal_patches.rb', line 264

def chomp!(*args)
  ensure_not_frozen!
  s = to_s.chomp(*args)
  @chunks = [s]
  self
end

#clearObject



279
280
281
282
283
# File 'lib/sequel_opal_patches.rb', line 279

def clear
  ensure_not_frozen!
  @chunks = []
  self
end

#dupObject



294
295
296
# File 'lib/sequel_opal_patches.rb', line 294

def dup
  ::HomuraSqlBuffer.new(to_s)
end

#empty?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/sequel_opal_patches.rb', line 222

def empty?
  @chunks.empty? || @chunks.all? { |c| c.to_s.empty? }
end

#end_with?(*args) ⇒ Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/sequel_opal_patches.rb', line 256

def end_with?(*args)
  to_s.end_with?(*args)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


306
307
308
# File 'lib/sequel_opal_patches.rb', line 306

def eql?(other)
  to_s.eql?(other.respond_to?(:to_s) ? other.to_s : other)
end

#freezeObject



285
286
287
288
# File 'lib/sequel_opal_patches.rb', line 285

def freeze
  @frozen = true
  self
end

#frozen?Boolean

Returns:

  • (Boolean)


290
291
292
# File 'lib/sequel_opal_patches.rb', line 290

def frozen?
  !!@frozen
end

#gsub(*args, &block) ⇒ Object



245
246
247
248
249
250
# File 'lib/sequel_opal_patches.rb', line 245

def gsub(*args, &block)
  ensure_not_frozen!
  s = to_s.gsub(*args, &block)
  @chunks = [s]
  self
end

#hashObject



302
303
304
# File 'lib/sequel_opal_patches.rb', line 302

def hash
  to_s.hash
end

#include?(other) ⇒ Boolean

Returns:

  • (Boolean)


252
253
254
# File 'lib/sequel_opal_patches.rb', line 252

def include?(other)
  to_s.include?(other.to_s)
end

#inspectObject



298
299
300
# File 'lib/sequel_opal_patches.rb', line 298

def inspect
  to_s.inspect
end

#lengthObject Also known as: size



217
218
219
# File 'lib/sequel_opal_patches.rb', line 217

def length
  to_s.length
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


327
328
329
# File 'lib/sequel_opal_patches.rb', line 327

def respond_to_missing?(name, include_private = false)
  ''.respond_to?(name, include_private) || super
end

#slice!(*args) ⇒ Object



271
272
273
274
275
276
277
# File 'lib/sequel_opal_patches.rb', line 271

def slice!(*args)
  ensure_not_frozen!
  s = to_s
  removed = s.slice!(*args)
  @chunks = [s]
  removed
end

#start_with?(*args) ⇒ Boolean

Returns:

  • (Boolean)


260
261
262
# File 'lib/sequel_opal_patches.rb', line 260

def start_with?(*args)
  to_s.start_with?(*args)
end

#sub(*args, &block) ⇒ Object

Return Buffer instead of raw String so subsequent ‘<<` chains don’t land on Opal’s immutable String. Callers that compare against String literals still work because of ‘==` (delegates to `to_s`).



238
239
240
241
242
243
# File 'lib/sequel_opal_patches.rb', line 238

def sub(*args, &block)
  ensure_not_frozen!
  s = to_s.sub(*args, &block)
  @chunks = [s]
  self
end

#to_sObject Also known as: to_str



212
213
214
# File 'lib/sequel_opal_patches.rb', line 212

def to_s
  @chunks.join
end