Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Hello

Slide 3

Slide 3 text

Lecture Coding session

Slide 4

Slide 4 text

Anton Davydov github.com/davydovanton twitter.com/anton_davydov davydovanton.com

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Core member

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

twitch.tv/davydovanton

Slide 16

Slide 16 text

OpenSource evangelist

Slide 17

Slide 17 text

ruby rom-rb dry-rb rails crystal etc

Slide 18

Slide 18 text

Stickers

Slide 19

Slide 19 text

• Memes • Coffee • Beer • Kendo • Psychology • jRPG • SW-2039-0208-4228 / 3DS also welcome • How to draw images for presentations

Slide 20

Slide 20 text

Why am I here?

Slide 21

Slide 21 text

Inspiration Web outside Ruby on Rails Next steps and community

Slide 22

Slide 22 text

Inspiration Web outside Ruby on Rails Next steps and community

Slide 23

Slide 23 text

Inspiration Web outside Ruby on Rails Next steps and community

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Luca Guidi github.com/jodosha

Slide 26

Slide 26 text

lucaguidi.com/2014/01/01/announcing-lotus/

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

General Ideas

Slide 29

Slide 29 text

Secure by default

Slide 30

Slide 30 text

Fast response times

Slide 31

Slide 31 text

Long-term maintenance

Slide 32

Slide 32 text

Modularity Forget about fat models

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Simplicity and Lightweight Framework is just a tool

Slide 35

Slide 35 text

Threadsafe

Slide 36

Slide 36 text

Zero Monkey-Patching Don’t think about framework or language

Slide 37

Slide 37 text

railshurts.com/quiz

Slide 38

Slide 38 text

railshurts.com/quiz

Slide 39

Slide 39 text

Architecturally Sound Isolation everywhere

Slide 40

Slide 40 text

How to made it?

Slide 41

Slide 41 text

Container Architecture Clean Architecture

Slide 42

Slide 42 text

Project request

Slide 43

Slide 43 text

App App App request

Slide 44

Slide 44 text

App App App lib request

Slide 45

Slide 45 text

Ap Ap Ap lib Ap Ap Ap lib Ap Ap Ap lib request

Slide 46

Slide 46 text

Ap Ap Ap lib Ap Ap lib Ap Ap Ap lib request Ap

Slide 47

Slide 47 text

apps/ ├── admin │ ├── application.rb │ ├── assets │ ├── config │ │ └── routes.rb │ ├── controllers │ │ └── ... │ ├── templates │ │ └── ... │ └── views │ └── ... └── web

Slide 48

Slide 48 text

Business Logic

Slide 49

Slide 49 text

lib/ ├── project_name │ ├── interactors │ │ └── create_user.rb │ ├── entities │ │ └── user.rb │ ├── mailers │ │ └── templates │ └── repositories

Slide 50

Slide 50 text

lib/ ├── project_name │ ├── interactors │ │ └── create_user.rb │ ├── entities │ │ └── user.rb │ ├── mailers │ │ └── templates │ └── repositories

Slide 51

Slide 51 text

lib/ ├── project_name │ ├── interactors │ │ └── create_user.rb │ ├── entities │ │ └── user.rb │ ├── mailers │ │ └── templates │ └── repositories

Slide 52

Slide 52 text

lib/ ├── project_name │ ├── interactors │ │ └── create_user.rb │ ├── entities │ │ └── user.rb │ ├── mailers │ │ └── templates │ └── repositories

Slide 53

Slide 53 text

Gems included

Slide 54

Slide 54 text

hanami - Base repository, CLI router - Rack compatible HTTP router for Ruby controller - Full featured and fast actions for Rack utils - Ruby core extensions and class utilities model - Persistence with entities and repositories

Slide 55

Slide 55 text

validations - Validations mixin for Ruby objects helpers - View helpers for Ruby applications view - Presentation with a separation assets - Assets management for Ruby mailer - Mail for Ruby applications

Slide 56

Slide 56 text

events - PubSub framework for Ruby cli - CLI framework for Ruby webconsole - Web console for development ujs - Assets management for Ruby

Slide 57

Slide 57 text

Differences

Slide 58

Slide 58 text

# rack class HelloApp def call(env) [200, { **env }, ['Hello!']] end end

Slide 59

Slide 59 text

# hanami-router class HelloApp def call(env) [200, { **env }, ['Hello!']] end end router = Hanami::Router.new router.get '/', to: 'hello_app'

