Skip to content

Commit f7401a5

Browse files
authored
[ruby/hanami] Update to Hanami 2.2 (#9685)
Move setting headers to application action. Use repo's for database operations.
1 parent feca831 commit f7401a5

File tree

21 files changed

+158
-115
lines changed

21 files changed

+158
-115
lines changed

frameworks/Ruby/hanami/Gemfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
source "https://rubygems.org"
44

5-
gem "hanami", "~> 2.0"
6-
gem "hanami-router", "~> 2.0"
7-
gem "hanami-controller", "~> 2.0"
8-
gem "hanami-validations", "~> 2.0"
5+
gem "hanami", "~> 2.2"
6+
gem "hanami-router", "~> 2.2"
7+
gem "hanami-controller", "~> 2.2"
8+
gem "hanami-db", "~> 2.2"
9+
gem "hanami-validations", "~> 2.2"
910

1011
gem "dry-types", "~> 1.0", ">= 1.6.1"
1112
gem "puma"

frameworks/Ruby/hanami/Gemfile.lock

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ GEM
22
remote: https://rubygems.org/
33
specs:
44
bigdecimal (3.1.9)
5-
concurrent-ruby (1.3.4)
5+
concurrent-ruby (1.3.5)
66
dry-auto_inject (1.1.0)
77
dry-core (~> 1.1)
88
zeitwerk (~> 2.6)
@@ -30,38 +30,38 @@ GEM
3030
dry-configurable (~> 1.0, < 2)
3131
dry-core (~> 1.0, < 2)
3232
dry-events (~> 1.0, < 2)
33-
dry-schema (1.13.4)
33+
dry-schema (1.14.1)
3434
concurrent-ruby (~> 1.0)
3535
dry-configurable (~> 1.0, >= 1.0.1)
36-
dry-core (~> 1.0, < 2)
37-
dry-initializer (~> 3.0)
38-
dry-logic (>= 1.4, < 2)
39-
dry-types (>= 1.7, < 2)
40-
zeitwerk (~> 2.6)
41-
dry-struct (1.7.0)
4236
dry-core (~> 1.1)
37+
dry-initializer (~> 3.2)
38+
dry-logic (~> 1.5)
4339
dry-types (~> 1.8)
40+
zeitwerk (~> 2.6)
41+
dry-struct (1.8.0)
42+
dry-core (~> 1.1)
43+
dry-types (~> 1.8, >= 1.8.2)
4444
ice_nine (~> 0.11)
4545
zeitwerk (~> 2.6)
46-
dry-system (1.2.0)
46+
dry-system (1.2.2)
4747
dry-auto_inject (~> 1.1)
4848
dry-configurable (~> 1.3)
4949
dry-core (~> 1.1)
5050
dry-inflector (~> 1.1)
5151
dry-transformer (1.0.1)
5252
zeitwerk (~> 2.6)
53-
dry-types (1.8.0)
53+
dry-types (1.8.2)
5454
bigdecimal (~> 3.0)
5555
concurrent-ruby (~> 1.0)
5656
dry-core (~> 1.0)
5757
dry-inflector (~> 1.0)
5858
dry-logic (~> 1.4)
5959
zeitwerk (~> 2.6)
60-
dry-validation (1.10.0)
60+
dry-validation (1.11.1)
6161
concurrent-ruby (~> 1.0)
62-
dry-core (~> 1.0, < 2)
63-
dry-initializer (~> 3.0)
64-
dry-schema (>= 1.12, < 2)
62+
dry-core (~> 1.1)
63+
dry-initializer (~> 3.2)
64+
dry-schema (~> 1.14)
6565
zeitwerk (~> 2.6)
6666
hanami (2.2.1)
6767
bundler (>= 1.16, < 3)
@@ -82,80 +82,84 @@ GEM
8282
dry-inflector (~> 1.0, < 2)
8383
rake (~> 13.0)
8484
zeitwerk (~> 2.6)
85-
hanami-controller (2.1.0)
85+
hanami-controller (2.2.0)
8686
dry-configurable (~> 1.0, < 2)
8787
dry-core (~> 1.0)
88-
hanami-utils (~> 2.1)
88+
hanami-utils (~> 2.2)
8989
rack (~> 2.0)
9090
zeitwerk (~> 2.6)
91-
hanami-router (2.1.0)
91+
hanami-db (2.2.1)
92+
rom (~> 5.4, >= 5.4.1)
93+
rom-sql (~> 3.7)
94+
zeitwerk (~> 2.6)
95+
hanami-router (2.2.0)
9296
mustermann (~> 3.0)
9397
mustermann-contrib (~> 3.0)
9498
rack (~> 2.0)
9599
hanami-utils (2.2.0)
96100
concurrent-ruby (~> 1.0)
97101
dry-core (~> 1.0, < 2)
98102
dry-transformer (~> 1.0, < 2)
99-
hanami-validations (2.1.0)
103+
hanami-validations (2.2.0)
100104
dry-validation (>= 1.10, < 2)
101-
zeitwerk (~> 2.6.0)
102105
hansi (0.2.1)
103106
ice_nine (0.11.2)
104-
json (2.9.1)
105-
logger (1.6.4)
107+
json (2.10.2)
108+
logger (1.6.6)
106109
mustermann (3.0.3)
107110
ruby2_keywords (~> 0.0.1)
108111
mustermann-contrib (3.0.3)
109112
hansi (~> 0.2.0)
110113
mustermann (= 3.0.3)
111114
nio4r (2.7.4)
112115
pg (1.5.9)
113-
puma (6.5.0)
116+
puma (6.6.0)
114117
nio4r (~> 2.0)
115-
rack (2.2.9)
118+
rack (2.2.13)
116119
rake (13.2.1)
117-
rom (5.3.2)
118-
rom-changeset (~> 5.3, >= 5.3.0)
119-
rom-core (~> 5.3, >= 5.3.2)
120-
rom-repository (~> 5.3, >= 5.3.0)
121-
rom-changeset (5.3.0)
120+
rom (5.4.2)
121+
rom-changeset (~> 5.4)
122+
rom-core (~> 5.4)
123+
rom-repository (~> 5.4, >= 5.4.2)
124+
rom-changeset (5.4.0)
122125
dry-core (~> 1.0)
123-
rom-core (~> 5.3)
124-
transproc (~> 1.0, >= 1.1.0)
125-
rom-core (5.3.2)
126+
rom-core (~> 5.4)
127+
transproc (~> 1.1)
128+
rom-core (5.4.0)
126129
concurrent-ruby (~> 1.1)
127130
dry-configurable (~> 1.0)
128131
dry-core (~> 1.0)
129132
dry-inflector (~> 1.0)
130-
dry-initializer (~> 3.0, >= 3.0.1)
133+
dry-initializer (~> 3.2)
131134
dry-struct (~> 1.0)
132135
dry-types (~> 1.6)
133-
transproc (~> 1.0, >= 1.1.0)
134-
rom-repository (5.3.0)
135-
dry-core (~> 1.0)
136-
dry-initializer (~> 3.0, >= 3.0.1)
137-
rom-core (~> 5.3, >= 5.3.0)
138-
rom-sql (3.6.5)
136+
transproc (~> 1.1)
137+
rom-repository (5.4.2)
139138
dry-core (~> 1.0)
140-
dry-types (~> 1.0)
141-
rom (~> 5.2, >= 5.2.1)
139+
dry-initializer (~> 3.2)
140+
rom-core (~> 5.4)
141+
rom-sql (3.7.0)
142+
dry-core (~> 1.1)
143+
dry-types (~> 1.8)
144+
rom (~> 5.4)
142145
sequel (>= 4.49)
143146
ruby2_keywords (0.0.5)
144-
sequel (5.88.0)
147+
sequel (5.90.0)
145148
bigdecimal
146149
transproc (1.1.1)
147-
zeitwerk (2.6.18)
150+
zeitwerk (2.7.2)
148151

149152
PLATFORMS
150153
ruby
151154
x86_64-darwin-23
152155

153156
DEPENDENCIES
154157
dry-types (~> 1.0, >= 1.6.1)
155-
hanami (~> 2.0)
156-
hanami-controller (~> 2.0)
157-
hanami-router (~> 2.0)
158-
hanami-validations (~> 2.0)
158+
hanami (~> 2.2)
159+
hanami-controller (~> 2.2)
160+
hanami-db (~> 2.2)
161+
hanami-router (~> 2.2)
162+
hanami-validations (~> 2.2)
159163
pg
160164
puma
161165
rake

frameworks/Ruby/hanami/Procfile.dev

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
web: bundle exec hanami server
2+
# assets: bundle exec hanami assets watch

frameworks/Ruby/hanami/app/action.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,13 @@
55

66
module HelloWorld
77
class Action < Hanami::Action
8+
before :set_headers
9+
10+
private
11+
12+
def set_headers(*, response)
13+
response.headers['Server'] = 'hanami'
14+
response.headers['Date'] = Time.now.httpdate
15+
end
816
end
917
end

frameworks/Ruby/hanami/app/actions/db/index.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ module Actions
55
module DB
66
class Index < HelloWorld::Action
77
QUERY_RANGE = 1..10_000 # range of IDs in the Fortune DB
8-
include Deps["persistence.rom"]
8+
9+
include Deps["repos.world_repo"]
910

1011
def handle(*, response)
11-
world = rom.relations[:World].by_pk(random_id).one
12-
response.headers['Server'] = 'hanami'
13-
response.headers['Date'] = Time.now.httpdate
12+
world = world_repo.find(random_id)
1413
response.format = :json
15-
response.body = world.to_json
14+
response.body = world.to_h.to_json
1615
end
1716

1817
def random_id

frameworks/Ruby/hanami/app/actions/json/index.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ module Actions
55
module JSON
66
class Index < HelloWorld::Action
77
def handle(*, response)
8-
response.headers['Server'] = 'hanami'
9-
response.headers['Date'] = Time.now.httpdate
108
response.format = :json
119
response.body = { 'message' => 'Hello, World!' }.to_json
1210
end

frameworks/Ruby/hanami/app/actions/plaintext/index.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ module Actions
55
module Plaintext
66
class Index < HelloWorld::Action
77
def handle(*, response)
8-
response.headers['Server'] = 'hanami'
9-
response.headers['Date'] = Time.now.httpdate
108
response.body = 'Hello, World!'
119
end
1210
end

frameworks/Ruby/hanami/app/actions/queries/index.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ class Index < HelloWorld::Action
99
MIN_QUERIES = 1 # min number of records that can be retrieved
1010
MAX_QUERIES = 500 # max number of records that can be retrieved
1111

12-
include Deps["persistence.rom"]
12+
include Deps["repos.world_repo"]
1313

1414
def handle(request, response)
1515
worlds = ALL_IDS.sample(queries(request)).map do |id|
16-
rom.relations[:World].by_pk(id).one
16+
world_repo.find(id)
1717
end
18-
response.headers['Server'] = 'hanami'
19-
response.headers['Date'] = Time.now.httpdate
2018
response.format = :json
21-
response.body = worlds.to_json
19+
response.body = worlds.map(&:to_h).to_json
2220
end
2321

2422
private

frameworks/Ruby/hanami/app/actions/updates/index.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,12 @@ class Index < HelloWorld::Action
99
MIN_QUERIES = 1 # min number of records that can be retrieved
1010
MAX_QUERIES = 500 # max number of records that can be retrieved
1111

12-
include Deps["persistence.rom"]
12+
include Deps["repos.world_repo"]
1313

1414
def handle(request, response)
1515
worlds = ALL_IDS.sample(queries(request)).map do |id|
16-
world = rom.relations[:World].by_pk(id)
17-
world_struct = world.one
18-
new_value = random_id
19-
new_value = random_id while new_value == world_struct[:randomnumber]
20-
world_struct[:randomnumber] = new_value
21-
world.command(:update).call(randomnumber: world_struct[:randomnumber])
22-
world_struct
16+
world_repo.update(id)
2317
end
24-
response.headers['Server'] = 'hanami'
25-
response.headers['Date'] = Time.now.httpdate
2618
response.format = :json
2719
response.body = worlds.to_json
2820
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
require "hanami/db/relation"
4+
5+
module HelloWorld
6+
module DB
7+
class Relation < Hanami::DB::Relation
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)