DHHの関わり “Basecamp(注1)という新しいプロダクトの開発のとき,⾃分が開発 環境を決められるようになり,それなら⼀番美しいソースコードを書け る⾔語にしようということでRubyにしたんです。” 出典: 小飼弾のアルファギークに逢いたい♥ #2 (2006) “I did all the original programming for Base Camp” 出典: Millionaire Story: David Heinemeier Hansson (2011) !13 ※ 太字強調は引用者によるもの
DHHがRailsで目指したもの “I was mostly doing PHP on my own, and I worked at a Java shop for a period of time. It was J EE to some extent and otherwise Java in general. Those were the two forming influences. With Ruby On Rails, I tried to form the best of both worlds to make it as quick as PHP and as solid and clean as something like Java.” 出典: Rails creator on Java and other 'junk' (2007) !19 ※ 太字強調は引用者によるもの
!30 Railsでの実装例: Controller class UserRegistrationsController < ApplicationController def create @user_registration = UserRegistration.new(user_registration_params) if @user_registration.save redirect_to complete_user_registrations_url else render :new end end private def user_registration_params params.require(:user_registration).permit(:email) end end
!33 Hanamiでの実装例: Interactor (入力値チェック) class StartUserRegistration include Hanami::Interactor class Validator include Hanami::Validations validations do required(:email).filled(:str?, format?: URI::MailTo::EMAIL_REGEXP) end end private def valid?(params) Validator.new(params).validate.yield_self do |result| result.messages.each_key { |key| error("#{key.capitalize} is invalid") } result.success? end end end #valid?がtrueを返すと#call(後述)が実行される
Clean ArchitectureとRails Modelの対応 !37 Clean Architecture Ruby on Rails ビジネスロジックの記述 Entity Model ユースケースの組み立て Interactor Model (AR Callbacks) 入力値のバリデーション Interactor Model (AR Validations) DBアクセス・データ変換 Repository Model (AR Query Interface)