Class: RDO::Postgres::Array
- Inherits:
-
Array
- Object
- Array
- RDO::Postgres::Array
- 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.
Defined Under Namespace
Classes: Boolean, Bytea, Date, Float, Integer, Numeric, Text, Timestamp, TimestampTZ
Class Method Summary collapse
-
.[](*args) ⇒ Array
Shortcut for the constructor.
-
.parse(str) ⇒ Object
Parse the given PostgreSQL formatted array String into an Array.
Instance Method Summary collapse
-
#format_value(v) ⇒ String
Format an individual element in the Array for building into a String.
-
#initialize(arr = 0) ⇒ Array
constructor
Initialize a new Array, coercing any sub-Arrays to the same type.
-
#parse_value(s) ⇒ Object
Parse an individual element from the array.
-
#to_a ⇒ ::Array
Convert the Array to a standard Ruby Array.
-
#to_s ⇒ String
Convert the Array to the format used by PostgreSQL.
Constructor Details
#initialize(arr = 0) ⇒ Array
Initialize a new Array, coercing any sub-Arrays to the same type.
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.
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.
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.
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.
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_s ⇒ String
Convert the Array to the format used by PostgreSQL.
63 64 65 |
# File 'lib/rdo/postgres/array.rb', line 63 def to_s "{#{map(&method(:format_value_or_null)).join(",")}}" end |