Guest User

Untitled

a guest
Jul 11th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # encoding: utf-8
  3.  
  4. require 'sequel'
  5.  
  6. # Database Manager Class
  7. # @function: Initializing and Manipulating the Models and the DB Connection.
  8. class DatabaseManager
  9.  
  10. def initialize
  11. self.database = Sequel.mysql 'sinatra', :user => 'sryche',
  12. :password => '**********************', :host => 'localhost'
  13. end
  14. end
  15.  
  16. # User Model Class
  17. # @function: Helps on User Management on Database.
  18. class Users
  19.  
  20. def initialize(db)
  21. @db = db.database
  22. unless @db.table_exists?(:users)
  23. @db.create_table :users do
  24. primary_key :id
  25. String :username
  26. String :password
  27. Integer :role
  28. end
  29. end
  30. @users = @db[:users]
  31. end
  32.  
  33. def insert(values)
  34. @users.insert(values)
  35. end
  36.  
  37. def print_users
  38. p @users.count
  39. end
  40. end
  41.  
  42.  
  43. # Posts Model Class
  44. # @function: Helps on Post Management on Database.
  45. class Posts
  46.  
  47. def initialize(db)
  48. @db = db
  49. unless @db.table_exists?(:posts)
  50. @db.create_table :posts do
  51. primary_key :id
  52. String :title
  53. String :body
  54. String :tags
  55. Integer :category_id
  56. Integer :user_id
  57. end
  58. end
  59.  
  60. @users = @db[:posts]
  61. end
  62. end
Add Comment
Please, Sign In to add comment