Sha256: 4beba9a5464e0fdb5b1349c98db0a92a96e559da154d7680aac97310d01c232c

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# encoding: utf-8

require 'dm-core'

module DataMapper
  class Property
    class PgHStore < Object
      def load(value)
        return nil unless value
        values = value.split(",")
        values.map! { |val| unescape_pg_hash(val) }
        values.map! { |key, val| [key, unescape_nil(val)] }
        Hash[*(values.flatten)]
      end

      def dump(value)
        return "" unless value
        value.map! do |idx, val|
          [escape_double_quote(idx), escape_value(val)].join(",")
        end
      end

      private

      def escape_nil(value)
        (value.nil?) ? 'NULL' : value
      end

      def unescape_nil(value)
        (value == 'NULL') ? nil : value
      end

      def escape_double_quote(value)
        value.gsub!(/"/, '\"')
        value
      end

      def unescape_double_quote(value)
        value.gsub!('"','')
        value.strip!
        value
      end

      def escape_pg_hash(value)
        (value =~ /[,\s=>]/ || value.empty?) ? %Q{"#{value}"} : value
      end

      def unescape_pg_hash(value)
        values = value.split("=>")
        values.map! { |val| unescape_double_quote(val) }
        values
      end

      def escape_value(value)
        escape_double_quote(escape_nil(escape_pg_hash(value)))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dm-postgres-types-0.0.2 lib/dm-postgres-types/property/pg_hstore.rb
dm-postgres-types-0.0.1 lib/dm-postgres-types/property/pg_hstore.rb