Hanami with a modern touch
-
Upload
creditas -
Category
Technology
-
view
59 -
download
0
Transcript of Hanami with a modern touch
![Page 1: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/1.jpg)
HANAMIwith a modern touch
![Page 2: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/2.jpg)
Ruby Framework Ecosystem
2004 2007 2010 2010 2014
![Page 3: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/3.jpg)
What is Hanami ?
![Page 4: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/4.jpg)
What Hanami stands for ?
HANAMI
RAILS
CUBA
PADRINO
![Page 5: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/5.jpg)
What Hanami stands for ?
![Page 6: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/6.jpg)
What Hanami stands for ?
![Page 7: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/7.jpg)
What Hanami stands for ?
![Page 8: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/8.jpg)
What Hanami stands for ?
![Page 9: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/9.jpg)
What Hanami stands for ?
![Page 10: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/10.jpg)
HANAMIV 1.0.0
After
1392 days
6205 commits
by 295 people
![Page 11: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/11.jpg)
% gem install hanami
Successfully installed hanami-1.0.0
1 gem installed
% hanami new bookshelf
19 files successfully created
Download, Develop, Deploy in 5 minutes.
.
├── Gemfile
├── Rakefile
├── apps
├── config
├── config.ru
├── db
├── lib
├── public
└── spec
6 directories, 3 files
![Page 12: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/12.jpg)
![Page 13: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/13.jpg)
% bundle exec hanami generate model book
create lib/bookshelf/entities/book.rb
create lib/bookshelf/repositories/book_repository.rb
create db/migrations/20161115110038_create_books.rb
create spec/bookshelf/entities/book_spec.rb
create spec/bookshelf/repositories/book_repository_spec.rb
Entity and Repository.
├── Gemfile
├── Rakefile
├── apps
├── config
├── config.ru
├── db
├──── migrations/all_migrations
├── lib
├──── bookshelf/entities/book.rb
├──── bookshelf/repositories/book_repository.rb
├── public
└── spec
![Page 14: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/14.jpg)
# db/migrations/20161115110038_create_books.rb
Hanami::Model.migration do
change do
create_table :books do
primary_key :id
column :title, String, null: false
column :author, String, null: false
column :created_at, DateTime, null: false
column :updated_at, DateTime, null: false
end
end
end
Migration
MigrationEntity
Repository
![Page 15: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/15.jpg)
Entity# lib/bookshelf/entities/book.rb
class Book < Hanami::Entity
attributes do
attribute :id, Types::Int
attribute :title, Types::String
attribute :author, Types::String
end
end
# Book.new(title: 10)
# => TypeError: 10 (Integer) has invalid type for :title MigrationEntity
Repository
![Page 16: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/16.jpg)
# lib/bookshelf/repositories/book.rb
class Book < Hanami::Repository
def count
books.count
end
def on_sale_count
books.where(on_sale: true).count
end
end
Repository
MigrationEntity
Repository
![Page 17: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/17.jpg)
% bundle exec hanami generate action web home#index
# apps/web/config/routes.rb
get '/home', to: 'home#index'
Routes
RoutesController
ViewTemplate
![Page 18: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/18.jpg)
# apps/web/controllers/home/index.rb
module Web::Controllers::Books
class Index
include Web::Action
expose :books
def call(params)
@books = BookRepository.new.all
end
end
end
Controller
RoutesController
ViewTemplate
![Page 19: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/19.jpg)
# apps/web/views/home/index.rb
module Web::Views::Home
class Create
include Web::View
template 'home/new'
def title
'Book 1'
end
end
end
View
RoutesController
ViewTemplate
![Page 20: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/20.jpg)
<h2>All books</h2>
<% if books.any? %>
<div id="books">
<% books.each do |book| %>
<div class="book">
<h2><%= book.title %></h2>
<p><%= book.author %></p>
</div>
<% end %>
</div>
<% else %>
<p class="placeholder">There are no books
yet.</p>
<% end %>
Template# apps/web/templates/home/index.html.erb
RoutesController
ViewTemplate
![Page 21: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/21.jpg)
result = Signup.new(name: "Luca").validate
result.success? # => true
result = Signup.new({}).validate
result.success? # => false
result.messages.fetch(:name) # => ["must be
filled"]
HANAMI::VALIDATIONS
class Signup
include Hanami::Validations
validations do
required(:name) { filled? & str? &
size?(3..64) }
end
end
![Page 22: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/22.jpg)
Entities are immutableclass Flube attr_reader :aonde_estou
def initialize @aonde_estou = :terra_plana end
def manda_pra_lua update(aonde_estou: :lua) endend
![Page 23: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/23.jpg)
Next Steps
http://hanamirb.org/guides/getting-started/
Getting Started
![Page 24: Hanami with a modern touch](https://reader031.fdocuments.us/reader031/viewer/2022030318/5a6801027f8b9a8a068b4c13/html5/thumbnails/24.jpg)
[email protected]/juniormp