Slide 60

Slide 60 text

# sinatra class Hello < Sinatra get '/' do 'Hello!' end end

Slide 61

Slide 61 text

# hanami Hanami::Router.new do get '/' do [200, { **env }, ['Hello!']] end end

Slide 62

Slide 62 text

Controllers

Slide 63

Slide 63 text

class UsersController < AC def new end def send_sms end private def user_params end end Controllers: Rails

Slide 64

Slide 64 text

Controllers: hanami action module Web::Controllers::Board class Index include Web::Action params do required(:email).filled end def call(params) end end end

Slide 65

Slide 65 text

Controllers: hanami action module Web::Controllers::Board class Index include Web::Action params do required(:email).filled end def call(params) end end end

Slide 66

Slide 66 text

Controllers: hanami action module Web::Controllers::Board class Index include Web::Action params do required(:email).filled end def call(params) end end end

Slide 67

Slide 67 text

Model

Slide 68

Slide 68 text

class User < ActiveRecord::Base include Gravtastic before_destroy :yank_gems has_many :rubygems, through: :ownerships validates :name, presence: true # ... end Model: Rails

Slide 69

Slide 69 text

class User < ActiveRecord::Base include Gravtastic before_destroy :yank_gems has_many :rubygems, through: :ownerships validates :name, presence: true # ... end Model: Rails

Slide 70

Slide 70 text

class User < ActiveRecord::Base include Gravtastic before_destroy :yank_gems has_many :rubygems, through: :ownerships validates :name, presence: true # ... end Model: Rails

Slide 71

Slide 71 text

class User < ActiveRecord::Base include Gravtastic before_destroy :yank_gems has_many :rubygems, through: :ownerships validates :name, presence: true # ... end Model: Rails

Slide 72

Slide 72 text

class User < ActiveRecord::Base include Gravtastic before_destroy :yank_gems has_many :rubygems, through: :ownerships validates :name, presence: true # ... end Model: Rails

Slide 73

Slide 73 text

Model: hanami hanami + ROM = ❤ rom-rb.org

Slide 74

Slide 74 text

Model: hanami entity class User < Hanami::Entity # ... end

Slide 75

Slide 75 text

Model: hanami entity >> user = User.new(id: 1) => #1}> >> user.id => 1 >> user.id = 1 NoMethodError: undefined method `id=' for #1}> Did you mean? id

Slide 76

Slide 76 text

Model: hanami entity >> user = User.new(id: 1) => #1}> >> user.id => 1 >> user.id = 1 NoMethodError: undefined method `id=' for #1}> Did you mean? id

Slide 77

Slide 77 text

Model: hanami entity >> user = User.new(id: 1) => #1}> >> user.id => 1 >> user.id = 1 NoMethodError: undefined method `id=' for #1}> Did you mean? id

Slide 78

Slide 78 text

class UserRepository < Hanami::Repository associations do has_many :books end def find_by_name(name) users # => ROM relation users.where(name: name).limit(1).order { id }.one end end Model: hanami repository

Slide 79

Slide 79 text

>> repo = UserRepository.new => # >> repo.find(1) => # >> repo.find_by_name(‘Anton’) => # Model: hanami repository

Slide 80

Slide 80 text

View

Slide 81

Slide 81 text

View: Rails rails view (partials?) + rails helper

Slide 82

Slide 82 text

View: Hanami hanami view (ruby class) + templates

Slide 83

Slide 83 text

Assets

Slide 84

Slide 84 text

webpacker vs hanami-webpack

Slide 85

Slide 85 text

Adding custom CLI commands

Slide 86

Slide 86 text

Rails ?

Slide 87

Slide 87 text

Hanami

Slide 88

Slide 88 text

Business logic

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Rails

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

youtu.be/DfU6H-8qal8

Slide 93

Slide 93 text

TLDR

Slide 94

Slide 94 text

service objects

Slide 95

Slide 95 text

Nobody knows what it is

Slide 96

Slide 96 text

Zero standardization

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

#call #perform #handle #run #run! #for #execute! #anything

Slide 99

Slide 99 text

Service.call(params) Service.new.call(params) Service.new(params).call Service.new(options).call(params) Service.new(dependencies).call(params)

Slide 100

Slide 100 text

What should service object return?

Slide 101

Slide 101 text

bool object hash array monad result object nothing

Slide 102

Slide 102 text

