pg-hstore 1.1.0 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/pg_hstore.rb +24 -9
  2. metadata +2 -2
data/lib/pg_hstore.rb CHANGED
@@ -1,13 +1,16 @@
1
1
  require 'strscan'
2
2
 
3
3
  module PgHstore
4
+ SINGLE_QUOTE = "'"
5
+ DOUBLE_QUOTE = '"'
6
+
4
7
  extend self
5
8
 
6
9
  def parse(string)
7
10
  hash = {}
8
11
 
9
12
  # remove single quotes around literal if necessary
10
- string = string[1..-2] if string[0] == "'" and string[-1] == "'"
13
+ string = string[1..-2] if string[0] == SINGLE_QUOTE and string[-1] == SINGLE_QUOTE
11
14
 
12
15
  scanner = StringScanner.new(string)
13
16
  while !scanner.eos?
@@ -23,21 +26,34 @@ module PgHstore
23
26
  hash
24
27
  end
25
28
 
26
- def dump(hash)
27
- string = hash.map do |(k,v)|
29
+ # set for_parameter = true if you're using the output for a bind variable
30
+ def dump(hash, for_parameter = false)
31
+ string = hash.map do |k, v|
28
32
  if v.nil?
29
33
  # represent nil as NULL without quotes
30
34
  v = "NULL"
31
35
  else
36
+ v = v.to_s
37
+ unless for_parameter
38
+ v = to_s_escaped v
39
+ end
32
40
  # otherwise, write a double-quoted string with backslash escapes for quotes
33
- v = to_s_escaped(v)
34
- v = "\"#{v}\""
41
+ v = DOUBLE_QUOTE + v + DOUBLE_QUOTE
42
+ end
43
+ k = k.to_s
44
+ unless for_parameter
45
+ k = to_s_escaped k
35
46
  end
47
+ k = DOUBLE_QUOTE + k + DOUBLE_QUOTE
36
48
 
37
49
  # TODO: throw an error if there is a NULL key
38
- "\"#{to_s_escaped(k)}\" => #{v}"
50
+ [k, v].join ' => '
39
51
  end.join(", ")
40
- "'#{string}'"
52
+ if for_parameter
53
+ string
54
+ else
55
+ SINGLE_QUOTE + string + SINGLE_QUOTE
56
+ end
41
57
  end
42
58
 
43
59
  private
@@ -67,9 +83,8 @@ module PgHstore
67
83
  scanner.skip(/,\s*/)
68
84
  end
69
85
 
70
-
71
86
  def to_s_escaped(str)
72
- str.to_s.gsub(/\\(?!")/) {'\\\\'}.gsub(/"/, '\"').gsub(/'/, "''")
87
+ str.gsub(/\\(?!")/) {'\\\\'}.gsub(DOUBLE_QUOTE, '\"').gsub(SINGLE_QUOTE, "''")
73
88
  end
74
89
 
75
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg-hstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-11-10 00:00:00.000000000 Z
13
+ date: 2012-12-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec