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

ROM.rb: Another Approach to Persistence

Seiya IZUMI
September 28, 2017

ROM.rb: Another Approach to Persistence

Meguro.rb #7

Seiya IZUMI

September 28, 2017
Tweet

More Decks by Seiya IZUMI

Other Decks in Programming

Transcript

  1. Model module Domain class Book attr_reader :id, :title, :price def

    initialize(attrs) @id, @title, @price = attrs.values_at(:id, :title, :price) end end end • POROͳΞϓϦέʔγϣϯݻ༗ͷϩδοΫΛ࣋ͭ੹຿ • ଟ͘ͷ৔߹RepositoryʹΑͬͯੜ੒͞ΕΔ • ROM.rb͕͜͜ʹ༩͑ΔӨڹ͸࠷খݶ
  2. Repository module Repository class Book < ROM::Repository[:books] def by_id(id) books.by_pk(id).map_to(Domain::Books).one!

    end def all books.map_to(Domain::Book) end end end • υϝΠϯϨΠϠͱӬଓԽϨΠϠͷ૭ޱʹͳΔ෦෼ • υϝΠϯʹద੾ͳΦϒδΣΫτΛੜ੒͠ɺӬଓԽ૷ஔʹݻ༗ͳΫΤϦੜ ੒ͳͲ͸Relation͕੹຿ͱͯ͠੥͚ෛ͏
  3. Relation class Books < ROM::Relation[:sql] schema(infer: true) do associations do

    belongs_to :author end def by_id(id) where(id: id) end def available where(is_available: true) end end end • ӬଓԽϨΠϠͱͯ͠Ξμϓλʹ ର͢Δૢ࡞Λϥοϓ͢Δ • ΞιγΤʔγϣϯ΍ϑΟʔϧυ όϦσʔγϣϯͳͲΛ͜͜Ͱఆ ٛ • ର৅ͱͳΔΞμϓλʹݻ༗ͷૢ ࡞͕Ͱ͖Δ • ͜͜Ͱఆٛͨ͠ϝιου܈Λ RepositoryͰ࢖͑Δ