enforce a separation of concerns between the core of our product and the delivery mechanisms. The first is expressed by the set of use cases that our product implements, while the latter are interfaces to make these features available to the outside world. このHanamiのアーキテクチャの目的は、製品のコアロジック の関心とその製品を配信するメカニズムの関心の分離を強化 することです。前者は実装する一連のユースケースによって 表現され、後者はこれらの機能を外部の世界で利用できるよ うにするためのインターフェースです。
:bigint not null, primary key # author :string not null # title :string not null # created_at :datetime not null # updated_at :datetime not null class Book < Hanami::Entity def total_price self.unit_price * 1.08 end end
Create a record for the given data (or entity) # update(id, data) – Update the record corresponding to the given id by setting the given data (or entity) # delete(id) – Delete the record corresponding to the given id # all - Fetch all the entities from the relation # find - Fetch an entity from the relation by primary key # first - Fetch the first entity from the relation # last - Fetch the last entity from the relation # clear - Delete all the records from the relation end
initialize(repository) @repository = repository end def call(params) repository.most_recent_by_author(params[:id]) end End article_repository = ArticleRepository.new MostRecentByAuthorGetter.new(article_repository).call(params[:id])
class AtomIndex < Index format :atom end end Hanami::View.configure do root 'app/templates' end Hanami::View.load! articles = ArticleRepository.new.all Articles::Index.render(format: :html, articles: articles) # => This will use Articles::Index # and "articles/index.html.erb" Articles::Index.render(format: :atom, articles: articles) # => This will use Articles::AtomIndex # and "articles/index.atom.erb" Articles::Index.render(format: :xml, articles: articles) # => This will raise a Hanami::View::MissingTemplateError
<div class="errors"> <h3>There was a problem with your submission</h3> <ul> <% error_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <%= form_for :book, '/books' do div class: 'input' do label :title text_field :title end div class: 'input' do label :author text_field :author end div class: 'input' do label :unit_price number_field :unit_price end div class: 'controls' do submit 'Create Book’ end end %> # apps/web/views/books/create.rb module Web::Views::Books class Create include Web::View template 'books/new’ def error_messages error_messages = [] errors.each do |error| error_messages = error.map{|k, v| error_format(k, v) } end error_messages end private def error_format(key, values) "#{key} #{values.join(",")}" end end end
<div class="errors"> <h3>There was a problem with your submission</h3> <ul> <% error_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <%= form_for :book, '/books' do div class: 'input' do label :title text_field :title end div class: 'input' do label :author text_field :author end div class: 'input' do label :unit_price number_field :unit_price end div class: 'controls' do submit 'Create Book’ end end %> # apps/web/views/books/create.rb module Web::Views::Books class Create include Web::View template 'books/new’ def error_messages error_messages = [] errors.each do |error| error_messages = error.map{|k, v| error_format(k, v) } end error_messages end private def error_format(key, values) "#{key} #{values.join(",")}" end end end
<div class="errors"> <h3>There was a problem with your submission</h3> <ul> <% error_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <%= form_for :book, '/books' do div class: 'input' do label :title text_field :title end div class: 'input' do label :author text_field :author end div class: 'input' do label :unit_price number_field :unit_price end div class: 'controls' do submit 'Create Book’ end end %> # apps/web/views/books/create.rb module Web::Views::Books class Create include Web::View template 'books/new’ def error_messages error_messages = [] errors.each do |error| error_messages = error.map{|k, v| error_format(k, v) } end error_messages end private def error_format(key, values) "#{key} #{values.join(",")}" end end end
<div id="books"> <% books.each do |book_presenter| %> <div class="book"> <h2><%= book_presenter.title %></h2> <p><%= book_presenter.author %></p> <p><%= book_presenter.total_price %></p> </div> <% end %> </div> <% else %> <p class="placeholder">There are no books yet.</p> <% end %> <a href="/books/new">New book</a> # lib/bookshelf/presenters /book_presenter.rb require 'hanami/view’ class BookPresenter include Hanami::Presenter attr_reader :book def initialize(book) @book = book end def title book.title end def author book.author end def total_price "税込価格: #{book.total_price.round}円" end end