Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord2-hstore/string.rb

Instance Method Summary collapse

Instance Method Details

#from_hstoreObject

Generates a hash from an hstore string format.

Original implementation from: github.com/softa/activerecord-postgres-hstore/blob/master/lib/activerecord-postgres-hstore/string.rb



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/activerecord2-hstore/string.rb', line 6

def from_hstore
  quoted_string = /"[^"\\]*(?:\\.[^"\\]*)*"/
  unquoted_string = /[^\s=,][^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
  string = /(#{quoted_string}|#{unquoted_string})/
  hstore_pair = /#{string}\s*=>\s*#{string}/

  token_pairs = (scan(hstore_pair)).map { |key, value| [key, value =~ /^NULL$/i ? nil : value] }
  token_pairs = token_pairs.map { |key, value|
    [key, value].map { |element| 
      case element
      when nil then element
      when /^"(.*)"$/ then $1.gsub(/\\(.)/, '\1')
      else element.gsub(/\\(.)/, '\1')
      end
    }
  }
  Hash[ token_pairs ]
end