class String

Public Instance Methods

from_hstore() click to toggle source

Creates a hash from a valid double quoted hstore format, ‘cause this is the format that postgresql spits out.

# File lib/padrino-hstore/string.rb, line 21
def from_hstore
  token_pairs = (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 /^"(.*)"$/ then $1.gsub(/\\(.)/, '\1')
      else t.gsub(/\\(.)/, '\1')
      end
    }
  }
  Hash[ token_pairs ]
end
to_hstore() click to toggle source

If the value os a column is already a String and it calls to_hstore, it just returns self. Validation occurs afterwards.

# File lib/padrino-hstore/string.rb, line 5
def to_hstore
  self
end
valid_hstore?() click to toggle source

Validates the hstore format. Valid formats are:

  • An empty string

  • A string like %(“foo”=>“bar”). I’ll call it a “double quoted hstore format”.

  • A string like %(foo=>bar). Postgres doesn’t emit this but it does accept it as input, we should accept any input Postgres does

# File lib/padrino-hstore/string.rb, line 14
def valid_hstore?
  pair = hstore_pair
  !!match(/^\s*(#{pair}\s*(,\s*#{pair})*)?\s*$/)
end

Private Instance Methods

hstore_pair() click to toggle source
# File lib/padrino-hstore/string.rb, line 37
def hstore_pair
  quoted_string = /"[^"\\]*(?:\\.[^"\\]*)*"/
  unquoted_string = /[^\s=,][^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
  string = /(#{quoted_string}|#{unquoted_string})/
  /#{string}\s*=>\s*#{string}/
end