Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/activerecord_postgres_hstore_core/hash.rb
Constant Summary collapse
- HSTORE_ESCAPED =
/[,\s=>\\]/
Instance Method Summary collapse
-
#from_hstore ⇒ Object
If the method from_hstore is called in a Hash, it just returns self.
-
#hstore_escape(str) ⇒ Object
Escapes values such that they will work in an hstore string.
-
#to_hstore ⇒ Object
Generates an hstore string format.
Instance Method Details
#from_hstore ⇒ Object
If the method from_hstore is called in a Hash, it just returns self.
35 36 37 |
# File 'lib/activerecord_postgres_hstore_core/hash.rb', line 35 def from_hstore self end |
#hstore_escape(str) ⇒ Object
Escapes values such that they will work in an hstore string
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/activerecord_postgres_hstore_core/hash.rb', line 5 def hstore_escape(str) if str.nil? return 'NULL' end str = str.to_s.dup # backslash is an escape character for strings, and an escape character for gsub, so you need 6 backslashes to get 2 in the output. # see http://stackoverflow.com/questions/1542214/weird-backslash-substitution-in-ruby for the gory details str.gsub!(/\\/, '\\\\\\') # escape backslashes before injecting more backslashes str.gsub!(/"/, '\"') if str =~ HSTORE_ESCAPED or str.empty? str = '"%s"' % str end return str end |
#to_hstore ⇒ Object
Generates an hstore string format. This is the format used to insert or update stuff in the database.
26 27 28 29 30 31 32 |
# File 'lib/activerecord_postgres_hstore_core/hash.rb', line 26 def to_hstore return "" if empty? map do |idx, val| "%s=>%s" % [hstore_escape(idx), hstore_escape(val)] end * "," end |