Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

replace hstore parsing backend with PgHstore

  • Loading branch information...
commit f5c49f0c3dcf806acb7672a72d4266faaeb6c17c 1 parent ab5d306
Seamus Abshere seamusabshere authored
10 CHANGELOG
View
@@ -0,0 +1,10 @@
+unreleased
+
+* Bug fixes
+
+ * Properly quote/escape double quotes https://github.com/engageis/activerecord-postgres-hstore/issues/78
+
+* Enhancements
+
+ * Delegate hstore dumping/loading to https://github.com/seamusabshere/pg-hstore
+ * Add a CHANGELOG!
1  activerecord-postgres-hstore.gemspec
View
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
s.add_dependency "rails"
s.add_dependency "rake"
+ s.add_dependency 'pg-hstore', '>=1.1.5'
s.add_development_dependency "bundler"
s.add_development_dependency "rdoc"
s.add_development_dependency "rspec", "~> 2.11"
32 lib/activerecord-postgres-hstore/coder.rb
View
@@ -1,3 +1,5 @@
+require 'pg_hstore'
+
module ActiveRecord
module Coders
class Hstore
@@ -22,39 +24,13 @@ def load(hstore)
end
private
- # Escapes values such that they will work in an hstore string
- def hstore_escape(str)
- return 'NULL' if str.nil?
- return str if str =~ /^".*"$/
- '"%s"' % str
- end
def to_hstore obj
- return "" if obj.empty?
- obj.map do |idx, val|
- "%s=>%s" % [hstore_escape(idx), hstore_escape(val)]
- end * ","
- end
-
- def hstore_pair
- quoted_string = /"[^"\\]*(?:\\.[^"\\]*)*"/
- unquoted_string = /[^\s=,][^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
- string = /(#{quoted_string}|#{unquoted_string})/
- /#{string}\s*=>\s*#{string}/
+ PgHstore.dump obj, true
end
def from_hstore hstore
- token_pairs = (hstore.scan(hstore_pair)).map { |k,v| [k,v =~ /^NULL$/i ? nil : v] }
- token_pairs = token_pairs.map { |k,v|
- [k,v].map { |t|
- case t
- when nil then t
- when /\A"(.*)"\Z/m then $1.gsub(/\\(.)/, '\1')
- else t.gsub(/\\(.)/, '\1')
- end
- }
- }
- Hash[ token_pairs ]
+ PgHstore.load hstore, false
end
end
end
11 spec/activerecord-coders-hstore_spec.rb
View
@@ -82,6 +82,17 @@
let(:value){ {'a' => 'a'} }
it{ should eql('"a"=>"a"') }
end
+
+ context 'when value has double quotes' do
+ let(:value){ {"a" => "\"a\""} }
+ it{ should eql(%q("a"=>"\"a\"")) }
+ end
+
+ # @seamusabshere not sure about this test
+ # context 'when value has double-escaped double quotes' do
+ # let(:value){ {"a" => "\\\"a\\\""} }
+ # it{ should eql(%q("a"=>"\"a\"")) }
+ # end
end
describe ".load" do
Please sign in to comment.
Something went wrong with that request. Please try again.