Sha256: f406ed37b5e4e83183d97a45e465a1b9810ccdc41dc395c75573cc5385e7a5e6

Contents?: true

Size: 896 Bytes

Versions: 7

Compression:

Stored size: 896 Bytes

Contents

require 'json'

module DataMapper
  module Types
    class Json < DataMapper::Type
      primitive String
      size 65535
      lazy true

      def self.load(value, property)
        if value.nil?
          nil
        elsif value.is_a?(String)
          ::JSON.load(value)
        else
          raise ArgumentError.new("+value+ must be nil or a String")
        end
      end

      def self.dump(value, property)
        if value.nil?
          nil
        elsif value.is_a?(String)
          value
        else
          ::JSON.dump(value)
        end
      end

      def self.typecast(value, property)
        # Arrays and hashes are left alone, while strings are parsed as JSON.
        if value.kind_of?(Array) || value.kind_of?(Hash)
          value
        else
          ::JSON.load(value.to_s)
        end
      end
    end # class Json
  end # module Types
end # module DataMapper

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
dm-types-0.9.4 lib/dm-types/json.rb
dm-types-0.9.6 lib/dm-types/json.rb
dm-types-0.9.5 lib/dm-types/json.rb
dm-types-0.9.3 lib/dm-types/json.rb
dm-types-0.9.8 lib/dm-types/json.rb
dm-types-0.9.7 lib/dm-types/json.rb
mack-data_mapper-0.8.2 lib/gems/dm-types-0.9.7/lib/dm-types/json.rb