2023-04-28 - MVC - Cookbook
- back to Le Wagon's Bootcamp log.
- link to the lecture (in the portuguese version the interesting part starts at 32min).
main topics
- MVC
Reassistindo
OOP Concept
OOP is all about state and behavior.
- state = data
- behavior = manipulates the state
Class method vs. Intance method
Class method:
class Cat
def self.branch
"Mammals"
end
end
puts Cat.branch
# => Mammals
felix = Cat.new
felix.branch
# NoMethodError
Instance method
class Cat
def branch
"Mammals"
end
end
felix = Cat.new
felix.branch
# => Mammals
puts Cat.branch
# NoMethodError
Task Manager with MVC
- test_scenario
- task.rb (model)
- @title
- @completed
- repository.rb
- @tasks = []
- add(task)
- all
- find
- controller.rb
- @view
- add_task
- list_tasks
- mark_task_as_complete
-
view.rb
- router.rb
- create Task class (Model).
- create Repository - holds instances of Task model.
- implemented Controller for CRUD actions.
- create View to do puts+gets
- create Router to send user to where they wanna go
My notes
All these files can look like an unnecessary extra complexity, but, trust me, they are useful.
It looks overkill here because we're using all these files for such a simple thing like a "todo manager", but in real life we're going to solve more complex problems (like financial transactions, booking apartments, etc.)
Video
- link no vídeo em português, começa nos 32min
MVC
UML diagram for the Task Manager:
Development flow
First the Model, then the Repository (abstraction layer on top of the persistence), then the View, then the Controller
Model
You should always start with your model. The most important thing in your app is your data, and using models allows you to manipulate whatever data you have.
Repository
Structure to store user's data. It's an abstraction layer on top of the persistence.
Controller
Gather data from the Repository to hand them over to the View. Also asks the view for information to get more data.
View
Responsible to interact with the user.