Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Rails構成とオブジェクト指向/ rails-structure-and-object-or...
Search
@amyroi
June 08, 2018
1.7k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rails構成とオブジェクト指向/ rails-structure-and-object-oriented
@amyroi
June 08, 2018
More Decks by @amyroi
See All by @amyroi
リリースレトロスペクティブ/release-retrospective
amyroi
0
400
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
620
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Ruling the World: When Life Gets Gamed
codingconduct
0
400
Believing is Seeing
oripsolob
1
240
The untapped power of vector embeddings
frankvandijk
2
1.9k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
540
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
310
SEO for Brand Visibility & Recognition
aleyda
0
4.8k
Chasing Engaging Ingredients in Design
codingconduct
0
350
Transcript
コード構成素案 主にビジネスロジックの話 @amyroi
コード配置例 # 例 app/ ├ lib/ # もしくは lib/ 配下、autoload
│ └ aiml/ # プロダクトコードに影響しないコンポーネン │ └ aws/ # プロダクトコードに影響しないコンポーネント │ └ nlu/ # gem 化想定 │ ├ models/ │ └ concerns/ # ActiveSupport::Concern │ └ user.rb # ActiveRelation 継承 │ └ validators/ # 独自validation クラス ├ behaviors/ │ └ user_search_form.rb # Form オブジェクト │ # | Service クラス │ # | Decorator クラス │ └ nlu_manage/ # プロジェクト内で行うNLU への │ # アクセス関するクラスをまとめるnamespace │ └ project.rb # 振る舞いのクラス │ └ bot.rb # 振る舞いのクラス 2
継承と結合をなくしていく module, callback, 継承の使用を極力さける 継承より委譲 密結合 → 疎結合へ lib配下のライブラリは対象外 3
メリット 振る舞いに合わせたオブジェクト設計 疎結合にすることによりユニットテストの見通しがよくなる デメリット デザインパターンの乱用 振る舞いにおけるファイル数増 4
継承 class A def save end end class B <
A end b = B.new b.save 5
委譲 class A def initialize(object) end def save object.save end
end class B # object を汚染しない end class C end a = A.new(B.new) a.save a = A.new(C.new) a.save 6
モジュール インスタンスを生成しない 例:helper グローバルでアクセスが可能。 moduleではなくclassにしていく。もしくはnamespaceとして使 う。 7
concernについて ActiveSupport::Concern 使用用途:ApplicationRecordに共通のクラスメソッド、インスタン スメソッドを追加する クラスのトップレベルの共通化。 使用頻度を少なく 8
クラス インスタンス化 メソッドのスコープを限定できる オブジェクト! models配下に振る舞いのクラスを生成していく ActiveModel、デザインパターン、委譲 transactionは明示的に行う 9
FormObject (ActiveModel) フォーム表示するための振る舞いを分離したクラス validationも配置可能 Serviceクラスでも可 10
モジュール module Seachable extend ActiveSupport::Concern def self.seach(params) end end class
User include Seachable end # Usage User.search(params) 11
Formオブジェクト class UserSearchForm include ActiveModel::Model def self.search(search_params) end end class
User # object を汚染しない end # Usage UserSearchForm.search(params) 12
Callback Railsのcallbackの影響 モデル同士の結合度の高さ 完結したユニットテストがかけない ActiveModel::Callbacksを使用 transactionは定義 Decoratorパターンで作成したクラスで処理を完結させる。 13
ActiveRecordのcallback class User before_create :join_project def join_project self.project = Project.create()
end end user = User.new user.save 14
Decoratorパターンで役割を分離した例 class UserCreateDecorator include ActiveModel::Model attr_accessor :user define_model_callback :save before_save
:set_project def save User.transaction do run_callbacks(:save) do @user.save end end end def set_project @user.project = Project.create end end user = UserCreateDecorator.new(User.new) user.save 15