Class: String

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

Instance Method Summary collapse

Instance Method Details

#from_hstoreObject

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



27
28
29
# File 'lib/activerecord-postgres-hstore/string.rb', line 27

def from_hstore
  Hash[ scan(/"([^"]+)"=>"([^"]+)"/) ]
end

#to_hstoreObject

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



5
6
7
# File 'lib/activerecord-postgres-hstore/string.rb', line 5

def to_hstore
  self
end

#valid_hstore?Boolean

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’). I’ll call it a “single quoted hstore format”.

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/activerecord-postgres-hstore/string.rb', line 13

def valid_hstore?
  return true if empty? || self == "''"
  # This is what comes from the database
  dbl_quotes_re = /"([^"]+)"=>"([^"]+)"/
  # TODO
  # This is what comes from the plugin
  # this is a big problem, 'cause regexes does not know how to count...
  # how should i very values quoted with two single quotes? using .+ sux.
  sngl_quotes_re = /'(.+)'=>'(.+)'/
  self.match(dbl_quotes_re) || self.match(sngl_quotes_re)
end