Class: DataMapper::Property::HStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-pg-types/hstore.rb

Instance Method Summary collapse

Instance Method Details

#dump(value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dm-pg-types/hstore.rb', line 13

def dump(value)
  return "" unless value
  
  # following code lifted from https://github.com/softa/activerecord-postgres-hstore/blob/master/lib/activerecord-postgres-hstore/hash.rb

  
  value.map { |idx, val| 
    iv = [idx,val].map { |_| 
      e = _.to_s.gsub(/"/, '\"')
      if _.nil?
        'NULL'
      elsif e =~ /[,\s=>]/ || e.empty?
        '"%s"' % e
      else
        e
      end
    }
    
    "%s => %s" % iv
  } * ","      
end

#load(value) ⇒ Object



7
8
9
10
11
# File 'lib/dm-pg-types/hstore.rb', line 7

def load(value)
  return nil unless value
  rv = value.split(",").map{|i| i.split("=>").map{|x| x.gsub('"','').strip}}
  Hash[*(rv.map{|k,v| [k, v == "NULL" ? nil : v]}.flatten)]
end