Skip to content

Instantly share code, notes, and snippets.

@sp3c73r2038
Created April 5, 2014 14:42
Show Gist options
  • Save sp3c73r2038/9992866 to your computer and use it in GitHub Desktop.
Save sp3c73r2038/9992866 to your computer and use it in GitHub Desktop.
sequel example usage
# -*- encoding: binary -*-
require 'sequel'
require 'sqlite3'
Bundler.require(:default, :development)
DB = Sequel.connect("sqlite:///tmp/test.db")
# DB.create_table(:users) do
# primary_key :id
# String :username, :null => false
# String :email, :null => false
# String :password_hash, :null => false
# String :salt, :null => false
# end
class User < Sequel::Model
attr_accessor :password, :password_confirmation
plugin :validation_class_methods
plugin :validation_helpers
validates do
length_of :username, :maxium => 1
confirmation_of :password
end
def before_create
self.salt = SecureRandom.uuid.gsub("-", "")[1..8]
d = Digest::SHA1.new
d.update(self.password + self.salt)
self.password_hash = d.hexdigest
super
end
end
u = User.new
u.username = 'foobar'
u.email = '[email protected]'
u.password = 'pass'
u.password_confirmation = 'pass'
u.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment