Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-postgres-array/array.rb

Instance Method Summary collapse

Instance Method Details

#from_postgres_array(base_type = :string) ⇒ Object

If the method from_postgres_array is called in an Array, it just returns self.



27
28
29
# File 'lib/activerecord-postgres-array/array.rb', line 27

def from_postgres_array(base_type = :string)
  self
end

#to_postgres_array(omit_quotes = false) ⇒ Object

Generates a single quoted postgres array string format. This is the format used to insert or update stuff in the database.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/activerecord-postgres-array/array.rb', line 4

def to_postgres_array(omit_quotes = false)
  result = "#{omit_quotes ? '' : "'" }{"

  result << collect do |value|
    if value.is_a?(Array)
      value.to_postgres_array(true)
    elsif value.is_a?(String)
      value = value.gsub(/\\/, '\&\&')
      value = value.gsub(/'/, "''")
      value = value.gsub(/"/, '\"')
      value = "\"#{ value }\""
      value
    elsif value.is_a?(NilClass)
      value = 'NULL'
    else
      value
    end
  end.join(",")

  result << "}#{omit_quotes ? '' : "'" }"
end