require 'dm-core'
require 'bcrypt'

module DataMapper
  class Property
    class BCryptHash < String
      include PassThroughLoadDump

      length 60

      def typecast(value)
        return value if value.nil? || value.kind_of?(BCrypt::Password)
        BCrypt::Password.new(value)
      rescue BCrypt::Errors::InvalidHash
        BCrypt::Password.create(value)
      end

      def dump(value)
        hash = typecast(value)
        hash.to_s if hash
      end

    end # class BCryptHash
  end # class Property
end # module DataMapper

require 'dm-core'
require 'bcrypt'

module DataMapper
  class Property
    class BCryptHash < String
      include PassThroughLoadDump

      length 60

      def typecast(value)
        return value if value.nil? || value.kind_of?(BCrypt::Password)
        BCrypt::Password.new(value)
      rescue BCrypt::Errors::InvalidHash
        BCrypt::Password.create(value)
      end

      def dump(value)
        hash = typecast(value)
        return if hash.nil?
        hash_string = hash.to_s
        hash_string.encode!('UTF-8') if hash_string.respond_to?(:encode!)
        hash_string
      end

    end # class BCryptHash
  end # class Property
end # module DataMapper

Change bcrypt type to dump a utf-8 string if possible
Change bcrypt type to dump a utf-8 string if possible

* This is required by the yaml adapter to properly serialize the
  string into something that can be serialized as a string
  primitive and not a binary string, which is not supported by
  safe_yaml at the moment.
