webLabLibrary 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/webLabLibrary/library_management.rb +151 -0
- data/lib/webLabLibrary/person_system.rb +76 -0
- data/lib/webLabLibrary/rails_cheat_sheet.rb +296 -0
- data/lib/webLabLibrary/rails_procedure.rb +134 -0
- data/lib/webLabLibrary/shopping_cart.rb +30 -0
- data/lib/webLabLibrary/student_marks.rb +44 -0
- data/lib/webLabLibrary/vehicle_management.rb +61 -0
- data/lib/webLabLibrary/vending_machine.rb +110 -0
- data/lib/webLabLibrary/version.rb +5 -0
- data/lib/webLabLibrary.rb +9 -0
- metadata +47 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 46960f9abf6ef0eb0179eea394e0eff225958d4abd9a706d5ddedb2674b8920b
|
|
4
|
+
data.tar.gz: 710d6853e668dcf2d00b895e2202896b9c8bec97b3e11511c4ba8ebb2e2d2257
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5ad21afae23a4b4c7671d3b9a37eb8b722981b77565472b11c7ade5103f89c5c516f601829c4578f249a07dabec41655f4bbfd13c729e05747d17741a3529e47
|
|
7
|
+
data.tar.gz: 4789ac2eb04e0a665c16f4930f4e2aebe49ac932fa853736f954434f292e26d98959a7f9874f1430e211680996e9dd53ee854b6807d7f742fc4170867e15fe3b
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# library_management_system.rb
|
|
2
|
+
|
|
3
|
+
class Library
|
|
4
|
+
def initialize
|
|
5
|
+
@books = {
|
|
6
|
+
"978-0143127741" => { title: "The Alchemist", author: "Paulo Coelho", copies: 5 },
|
|
7
|
+
"978-0062315007" => { title: "Sapiens", author: "Yuval Noah Harari", copies: 3 },
|
|
8
|
+
"978-0451524935" => { title: "1984", author: "George Orwell", copies: 4 }
|
|
9
|
+
}
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Add a new book
|
|
13
|
+
def add_book
|
|
14
|
+
print "Enter ISBN: "
|
|
15
|
+
isbn = gets.chomp
|
|
16
|
+
|
|
17
|
+
if @books[isbn]
|
|
18
|
+
puts "Book already exists!"
|
|
19
|
+
return
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
print "Enter title: "
|
|
23
|
+
title = gets.chomp
|
|
24
|
+
print "Enter author: "
|
|
25
|
+
author = gets.chomp
|
|
26
|
+
print "Enter number of copies: "
|
|
27
|
+
copies = gets.to_i
|
|
28
|
+
|
|
29
|
+
@books[isbn] = { title: title, author: author, copies: copies }
|
|
30
|
+
puts "Book added successfully!"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Update copies
|
|
34
|
+
def update_copies
|
|
35
|
+
print "Enter ISBN: "
|
|
36
|
+
isbn = gets.chomp
|
|
37
|
+
|
|
38
|
+
if @books[isbn]
|
|
39
|
+
print "Enter new number of copies: "
|
|
40
|
+
@books[isbn][:copies] = gets.to_i
|
|
41
|
+
puts "Copies updated!"
|
|
42
|
+
else
|
|
43
|
+
puts "Book not found!"
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Remove book
|
|
48
|
+
def remove_book
|
|
49
|
+
print "Enter ISBN: "
|
|
50
|
+
isbn = gets.chomp
|
|
51
|
+
|
|
52
|
+
if @books.delete(isbn)
|
|
53
|
+
puts "Book removed successfully!"
|
|
54
|
+
else
|
|
55
|
+
puts "Book not found!"
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Search book
|
|
60
|
+
def search_book
|
|
61
|
+
print "Enter ISBN: "
|
|
62
|
+
isbn = gets.chomp
|
|
63
|
+
|
|
64
|
+
if @books[isbn]
|
|
65
|
+
book = @books[isbn]
|
|
66
|
+
puts "\n--- Book Details ---"
|
|
67
|
+
puts "Title: #{book[:title]}"
|
|
68
|
+
puts "Author: #{book[:author]}"
|
|
69
|
+
puts "Copies: #{book[:copies]}"
|
|
70
|
+
else
|
|
71
|
+
puts "Book not found!"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Display all books
|
|
76
|
+
def display_books
|
|
77
|
+
puts "\n--- Library Catalog ---"
|
|
78
|
+
if @books.empty?
|
|
79
|
+
puts "No books available."
|
|
80
|
+
return
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
@books.each do |isbn, book|
|
|
84
|
+
puts "#{isbn} | #{book[:title]} | #{book[:author]} | Copies: #{book[:copies]}"
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Issue book
|
|
89
|
+
def issue_book
|
|
90
|
+
print "Enter ISBN to issue: "
|
|
91
|
+
isbn = gets.chomp
|
|
92
|
+
|
|
93
|
+
if @books[isbn] && @books[isbn][:copies] > 0
|
|
94
|
+
@books[isbn][:copies] -= 1
|
|
95
|
+
puts "Book issued successfully!"
|
|
96
|
+
else
|
|
97
|
+
puts "Book not available!"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Return book
|
|
102
|
+
def return_book
|
|
103
|
+
print "Enter ISBN to return: "
|
|
104
|
+
isbn = gets.chomp
|
|
105
|
+
|
|
106
|
+
if @books[isbn]
|
|
107
|
+
@books[isbn][:copies] += 1
|
|
108
|
+
puts "Book returned successfully!"
|
|
109
|
+
else
|
|
110
|
+
puts "Invalid ISBN!"
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Main Menu
|
|
116
|
+
def menu
|
|
117
|
+
library = Library.new
|
|
118
|
+
|
|
119
|
+
loop do
|
|
120
|
+
puts "\n===== Library Management System ====="
|
|
121
|
+
puts "1. Add Book"
|
|
122
|
+
puts "2. Update Copies"
|
|
123
|
+
puts "3. Remove Book"
|
|
124
|
+
puts "4. Search Book"
|
|
125
|
+
puts "5. Display All Books"
|
|
126
|
+
puts "6. Issue Book"
|
|
127
|
+
puts "7. Return Book"
|
|
128
|
+
puts "8. Exit"
|
|
129
|
+
|
|
130
|
+
print "Enter your choice: "
|
|
131
|
+
choice = gets.to_i
|
|
132
|
+
|
|
133
|
+
case choice
|
|
134
|
+
when 1 then library.add_book
|
|
135
|
+
when 2 then library.update_copies
|
|
136
|
+
when 3 then library.remove_book
|
|
137
|
+
when 4 then library.search_book
|
|
138
|
+
when 5 then library.display_books
|
|
139
|
+
when 6 then library.issue_book
|
|
140
|
+
when 7 then library.return_book
|
|
141
|
+
when 8
|
|
142
|
+
puts "Exiting system..."
|
|
143
|
+
break
|
|
144
|
+
else
|
|
145
|
+
puts "Invalid choice!"
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Run program
|
|
151
|
+
menu
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# person_system.rb
|
|
2
|
+
|
|
3
|
+
# Superclass
|
|
4
|
+
class Person
|
|
5
|
+
attr_accessor :name, :age
|
|
6
|
+
|
|
7
|
+
def initialize(name, age)
|
|
8
|
+
@name = name
|
|
9
|
+
@age = age
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def display_info
|
|
13
|
+
puts "Name: #{@name}"
|
|
14
|
+
puts "Age: #{@age}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def role_description
|
|
18
|
+
puts "This is a person."
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Subclass: Student
|
|
23
|
+
class Student < Person
|
|
24
|
+
attr_accessor :student_id, :course
|
|
25
|
+
|
|
26
|
+
def initialize(name, age, student_id, course)
|
|
27
|
+
super(name, age)
|
|
28
|
+
@student_id = student_id
|
|
29
|
+
@course = course
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Method overriding
|
|
33
|
+
def role_description
|
|
34
|
+
puts "Role: Student"
|
|
35
|
+
puts "Student ID: #{@student_id}"
|
|
36
|
+
puts "Course: #{@course}"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Subclass: Teacher
|
|
41
|
+
class Teacher < Person
|
|
42
|
+
attr_accessor :employee_id, :subject
|
|
43
|
+
|
|
44
|
+
def initialize(name, age, employee_id, subject)
|
|
45
|
+
super(name, age)
|
|
46
|
+
@employee_id = employee_id
|
|
47
|
+
@subject = subject
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Method overriding
|
|
51
|
+
def role_description
|
|
52
|
+
puts "Role: Teacher"
|
|
53
|
+
puts "Employee ID: #{@employee_id}"
|
|
54
|
+
puts "Subject: #{@subject}"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Demonstration
|
|
59
|
+
def main
|
|
60
|
+
puts "\n--- Person Management System ---"
|
|
61
|
+
|
|
62
|
+
student = Student.new("Alice", 20, "S101", "Computer Science")
|
|
63
|
+
teacher = Teacher.new("Mr. John", 40, "T201", "Mathematics")
|
|
64
|
+
|
|
65
|
+
people = [student, teacher]
|
|
66
|
+
|
|
67
|
+
people.each do |person|
|
|
68
|
+
puts "\n-----------------------------"
|
|
69
|
+
person.display_info
|
|
70
|
+
person.role_description # Polymorphism (overridden method)
|
|
71
|
+
puts "-----------------------------"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Run program
|
|
76
|
+
main
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
1️⃣ Create App
|
|
6
|
+
rails new app_name
|
|
7
|
+
cd app_name
|
|
8
|
+
rails server
|
|
9
|
+
What happens:
|
|
10
|
+
Creates full Rails project structure
|
|
11
|
+
Starts server on port 3000
|
|
12
|
+
|
|
13
|
+
👉 Open:
|
|
14
|
+
|
|
15
|
+
http://localhost:3000
|
|
16
|
+
2️⃣ Plan (DO THIS FIRST)
|
|
17
|
+
✔ Models (Tables)
|
|
18
|
+
|
|
19
|
+
Define real-world entities:
|
|
20
|
+
|
|
21
|
+
User, Post, Comment
|
|
22
|
+
✔ Attributes (Columns)
|
|
23
|
+
User:
|
|
24
|
+
name:string
|
|
25
|
+
email:string
|
|
26
|
+
|
|
27
|
+
Post:
|
|
28
|
+
title:string
|
|
29
|
+
content:text
|
|
30
|
+
user_id:integer
|
|
31
|
+
|
|
32
|
+
👉 user_id is a foreign key
|
|
33
|
+
|
|
34
|
+
✔ Relationships
|
|
35
|
+
User has_many Posts
|
|
36
|
+
Post belongs_to User
|
|
37
|
+
Post has_many Comments
|
|
38
|
+
Comment belongs_to Post
|
|
39
|
+
|
|
40
|
+
👉 Always decide:
|
|
41
|
+
|
|
42
|
+
Parent → Child relationship
|
|
43
|
+
3️⃣ Generate Models
|
|
44
|
+
Syntax:
|
|
45
|
+
rails generate model ModelName field:type
|
|
46
|
+
Example:
|
|
47
|
+
rails generate model User name:string email:string
|
|
48
|
+
rails generate model Post title:string content:text user:references
|
|
49
|
+
What Rails creates:
|
|
50
|
+
app/models/user.rb
|
|
51
|
+
app/models/post.rb
|
|
52
|
+
db/migrate/xxxx_create_users.rb
|
|
53
|
+
db/migrate/xxxx_create_posts.rb
|
|
54
|
+
Important:
|
|
55
|
+
user:references → creates user_id + index + foreign key
|
|
56
|
+
4️⃣ Define Associations
|
|
57
|
+
|
|
58
|
+
Edit:
|
|
59
|
+
|
|
60
|
+
app/models/user.rb
|
|
61
|
+
app/models/post.rb
|
|
62
|
+
One-to-Many:
|
|
63
|
+
class User < ApplicationRecord
|
|
64
|
+
has_many :posts
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
class Post < ApplicationRecord
|
|
68
|
+
belongs_to :user
|
|
69
|
+
end
|
|
70
|
+
With dependent delete:
|
|
71
|
+
has_many :posts, dependent: :destroy
|
|
72
|
+
|
|
73
|
+
👉 Example:
|
|
74
|
+
|
|
75
|
+
user = User.find(1)
|
|
76
|
+
user.destroy
|
|
77
|
+
|
|
78
|
+
✔ deletes user
|
|
79
|
+
✔ deletes all posts of that user
|
|
80
|
+
|
|
81
|
+
5️⃣ Run Migration
|
|
82
|
+
rails db:migrate
|
|
83
|
+
What happens:
|
|
84
|
+
Reads migration files
|
|
85
|
+
Creates tables in DB
|
|
86
|
+
Example Table:
|
|
87
|
+
users table:
|
|
88
|
+
id
|
|
89
|
+
name
|
|
90
|
+
email
|
|
91
|
+
|
|
92
|
+
posts table:
|
|
93
|
+
id
|
|
94
|
+
title
|
|
95
|
+
content
|
|
96
|
+
user_id
|
|
97
|
+
6️⃣ Generate Controller
|
|
98
|
+
Basic:
|
|
99
|
+
rails generate controller Posts
|
|
100
|
+
|
|
101
|
+
👉 Creates only controller + views
|
|
102
|
+
|
|
103
|
+
Scaffold (recommended for lab):
|
|
104
|
+
rails generate scaffold Post title:string content:text user:references
|
|
105
|
+
|
|
106
|
+
👉 Automatically creates:
|
|
107
|
+
|
|
108
|
+
Model
|
|
109
|
+
Migration
|
|
110
|
+
Controller
|
|
111
|
+
Views
|
|
112
|
+
Routes
|
|
113
|
+
7️⃣ Routes
|
|
114
|
+
|
|
115
|
+
Edit:
|
|
116
|
+
|
|
117
|
+
config/routes.rb
|
|
118
|
+
Add:
|
|
119
|
+
resources :posts
|
|
120
|
+
Generates:
|
|
121
|
+
URL Action
|
|
122
|
+
/posts index
|
|
123
|
+
/posts/:id show
|
|
124
|
+
/posts/new new
|
|
125
|
+
POST /posts create
|
|
126
|
+
/posts/:id/edit edit
|
|
127
|
+
PATCH update
|
|
128
|
+
DELETE destroy
|
|
129
|
+
Check:
|
|
130
|
+
rails routes
|
|
131
|
+
8️⃣ Controller (CRUD)
|
|
132
|
+
|
|
133
|
+
File:
|
|
134
|
+
|
|
135
|
+
app/controllers/posts_controller.rb
|
|
136
|
+
Full CRUD:
|
|
137
|
+
def index
|
|
138
|
+
@posts = Post.all
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def show
|
|
142
|
+
@post = Post.find(params[:id])
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def new
|
|
146
|
+
@post = Post.new
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def create
|
|
150
|
+
@post = Post.new(post_params)
|
|
151
|
+
if @post.save
|
|
152
|
+
redirect_to @post
|
|
153
|
+
else
|
|
154
|
+
render :new
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def edit
|
|
159
|
+
@post = Post.find(params[:id])
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def update
|
|
163
|
+
@post = Post.find(params[:id])
|
|
164
|
+
if @post.update(post_params)
|
|
165
|
+
redirect_to @post
|
|
166
|
+
else
|
|
167
|
+
render :edit
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def destroy
|
|
172
|
+
@post = Post.find(params[:id])
|
|
173
|
+
@post.destroy
|
|
174
|
+
redirect_to posts_path
|
|
175
|
+
end
|
|
176
|
+
Strong Params (REQUIRED)
|
|
177
|
+
def post_params
|
|
178
|
+
params.require(:post).permit(:title, :content, :user_id)
|
|
179
|
+
end
|
|
180
|
+
9️⃣ Views
|
|
181
|
+
|
|
182
|
+
Location:
|
|
183
|
+
|
|
184
|
+
app/views/posts/
|
|
185
|
+
Files:
|
|
186
|
+
index.html.erb → list records
|
|
187
|
+
show.html.erb → display one record
|
|
188
|
+
new.html.erb → form page
|
|
189
|
+
edit.html.erb → edit form
|
|
190
|
+
_form.html.erb → shared form
|
|
191
|
+
Form Example:
|
|
192
|
+
<%= form_with model: @post do |f| %>
|
|
193
|
+
<%= f.label :title %>
|
|
194
|
+
<%= f.text_field :title %>
|
|
195
|
+
|
|
196
|
+
<%= f.label :content %>
|
|
197
|
+
<%= f.text_area :content %>
|
|
198
|
+
|
|
199
|
+
<%= f.submit %>
|
|
200
|
+
<% end %>
|
|
201
|
+
🔟 Associations in Views
|
|
202
|
+
Show related data:
|
|
203
|
+
<p>Author: <%= @post.user.name %></p>
|
|
204
|
+
Dropdown (foreign key):
|
|
205
|
+
<%= f.collection_select :user_id, User.all, :id, :name %>
|
|
206
|
+
|
|
207
|
+
👉 Format:
|
|
208
|
+
|
|
209
|
+
collection_select(field, collection, value_method, text_method)
|
|
210
|
+
1️⃣1️⃣ Authentication
|
|
211
|
+
|
|
212
|
+
Using Devise
|
|
213
|
+
|
|
214
|
+
Install:
|
|
215
|
+
bundle add devise
|
|
216
|
+
rails generate devise:install
|
|
217
|
+
rails generate devise User
|
|
218
|
+
rails db:migrate
|
|
219
|
+
What you get:
|
|
220
|
+
User model
|
|
221
|
+
Login / Signup
|
|
222
|
+
Sessions
|
|
223
|
+
Password handling
|
|
224
|
+
1️⃣2️⃣ Feature Extensions
|
|
225
|
+
✔ Kanban
|
|
226
|
+
rails generate model Task title:string status:string
|
|
227
|
+
Status values:
|
|
228
|
+
todo, doing, done
|
|
229
|
+
Controller:
|
|
230
|
+
@todo = Task.where(status: "todo")
|
|
231
|
+
@doing = Task.where(status: "doing")
|
|
232
|
+
@done = Task.where(status: "done")
|
|
233
|
+
✔ Library
|
|
234
|
+
class Author < ApplicationRecord
|
|
235
|
+
has_many :books
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
class Book < ApplicationRecord
|
|
239
|
+
belongs_to :author
|
|
240
|
+
end
|
|
241
|
+
✔ E-learning
|
|
242
|
+
class Course < ApplicationRecord
|
|
243
|
+
has_many :lessons
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
class Lesson < ApplicationRecord
|
|
247
|
+
belongs_to :course
|
|
248
|
+
end
|
|
249
|
+
✔ Blog
|
|
250
|
+
Rich Text using Action Text
|
|
251
|
+
rails action_text:install
|
|
252
|
+
rails db:migrate
|
|
253
|
+
has_rich_text :content
|
|
254
|
+
Tags (Many-to-Many)
|
|
255
|
+
rails generate model Tag name:string
|
|
256
|
+
rails generate model PostTag post:references tag:references
|
|
257
|
+
Comments
|
|
258
|
+
rails generate model Comment content:text user:references post:references
|
|
259
|
+
✔ Recipe
|
|
260
|
+
class User < ApplicationRecord
|
|
261
|
+
has_many :recipes
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
class Recipe < ApplicationRecord
|
|
265
|
+
has_many :comments
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
class Comment < ApplicationRecord
|
|
269
|
+
belongs_to :user
|
|
270
|
+
end
|
|
271
|
+
Display Comments:
|
|
272
|
+
<% @recipe.comments.each do |c| %>
|
|
273
|
+
<p><%= c.user.name %>: <%= c.content %></p>
|
|
274
|
+
<% end %>
|
|
275
|
+
1️⃣3️⃣ Testing (Console)
|
|
276
|
+
rails console
|
|
277
|
+
Insert Data:
|
|
278
|
+
User.create(name: "Sakthi")
|
|
279
|
+
Post.create(title: "Hello", content: "Test", user_id: 1)
|
|
280
|
+
Query:
|
|
281
|
+
Post.first.user
|
|
282
|
+
User.first.posts
|
|
283
|
+
1️⃣4️⃣ Final Checklist
|
|
284
|
+
✔ Models created
|
|
285
|
+
✔ Associations defined
|
|
286
|
+
✔ Migrations run
|
|
287
|
+
✔ Routes working
|
|
288
|
+
✔ CRUD working
|
|
289
|
+
✔ Forms working
|
|
290
|
+
✔ Data displayed
|
|
291
|
+
✔ Relations visible
|
|
292
|
+
⚡ FINAL MEMORY LINE
|
|
293
|
+
Plan → Model → Migrate → Controller → Routes → Views → Associations → Features
|
|
294
|
+
|
|
295
|
+
========================================================================
|
|
296
|
+
=end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
|
|
3
|
+
RAILS MASTER CHEAT SHEET (DETAILED)
|
|
4
|
+
|
|
5
|
+
1. Create App
|
|
6
|
+
rails new app_name
|
|
7
|
+
cd app_name
|
|
8
|
+
rails server
|
|
9
|
+
|
|
10
|
+
Open: http://localhost:3000
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
2. Plan (DO THIS FIRST)
|
|
14
|
+
|
|
15
|
+
Models:
|
|
16
|
+
User, Post, Comment
|
|
17
|
+
|
|
18
|
+
Attributes:
|
|
19
|
+
User: name:string, email:string
|
|
20
|
+
Post: title:string, content:text, user_id:integer
|
|
21
|
+
|
|
22
|
+
Relationships:
|
|
23
|
+
User has_many Posts
|
|
24
|
+
Post belongs_to User
|
|
25
|
+
Post has_many Comments
|
|
26
|
+
Comment belongs_to Post
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
3. Generate Models
|
|
30
|
+
rails generate model User name:string email:string
|
|
31
|
+
rails generate model Post title:string content:text user:references
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
4. Define Associations
|
|
35
|
+
class User < ApplicationRecord
|
|
36
|
+
has_many :posts, dependent: :destroy
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class Post < ApplicationRecord
|
|
40
|
+
belongs_to :user
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
5. Run Migration
|
|
45
|
+
rails db:migrate
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
6. Generate Controller
|
|
49
|
+
rails generate scaffold Post title:string content:text user:references
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
7. Routes
|
|
53
|
+
config/routes.rb:
|
|
54
|
+
resources :posts
|
|
55
|
+
|
|
56
|
+
rails routes
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
8. Controller CRUD
|
|
60
|
+
(index, show, new, create, edit, update, destroy)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
9. Views
|
|
64
|
+
app/views/posts/
|
|
65
|
+
|
|
66
|
+
Form:
|
|
67
|
+
<%= form_with model: @post do |f| %>
|
|
68
|
+
<%= f.text_field :title %>
|
|
69
|
+
<%= f.text_area :content %>
|
|
70
|
+
<%= f.submit %>
|
|
71
|
+
<% end %>
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
10. Associations in Views
|
|
75
|
+
<p><%= @post.user.name %></p>
|
|
76
|
+
|
|
77
|
+
<%= f.collection_select :user_id, User.all, :id, :name %>
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
11. Authentication (Devise)
|
|
81
|
+
bundle add devise
|
|
82
|
+
rails generate devise:install
|
|
83
|
+
rails generate devise User
|
|
84
|
+
rails db:migrate
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
12. Features
|
|
88
|
+
|
|
89
|
+
Kanban:
|
|
90
|
+
rails generate model Task title:string status:string
|
|
91
|
+
|
|
92
|
+
Library:
|
|
93
|
+
Author has_many books
|
|
94
|
+
Book belongs_to author
|
|
95
|
+
|
|
96
|
+
E-learning:
|
|
97
|
+
Course has_many lessons
|
|
98
|
+
Lesson belongs_to course
|
|
99
|
+
|
|
100
|
+
Blog:
|
|
101
|
+
rails action_text:install
|
|
102
|
+
has_rich_text :content
|
|
103
|
+
|
|
104
|
+
Tags:
|
|
105
|
+
rails generate model Tag name:string
|
|
106
|
+
rails generate model PostTag post:references tag:references
|
|
107
|
+
|
|
108
|
+
Comments:
|
|
109
|
+
rails generate model Comment content:text user:references post:references
|
|
110
|
+
|
|
111
|
+
Recipe:
|
|
112
|
+
User has_many recipes
|
|
113
|
+
Recipe has_many comments
|
|
114
|
+
Comment belongs_to user
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
13. Testing
|
|
118
|
+
rails console
|
|
119
|
+
|
|
120
|
+
User.create(name: "Sakthi")
|
|
121
|
+
Post.create(title: "Hello", content: "Test", user_id: 1)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
14. Final Checklist
|
|
125
|
+
Models, Associations, Migrations, Routes, CRUD, Views, Data
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
FINAL MEMORY:
|
|
129
|
+
Plan → Model → Migrate → Controller → Routes → Views → Associations → Features
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
=========================================================
|
|
134
|
+
=end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# shopping_cart.rb
|
|
2
|
+
|
|
3
|
+
def calculate_total(cart)
|
|
4
|
+
total = 0
|
|
5
|
+
|
|
6
|
+
cart.each do |product|
|
|
7
|
+
name, quantity, price = product
|
|
8
|
+
subtotal = quantity * price
|
|
9
|
+
|
|
10
|
+
# Apply 10% discount if quantity > 5
|
|
11
|
+
if quantity > 5
|
|
12
|
+
subtotal *= 0.9
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
total += subtotal
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
total
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Sample Input
|
|
22
|
+
cart = [
|
|
23
|
+
["Laptop", 1, 800],
|
|
24
|
+
["Headphones", 6, 50],
|
|
25
|
+
["Mouse", 3, 25]
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
total = calculate_total(cart)
|
|
29
|
+
|
|
30
|
+
puts "Final total: $#{total}"
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# student_scores.rb
|
|
2
|
+
|
|
3
|
+
def calculate_grade(avg)
|
|
4
|
+
case avg
|
|
5
|
+
when 90..100 then "A"
|
|
6
|
+
when 75...90 then "B"
|
|
7
|
+
when 60...75 then "C"
|
|
8
|
+
when 50...60 then "D"
|
|
9
|
+
else "F"
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
students = []
|
|
14
|
+
|
|
15
|
+
puts "Enter number of students:"
|
|
16
|
+
n = gets.to_i
|
|
17
|
+
|
|
18
|
+
n.times do |i|
|
|
19
|
+
puts "\nEnter name of student #{i + 1}:"
|
|
20
|
+
name = gets.chomp
|
|
21
|
+
|
|
22
|
+
puts "Enter marks (space separated):"
|
|
23
|
+
marks = gets.chomp.split.map(&:to_i)
|
|
24
|
+
|
|
25
|
+
total = marks.sum
|
|
26
|
+
average = total.to_f / marks.size
|
|
27
|
+
grade = calculate_grade(average)
|
|
28
|
+
|
|
29
|
+
students << {
|
|
30
|
+
name: name,
|
|
31
|
+
marks: marks,
|
|
32
|
+
total: total,
|
|
33
|
+
average: average,
|
|
34
|
+
grade: grade
|
|
35
|
+
}
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Display Summary
|
|
39
|
+
puts "\n===== Student Summary ====="
|
|
40
|
+
puts "Name\t\tTotal\tAverage\tGrade"
|
|
41
|
+
|
|
42
|
+
students.each do |s|
|
|
43
|
+
puts "#{s[:name]}\t\t#{s[:total]}\t#{s[:average].round(2)}\t#{s[:grade]}"
|
|
44
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# vehicle_management.rb
|
|
2
|
+
|
|
3
|
+
# Superclass
|
|
4
|
+
class Vehicle
|
|
5
|
+
attr_accessor :brand, :speed
|
|
6
|
+
|
|
7
|
+
def initialize(brand, speed)
|
|
8
|
+
@brand = brand
|
|
9
|
+
@speed = speed
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def display_info
|
|
13
|
+
puts "Brand: #{@brand}"
|
|
14
|
+
puts "Speed: #{@speed} km/h"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def start_engine
|
|
18
|
+
puts "Vehicle engine started"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Subclass: Car
|
|
23
|
+
class Car < Vehicle
|
|
24
|
+
def start_engine
|
|
25
|
+
puts "Car engine starts with a key or button"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Subclass: Bike
|
|
30
|
+
class Bike < Vehicle
|
|
31
|
+
def start_engine
|
|
32
|
+
puts "Bike engine starts with a kick or self-start"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Subclass: Truck
|
|
37
|
+
class Truck < Vehicle
|
|
38
|
+
def start_engine
|
|
39
|
+
puts "Truck engine starts with heavy ignition system"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Demonstration
|
|
44
|
+
def main
|
|
45
|
+
puts "\n--- Vehicle Management System ---"
|
|
46
|
+
|
|
47
|
+
car = Car.new("Toyota", 180)
|
|
48
|
+
bike = Bike.new("Yamaha", 120)
|
|
49
|
+
truck = Truck.new("Tata", 100)
|
|
50
|
+
|
|
51
|
+
vehicles = [car, bike, truck]
|
|
52
|
+
|
|
53
|
+
vehicles.each do |vehicle|
|
|
54
|
+
puts "\n#{vehicle.class} Details:"
|
|
55
|
+
vehicle.display_info
|
|
56
|
+
vehicle.start_engine # Method overriding happens here
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Run program
|
|
61
|
+
main
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# vending_machine.rb
|
|
2
|
+
|
|
3
|
+
# Product list
|
|
4
|
+
PRODUCTS = {
|
|
5
|
+
1 => { name: "Chips", price: 20 },
|
|
6
|
+
2 => { name: "Soda", price: 30 },
|
|
7
|
+
3 => { name: "Chocolate", price: 25 }
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
def display_products
|
|
11
|
+
puts "\nAvailable Products:"
|
|
12
|
+
PRODUCTS.each do |id, product|
|
|
13
|
+
puts "#{id}. #{product[:name]} - ₹#{product[:price]}"
|
|
14
|
+
end
|
|
15
|
+
puts "Type 'cancel' anytime to cancel the transaction."
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def get_product_selection
|
|
19
|
+
loop do
|
|
20
|
+
print "\nSelect a product (1-3): "
|
|
21
|
+
input = gets.chomp.downcase
|
|
22
|
+
|
|
23
|
+
return :cancel if input == "cancel"
|
|
24
|
+
|
|
25
|
+
choice = input.to_i
|
|
26
|
+
return choice if PRODUCTS.key?(choice)
|
|
27
|
+
|
|
28
|
+
puts "Invalid selection. Please choose 1, 2, or 3."
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def get_quantity
|
|
33
|
+
loop do
|
|
34
|
+
print "Enter quantity: "
|
|
35
|
+
input = gets.chomp.downcase
|
|
36
|
+
|
|
37
|
+
return :cancel if input == "cancel"
|
|
38
|
+
|
|
39
|
+
qty = input.to_i
|
|
40
|
+
return qty if qty > 0
|
|
41
|
+
|
|
42
|
+
puts "Invalid quantity. Enter a positive number."
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def collect_money(total_price)
|
|
47
|
+
inserted = 0
|
|
48
|
+
|
|
49
|
+
while inserted < total_price
|
|
50
|
+
remaining = total_price - inserted
|
|
51
|
+
print "Insert money (₹#{remaining} remaining): "
|
|
52
|
+
input = gets.chomp.downcase
|
|
53
|
+
|
|
54
|
+
return :cancel, inserted if input == "cancel"
|
|
55
|
+
|
|
56
|
+
amount = input.to_i
|
|
57
|
+
if amount <= 0
|
|
58
|
+
puts "Enter a valid amount."
|
|
59
|
+
next
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
inserted += amount
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
return inserted, 0
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def transaction
|
|
69
|
+
display_products
|
|
70
|
+
|
|
71
|
+
product_id = get_product_selection
|
|
72
|
+
return if product_id == :cancel
|
|
73
|
+
|
|
74
|
+
quantity = get_quantity
|
|
75
|
+
return if quantity == :cancel
|
|
76
|
+
|
|
77
|
+
product = PRODUCTS[product_id]
|
|
78
|
+
total_price = product[:price] * quantity
|
|
79
|
+
|
|
80
|
+
puts "\nTotal price: ₹#{total_price}"
|
|
81
|
+
|
|
82
|
+
money, partial = collect_money(total_price)
|
|
83
|
+
|
|
84
|
+
if money == :cancel
|
|
85
|
+
puts "\nTransaction cancelled."
|
|
86
|
+
puts "Returning inserted money: ₹#{partial}"
|
|
87
|
+
return
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
change = money - total_price
|
|
91
|
+
|
|
92
|
+
puts "\n--- Transaction Summary ---"
|
|
93
|
+
puts "Product: #{product[:name]}"
|
|
94
|
+
puts "Quantity: #{quantity}"
|
|
95
|
+
puts "Total Paid: ₹#{money}"
|
|
96
|
+
puts "Change Returned: ₹#{change}" if change > 0
|
|
97
|
+
puts "Thank you for your purchase!"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Main loop for multiple transactions
|
|
101
|
+
loop do
|
|
102
|
+
transaction
|
|
103
|
+
|
|
104
|
+
print "\nDo you want to buy another product? (yes/no): "
|
|
105
|
+
choice = gets.chomp.downcase
|
|
106
|
+
|
|
107
|
+
break unless choice == "yes"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
puts "\nExiting Vending Machine. Goodbye!"
|
metadata
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: webLabLibrary
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Saktheeswaran
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
email:
|
|
13
|
+
- sakthisakthi19102005@gmail.com
|
|
14
|
+
executables: []
|
|
15
|
+
extensions: []
|
|
16
|
+
extra_rdoc_files: []
|
|
17
|
+
files:
|
|
18
|
+
- lib/webLabLibrary.rb
|
|
19
|
+
- lib/webLabLibrary/library_management.rb
|
|
20
|
+
- lib/webLabLibrary/person_system.rb
|
|
21
|
+
- lib/webLabLibrary/rails_cheat_sheet.rb
|
|
22
|
+
- lib/webLabLibrary/rails_procedure.rb
|
|
23
|
+
- lib/webLabLibrary/shopping_cart.rb
|
|
24
|
+
- lib/webLabLibrary/student_marks.rb
|
|
25
|
+
- lib/webLabLibrary/vehicle_management.rb
|
|
26
|
+
- lib/webLabLibrary/vending_machine.rb
|
|
27
|
+
- lib/webLabLibrary/version.rb
|
|
28
|
+
licenses: []
|
|
29
|
+
metadata: {}
|
|
30
|
+
rdoc_options: []
|
|
31
|
+
require_paths:
|
|
32
|
+
- lib
|
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '0'
|
|
43
|
+
requirements: []
|
|
44
|
+
rubygems_version: 4.0.3
|
|
45
|
+
specification_version: 4
|
|
46
|
+
summary: Simple lab gem
|
|
47
|
+
test_files: []
|