Class: RDO::Postgres::Array

Inherits:
Array
  • Object
show all
Defined in:
lib/rdo/postgres/array.rb

Overview

Utility class used to handle PostgreSQL arrays.

The default implementation assumes only String support. Subclasses override #parse_value and #format_value for typed-arrays.

Examples:

Turn a Ruby Array into a PostgreSQL array String

RDO::Postgres::Array.new(original).to_s
# => {"John Smith","Sarah Doe"}

Turn a PostgreSQL array String into a Ruby Array

RDO::Postgres::Array.parse('{"John Smith","Sarah Doe"}').to_a
# => ["John Smith", "Sarah Doe"]

Direct Known Subclasses

Boolean, Bytea, Date, Float, Integer, Numeric, Text, Timestamp, TimestampTZ

Defined Under Namespace

Classes: Boolean, Bytea, Date, Float, Integer, Numeric, Text, Timestamp, TimestampTZ

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arr = 0) ⇒ Array

Initialize a new Array, coercing any sub-Arrays to the same type.

Parameters:

  • arr (Array) (defaults to: 0)

    the Array to wrap



51
52
53
54
55
56
57
# File 'lib/rdo/postgres/array.rb', line 51

def initialize(arr = 0)
  if ::Array === arr
    super(arr.map{|v| ::Array === v ? self.class.new(v) : v})
  else
    super
  end
end

Class Method Details

.[](*args) ⇒ Array

Shortcut for the constructor.

Parameters:

  • *args (Object...)

    a list of objects to put inside the Array

Returns:

  • (Array)

    a newly initialzed Array



31
32
33
# File 'lib/rdo/postgres/array.rb', line 31

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

.parse(str) ⇒ Object

Parse the given PostgreSQL formatted array String into an Array



42
43
44
# File 'lib/rdo/postgres/array.rb', line 42

def parse(str)
  # defined in ext/rdo_postgres/arrays.c
end

Instance Method Details

#format_value(v) ⇒ String

Format an individual element in the Array for building into a String.

The default implementation wraps quotes around the element.

Parameters:

  • v (Object)

    the Ruby type in the Array

Returns:

  • (String)

    a String used to build the formatted array



84
85
86
# File 'lib/rdo/postgres/array.rb', line 84

def format_value(v)
  %Q{"#{v.to_s.gsub('\\', '\\\\\\\\').gsub('"', '\\\\"')}"}
end

#parse_value(s) ⇒ Object

Parse an individual element from the array.

The default implementation parses as if it were text. Subclasses should override this.

Parameters:

  • s (String)

    the string form of the array element

Returns:

  • (Object)

    the Ruby value



98
99
100
# File 'lib/rdo/postgres/array.rb', line 98

def parse_value(s)
  s[0] == '"' ? s[1...-1].gsub(/\\(.)/, "\\1") : s
end

#to_a::Array

Convert the Array to a standard Ruby Array.

Returns:

  • (::Array)

    a Ruby Array



71
72
73
# File 'lib/rdo/postgres/array.rb', line 71

def to_a
  super.map{|v| Array === v ? v.to_a : v}
end

#to_sString

Convert the Array to the format used by PostgreSQL.

Returns:

  • (String)

    a PostgreSQL array string



63
64
65
# File 'lib/rdo/postgres/array.rb', line 63

def to_s
  "{#{map(&method(:format_value_or_null)).join(",")}}"
end