Class: DataMapper::Property::BCryptHash

Inherits:
String
  • Object
show all
Defined in:
lib/dm-types/bcrypt_hash.rb

Instance Method Summary collapse

Instance Method Details

#dump(value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/dm-types/bcrypt_hash.rb', line 15

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

#load(value) ⇒ Object



11
12
13
# File 'lib/dm-types/bcrypt_hash.rb', line 11

def load(value)
  typecast(value)
end

#typecast(value) ⇒ Object



23
24
25
26
27
28
# File 'lib/dm-types/bcrypt_hash.rb', line 23

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