Upgrade to Pro — share decks privately, control downloads, hide ads and more …

フレームワークを作らない方法/How NOT to build frameworks

フレームワークを作らない方法/How NOT to build frameworks

銀座Rails#10での発表資料です

MOROHASHI Kyosuke

June 21, 2019
Tweet

More Decks by MOROHASHI Kyosuke

Other Decks in Programming

Transcript

  1.  

  2.   let!(:article) { create(:article, author: author } # ...͜ͷؒ

    30ߦ... context ';ͭ͏ͷϢʔβʔͷهࣄ͸Ұཡදࣔର৅' do let(:author) { create(:author) } it { expect(Article.indexable).to include(article) } context 'ୀձͨ͠Ϣʔβʔͷهࣄ͸Ұཡදࣔର৅֎' do let(:author) { create(:author, retired_at: Time.now) } it { expect(Article.indexable).not_to include(article) }
  3.   def article_by(user) create(:article, author: author) end context ';ͭ͏ͷϢʔβʔ'

    do let(:article) { article_by(create(:user)) } it { expect(Article.indexable).to include(article) } context 'ୀձͨ͠Ϣʔβʔ' do let(:article) { article_by(:author, retired_at: Time.now) } it { expect(Article.indexable).not_to include(article) }
  4.   class TwitterNotification < BaseNotification private def before_request authorize(@article.author)

    def request @user = article.author.access_token ... def after_request nil class BaseNotification def notify(article) @article = article before_request request after_request private def request raise NotImplementedError
  5.   class TwitterNotification < BaseNotification private def before_request authorize(@article.author)

    def request @user = article.author.access_token ... def after_request nil class BaseNotification def notify(article) @article = article before_request request after_request private def request raise NotImplementedError
  6. ‣ ;ͭ͏ͷ܁Γฦ͠ ‣ .PEFMBMMFBDIEPFOE ‣ ॲཧͷڥք ‣ .PEFMUBOTBDUJPOEPFOE ‣ %4-

    ‣ 3BJMTBQQMJDBUJPOSPVUFTESBXEPFOE ‣ 34QFDͷMFU 3BJMTΞϓϦʹ͓͚ΔϒϩοΫ  
  7.   module Transactionable def transaction do_begin do_something do_commit rescue

    do_rollback class Model include Transactionable private def do_something model1.save! model2.save! NJYJOͰॻ͍ͯΈΔ
  8.   class MyModel < AbstractModel private def do_something model1.save!

    model2.save! ςϯϓϨʔτϝιουͰॻ͍ͯΈΔ class AbstractModel def transaction do_begin do_something do_commit rescue do_rollback private def do_something raise NotImplementedError
  9.   class SaveStrategy def initialize(*models) def do_something @models.each(&:save!) ...

    s = SaveStrategy.new( model1, model2 ) model.transaction(s) ετϥςδʔͰॻ͍ͯΈΔ class Model def transaction(strategy) do_begin strategy.do_something do_commit rescue do_rollback
  10.   class Model def transaction(&strategy) do_begin strategy.call # or

    yield do_commit rescue do_rollback ϒϩοΫΛड͚෇͚Δ model.transaction do model1.save! model2.save!
  11.   class MyModel < AbstractModel private def do_something model1.save!

    model2.save! ྫEP@TPNFUIJOH͕ݺ͹Εͯͳͦ͞͏ͳͱ͖Ͳ͏͢Δ class AbstractModel def transaction do_begin do_something do_commit rescue do_rollback private def do_something raise NotImplementedError
  12. ‣ ϔϧύʔϝιουΛɺετϥςδʔύλʔϯΛɺϒϩοΫΛ ࢖͑͹Α͍Θ͚Ͱ͸ͳ͍ɻ ‣ 'PP'SBNFXPSLOBOUPLB@QSPD DPOUFYU \^Έ͍ͨͳ ‣ ͍ͭɺͲ͏ݺ͹ΕΔͷ 

    ‣ ʮࠓ͜ͷศརϥΠϒϥϦΛॻ͍ͯΔࣗ෼͸ɺͦΕ͕
 Ͳ͏࢖ΘΕΔ͔·ͩ஌Βͳ͍ʯͱ͍͏ؾ࣋ͪʹ޲͖߹͏ɻ )PXʹ͍ͭͯ࿩͠·͕ͨ͠