Class: DataMapper::Property::HStore

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

Instance Method Summary collapse

Instance Method Details

#dump(hash) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
# File 'lib/dm-hstore/property.rb', line 24

def dump(hash)
  return "" if hash.nil?
  raise ArgumentError, 'invalid format' unless hash.is_a? Hash
  hash.map do |key, value|
    value = value.nil? ? "NULL" : "\"#{value}\""
    "\"#{key}\"=>#{value}"
  end.join(',')
end

#load(str) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dm-hstore/property.rb', line 9

def load(str)
  return {} if str.nil?
  hash = Hash[*str.to_s.split(',').map do |item|
    if item.strip =~ /\A\"(.*)\"[ ]*=>[ ]*(.*)\z/
      key = $1.to_sym
      value = $2 =~ /\"(.*)\"/ ? $1 : nil
      [key, value]
    else
      raise ArgumentError, 'invalid format'
    end
  end.flatten]
  hash = hash.with_indifferent_access if hash.respond_to? :with_indifferent_access
  hash
end