Module: Postgres::Conversion
- Included in:
- TC_Conversion
- Defined in:
- lib/postgres-pr/typeconv/conv.rb,
lib/postgres-pr/typeconv/array.rb,
lib/postgres-pr/typeconv/bytea.rb
Defined Under Namespace
Classes: ConversionError
Instance Method Summary collapse
- #decode_array(str, delim = ',', &conv_proc) ⇒ Object
-
#decode_bytea(str) ⇒ Object
Decodes a bytea encoded string.
-
#encode_bytea(str) ⇒ Object
Encodes a string as bytea value.
Instance Method Details
#decode_array(str, delim = ',', &conv_proc) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/postgres-pr/typeconv/array.rb', line 5 def decode_array(str, delim=',', &conv_proc) delim = Regexp.escape(delim) buf = StringScanner.new(str) return parse_arr(buf, delim, &conv_proc) ensure raise ConversionError, "end of string expected (#{buf.rest})" unless buf.empty? end |
#decode_bytea(str) ⇒ Object
Decodes a bytea encoded string.
for decoding rules see:
http://www.postgresql.org/docs/7.4/static/datatype-binary.html
22 23 24 25 26 |
# File 'lib/postgres-pr/typeconv/bytea.rb', line 22 def decode_bytea(str) str.gsub(/\\(\\|'|[0-3][0-7][0-7])/) {|s| if s.size == 2 then s[1,1] else s[1,3].oct.chr end } end |
#encode_bytea(str) ⇒ Object
Encodes a string as bytea value.
for encoding rules see:
http://www.postgresql.org/docs/7.4/static/datatype-binary.html
12 13 14 |
# File 'lib/postgres-pr/typeconv/bytea.rb', line 12 def encode_bytea(str) str.gsub(/[\000-\037\047\134\177-\377]/) {|b| "\\#{ b[0].to_s(8).rjust(3, '0') }" } end |