Hanami

Slide 103

Slide 103 text

Dependency injection

Slide 104

Slide 104 text

Dependency injection

Slide 105

Slide 105 text

Isolated part of your code

Slide 106

Slide 106 text

github.com/discourse/discourse

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

Dependency injection

Slide 114

Slide 114 text

Dependency injection

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

The act of putting a something, into a someone

Slide 120

Slide 120 text

Dependency injection

Slide 121

Slide 121 text

The act of putting a dependency, into a code

Slide 122

Slide 122 text

Examples

Slide 123

Slide 123 text

def greeting(id) user = User.find(id) “Hello, #{user.name}” end

Slide 124

Slide 124 text

puts greeting(1337) # => Hello, Anton

Slide 125

Slide 125 text

def greeting(id) user = User.find(id) “Hello, #{user.name}” end

Slide 126

Slide 126 text

def greeting(id, relation: User) user = relation.find(id) “Hello, #{user.name}” end

Slide 127

Slide 127 text

def greeting(id, relation: User) user = relation.find(id) “Hello, #{user.name}” end

Slide 128

Slide 128 text

def greeting(id, relation: User) user = relation.find(id) “Hello, #{user.name}” end

Slide 129

Slide 129 text

puts greeting(1337) # => Hello, Anton

Slide 130

Slide 130 text

puts greeting( 1337, relation: UserMemory ) # => Hello, Memory

Slide 131

Slide 131 text

Congrats

Slide 132

Slide 132 text

You know everything about dependency injection

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

More here t.me/pepegramming/228

Slide 136

Slide 136 text

github.com/dry-rb/dry-auto_inject

Slide 137

Slide 137 text

class Service attr_reader :repo def initialize(repo: UserRepo.new) @repo = repo end def call(payload) user = repo.find(payload[:id]) # … end end

Slide 138

Slide 138 text

class Service include DEPS[repo: :user_repo] def call(payload) user = repo.find(payload[:id]) # … end end

Slide 139

Slide 139 text

class Service include DEPS[repo: :user_repo] def call(payload) user = repo.find(payload[:id]) # … end end

Slide 140

Slide 140 text

IoC / DI container

Slide 141

Slide 141 text

IoC == Inversion of control

Slide 142

Slide 142 text

Object which know about project dependencies

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

Simple implementation

Slide 145

Slide 145 text

Hash.new

Slide 146

Slide 146 text

Container = {} Container[:redis] = Redis.new(params) Container[:redis]

Slide 147

Slide 147 text

No booting control Mutable No injecting control

Slide 148

Slide 148 text

dry-system

Slide 149

Slide 149 text

Register dependency

Slide 150

Slide 150 text

Container = Dry::Container.new Container.register(:redis, Redis.new(params)) # => okay Container.register(:redis, MockRedis.new) # => raice error

Slide 151

Slide 151 text

Container = Dry::Container.new Container.register(:redis, Redis.new(params)) # => okay Container.register(:redis, MockRedis.new) # => raice error

Slide 152

Slide 152 text

Inject dependency

Slide 153

Slide 153 text

class Service include Container[redis: :redis] def call(payload) user = redis.get(payload[:id]) # … end end

Slide 154

Slide 154 text

class Service include Container[redis: :redis] def call(payload) user = redis.get(payload[:id]) # … end end

Slide 155

Slide 155 text

class Service include Container[redis: :redis] def call(payload) user = redis.get(payload[:id]) # … end end

Slide 156

Slide 156 text

class Service include Container[redis: :redis] def call(payload) user = redis.get(payload[:id]) # … end end

Slide 157

Slide 157 text

More here

Slide 158

Slide 158 text

No content

Slide 159

Slide 159 text

And here

Slide 160

Slide 160 text

dry-rb.org/gems/dry-system/

Slide 161

Slide 161 text

Why?

Slide 162

Slide 162 text

Container Architecture + Dependency management

Slide 163

Slide 163 text

DDD

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

No content

Slide 166

Slide 166 text

Separate domains for different business values

Slide 167

Slide 167 text

Each domain isolated from others

Slide 168

Slide 168 text

pros easy to create separated services: just replace code domains + containers = ❤

Slide 169

Slide 169 text

cons hard to start so much files™

Slide 170

Slide 170 text

No content

Slide 171

Slide 171 text

http is a small part of system

Slide 172

Slide 172 text

No content

Slide 173

Slide 173 text

