Sha256: 77325bccdaaa147798af2516e34aa34fbb36fbd960ff5dad565d8e8030a188e3

Contents?: true

Size: 815 Bytes

Versions: 1

Compression:

Stored size: 815 Bytes

Contents

require 'json'

module DataMapper
  module Types
    class Json < DataMapper::Type
      primitive Text

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

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

      def self.typecast(value, property)
        if value.nil? || value.kind_of?(Array) || value.kind_of?(Hash)
          value
        else
          ::JSON.load(value.to_s)
        end
      end
    end # class Json
    JSON = Json
  end # module Types
end # module DataMapper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dm-types-0.10.2 lib/dm-types/json.rb