hanami with a modern touch

24
HANAMI with a modern touch

Upload: creditas

Post on 24-Jan-2018

59 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Hanami with a modern touch

HANAMIwith a modern touch

Page 2: Hanami with a modern touch

Ruby Framework Ecosystem

2004 2007 2010 2010 2014

Page 3: Hanami with a modern touch

What is Hanami ?

Page 4: Hanami with a modern touch

What Hanami stands for ?

HANAMI

RAILS

CUBA

PADRINO

Page 5: Hanami with a modern touch

What Hanami stands for ?

Page 6: Hanami with a modern touch

What Hanami stands for ?

Page 7: Hanami with a modern touch

What Hanami stands for ?

Page 8: Hanami with a modern touch

What Hanami stands for ?

Page 9: Hanami with a modern touch

What Hanami stands for ?

Page 10: Hanami with a modern touch

HANAMIV 1.0.0

After

1392 days

6205 commits

by 295 people

Page 11: Hanami with a modern touch

% 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
Page 13: Hanami with a modern touch

% 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

# 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

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

# 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

% 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

# 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

# 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

<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

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

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

Next Steps

http://hanamirb.org/guides/getting-started/

Getting Started

Page 24: Hanami with a modern touch

[email protected]/juniormp