websockets rack background processing ETL …

Slide 174

Slide 174 text

They all use business logic

Slide 175

Slide 175 text

No content

Slide 176

Slide 176 text

No content

Slide 177

Slide 177 text

Hanami

Slide 178

Slide 178 text

Pros and Cons

Slide 179

Slide 179 text

No magic

Slide 180

Slide 180 text

module Web::Controllers::Board class Index include Web::Action def call(params) end end end

Slide 181

Slide 181 text

Rails action test describe BoardController do let(:response){ get :index } it 'is successful' do expect(response).to eq(200) end end

Slide 182

Slide 182 text

Action test describe Web::Controllers::Board::Index do let(:action){ Board::Index.new } let(:params){ Hash[] } it 'is successful' do response = action.call(params) expect(response[0]).to eq(200) end end

Slide 183

Slide 183 text

Action test describe Web::Controllers::Board::Index do let(:action){ Board::Index.new } let(:params){ Hash[] } it 'is successful' do response = action.call(params) expect(response[0]).to eq(200) end end

Slide 184

Slide 184 text

Action test describe Web::Controllers::Board::Index do let(:action){ Board::Index.new } let(:params){ Hash[] } it 'is successful' do response = action.call(params) expect(response[0]).to eq(200) end end

Slide 185

Slide 185 text

No monkey-patching

Slide 186

Slide 186 text

Best practices

Slide 187

Slide 187 text

The logic separation

Slide 188

Slide 188 text

transport logic =/= business logic

Slide 189

Slide 189 text

TDD

Slide 190

Slide 190 text

No content

Slide 191

Slide 191 text

No content

Slide 192

Slide 192 text

Cons

Slide 193

Slide 193 text

Architecture Implementation Ecosystem

Slide 194

Slide 194 text

Architecture

Slide 195

Slide 195 text

Validations in actions

Slide 196

Slide 196 text

No content

Slide 197

Slide 197 text

No content

Slide 198

Slide 198 text

No content

Slide 199

Slide 199 text

No content

Slide 200

Slide 200 text

No content

Slide 201

Slide 201 text

No content

Slide 202

Slide 202 text

No content

Slide 203

Slide 203 text

No content

Slide 204

Slide 204 text

No content

Slide 205

Slide 205 text

No content

Slide 206

Slide 206 text

How to fix it?

Slide 207

Slide 207 text

Have no ideas

Slide 208

Slide 208 text

Data transfer objects (DTO)

Slide 209

Slide 209 text

Validate payload and create an object

Slide 210

Slide 210 text

No content

Slide 211

Slide 211 text

No content

Slide 212

Slide 212 text

No content

Slide 213

Slide 213 text

No content

Slide 214

Slide 214 text

Only HTTP transport

Slide 215

Slide 215 text

No content

Slide 216

Slide 216 text

No content

Slide 217

Slide 217 text

No content

Slide 218

Slide 218 text

No content

Slide 219

Slide 219 text

No content

Slide 220

Slide 220 text

Will fixed soon

Slide 221

Slide 221 text

Implementation

Slide 222

Slide 222 text

Hanami Model

Slide 223

Slide 223 text

Wrapped on rom 3.0

Slide 224

Slide 224 text

Associations

Slide 225

Slide 225 text

How we solve it?

Slide 226

Slide 226 text

Use rom instead hanami-model

Slide 227

Slide 227 text

Ecosystem

Slide 228

Slide 228 text

It’s grow but we need gems

Slide 229

Slide 229 text

Example: auth libs like devise

Slide 230

Slide 230 text

Tip: good idea to start OSS career

Slide 231

Slide 231 text

Also, we have some opensourse project for learning

Slide 232

Slide 232 text

octostar.herokuapp.com

Slide 233

Slide 233 text

contributors.hanamirb.org

Slide 234

Slide 234 text

rubyjobs.dev

Slide 235

Slide 235 text

More on awesome-hanami.org

Slide 236

Slide 236 text

Conclusion

Slide 237

Slide 237 text

Ruby lives outside rails

Slide 238

Slide 238 text

Rails - fast prototyping or legacy Hanami - small or well maintained projects

Slide 239

Slide 239 text

Hanami can be useful for improving your skills

Slide 240

Slide 240 text

Because you can use hanami’s ideas in rails

Slide 241

Slide 241 text

Or starting OSS career

Slide 242

Slide 242 text

git.io/JeShe