Module: ArJdbc::PostgreSQL::Column::Cast

Defined in:
lib/arjdbc/postgresql/column.rb

Overview

Note:

Based on active_record/connection_adapters/postgresql/cast.rb (4.0).

Instance Method Summary collapse

Instance Method Details

#array_to_string(value, column, adapter, should_be_quoted = false) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/arjdbc/postgresql/column.rb', line 372

def array_to_string(value, column, adapter, should_be_quoted = false)
  casted_values = value.map do |val|
    if String === val
      if val == "NULL"
        "\"#{val}\""
      else
        quote_and_escape(adapter.type_cast(val, column, true))
      end
    else
      adapter.type_cast(val, column, true)
    end
  end
  "{#{casted_values.join(',')}}"
end

#cidr_to_string(object) ⇒ Object



411
412
413
414
415
416
417
# File 'lib/arjdbc/postgresql/column.rb', line 411

def cidr_to_string(object)
  if IPAddr === object
    "#{object.to_s}/#{object.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
  else
    object
  end
end

#hstore_to_string(object) ⇒ Object



342
343
344
345
346
347
348
# File 'lib/arjdbc/postgresql/column.rb', line 342

def hstore_to_string(object)
  if Hash === object
    object.map { |k,v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(',')
  else
    object
  end
end

#json_to_string(object) ⇒ Object



364
365
366
367
368
369
370
# File 'lib/arjdbc/postgresql/column.rb', line 364

def json_to_string(object)
  if Hash === object
    ActiveSupport::JSON.encode(object)
  else
    object
  end
end

#point_to_string(point) ⇒ Object



302
303
304
# File 'lib/arjdbc/postgresql/column.rb', line 302

def point_to_string(point)
  "(#{point[0]},#{point[1]})"
end

#range_to_string(object) ⇒ Object



387
388
389
390
391
# File 'lib/arjdbc/postgresql/column.rb', line 387

def range_to_string(object)
  from = object.begin.respond_to?(:infinite?) && object.begin.infinite? ? '' : object.begin
  to   = object.end.respond_to?(:infinite?) && object.end.infinite? ? '' : object.end
  "[#{from},#{to}#{object.exclude_end? ? ')' : ']'}"
end

#string_to_bit(value) ⇒ Object



326
327
328
329
330
331
# File 'lib/arjdbc/postgresql/column.rb', line 326

def string_to_bit(value)
  case value
  when /^[01]*$/      then value             # Bit-string notation
  when /^[0-9A-F]*$/i then value.hex.to_s(2) # Hexadecimal notation
  end
end

#string_to_cidr(string) ⇒ Object



401
402
403
404
405
406
407
408
409
# File 'lib/arjdbc/postgresql/column.rb', line 401

def string_to_cidr(string)
  if string.nil?
    nil
  elsif String === string
    IPAddr.new(string)
  else
    string
  end
end

#string_to_hstore(string) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/arjdbc/postgresql/column.rb', line 350

def string_to_hstore(string)
  if string.nil?
    nil
  elsif String === string
    Hash[string.scan(HstorePair).map { |k,v|
      v = v.upcase == 'NULL' ? nil : v.gsub(/^"(.*)"$/,'\1').gsub(/\\(.)/, '\1')
      k = k.gsub(/^"(.*)"$/,'\1').gsub(/\\(.)/, '\1')
      [k,v]
    }]
  else
    string
  end
end

#string_to_json(string) ⇒ Object



393
394
395
396
397
398
399
# File 'lib/arjdbc/postgresql/column.rb', line 393

def string_to_json(string)
  if String === string
    ActiveSupport::JSON.decode(string)
  else
    string
  end
end

#string_to_point(string) ⇒ Object



306
307
308
309
310
311
# File 'lib/arjdbc/postgresql/column.rb', line 306

def string_to_point(string)
  if string[0] == '(' && string[-1] == ')'
    string = string[1...-1]
  end
  string.split(',').map { |v| Float(v) }
end

#string_to_time(string) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/arjdbc/postgresql/column.rb', line 313

def string_to_time(string)
  return string unless String === string

  case string
  when  'infinity' then  1.0 / 0.0
  when '-infinity' then -1.0 / 0.0
  when / BC$/
    super("-" + string.sub(/ BC$/, ""))
  else
    super
  end
end