Sha256: 6bb53504d2d56e9974bdf8e184e3da970b00086a11f497eaf35958fa854505a3

Contents?: true

Size: 925 Bytes

Versions: 2

Compression:

Stored size: 925 Bytes

Contents

require 'dm-core'
require 'json' unless defined? JSON

module DataMapper
  class Property
    class Json < Text

      def custom?
        true
      end

      def primitive?(value)
        value.kind_of?(::Array) || value.kind_of?(::Hash)
      end

      def valid?(value, negated = false)
        super || dump(value).kind_of?(::String)
      end

      def load(value)
        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 dump(value)
        if value.nil? || value.is_a?(::String)
          value
        else
          ::JSON.dump(value)
        end
      end

      def typecast_to_primitive(value)
        ::JSON.load(value.to_s)
      end

    end # class Json

    JSON = Json

  end # class Property
end # module DataMapper

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dm-types-1.1.0 lib/dm-types/json.rb
dm-types-1.1.0.rc3 lib/dm-types/json.rb