hanami with a modern touch

Post on 24-Jan-2018

59 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

HANAMIwith a modern touch

Ruby Framework Ecosystem

2004 2007 2010 2010 2014

What is Hanami ?

What Hanami stands for ?

HANAMI

RAILS

CUBA

PADRINO

What Hanami stands for ?

What Hanami stands for ?

What Hanami stands for ?

What Hanami stands for ?

What Hanami stands for ?

HANAMIV 1.0.0

After

1392 days

6205 commits

by 295 people

% 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

% 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

# 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

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

# 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

% bundle exec hanami generate action web home#index

# apps/web/config/routes.rb

get '/home', to: 'home#index'

Routes

RoutesController

ViewTemplate

# 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

# 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

<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

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

Entities are immutableclass Flube attr_reader :aonde_estou

def initialize @aonde_estou = :terra_plana end

def manda_pra_lua update(aonde_estou: :lua) endend

Next Steps

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

Getting Started

mauricio.junior@creditas.com.brgithub.com/juniormp

top related