rack-http_router 0.0.35 → 0.0.36

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rack-http_router/action.rb +55 -23
  3. metadata +4 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e7eb090e26635738162faa639dae1abd812fe0b5491f4767ae3bdfd8f8db36e
4
- data.tar.gz: cf2d047dd885c51b8e45506835d03890502ab98a801065953cf06a2e516969e6
3
+ metadata.gz: d815663c5e328d7c57d595fa667bf3137e780388ec74e59ef1ff607120ba9c8d
4
+ data.tar.gz: fa7cbeb64c3ca10814cbabaeabebfd97cd4c90973d837e46534d50d37d0b355d
5
5
  SHA512:
6
- metadata.gz: a79812b7fb6d9d93f78d3e4dae086ee14e39036a6f0a82b946d6c68b0b0b3de3f9e0945e6fedad92efb51ccb2d4665bdfdc565cd4e9718efe400fe449a17152a
7
- data.tar.gz: f5619bcb193f626bbc0b191798391b63e02a5eb98e04da9f63775f2e69665d502b92536dd5c6dee56feefbeed894132334abc1fcddef9bf10b4accd2ecf0da89
6
+ metadata.gz: 6a13a2597ac73c3dc785b63cc8c0b0fa1de86a3437e3fc6f48bf2eaf56de71a1070a8a5efd630ae34f867196bb91176a30cc7c84d87c7ea4363e41d27122c489
7
+ data.tar.gz: b614c1cf570854cffdf80f99b2bd2a2f1da1e1fc34b56e51d7043e6011f9183a83bc36391a12f57a7bf249839576de08e1a4dd7d209b2be3cdb06ef1b7d5d1b6
@@ -44,6 +44,10 @@ module Rack
44
44
  end
45
45
  end
46
46
 
47
+ def layout(layout_path, file_path)
48
+ Rack::HttpRouter::Action.layout(layout_path, file_path)
49
+ end
50
+
47
51
  def assign(obj, hash)
48
52
  Rack::HttpRouter::Action.assign(obj, hash)
49
53
  end
@@ -89,22 +93,6 @@ module Rack
89
93
  end
90
94
 
91
95
  class << self
92
- def assign(obj, hash)
93
- hash.each do |k, v|
94
- obj.define_singleton_method(k) { v }
95
- end
96
-
97
- obj
98
- end
99
-
100
- def html(content, status: 200)
101
- [status, { 'Content-Type' => 'text/html' }, [content]]
102
- end
103
-
104
- def html_response(content, status: 200)
105
- Rack::Response.new(content, status, { 'Content-Type' => 'text/html' })
106
- end
107
-
108
96
  def view_response(
109
97
  paths,
110
98
  view_params = {},
@@ -133,11 +121,31 @@ module Rack
133
121
  db: nil,
134
122
  response_instance: false
135
123
  )
136
- erb = if paths.is_a?(Array)
137
- paths.map { |path| erb("#{config.dig(:views, :path) || "views"}/#{path}", config, route, db, view_params) }.join
138
- else
139
- erb("#{config.dig(:views, :path) || "views"}/#{paths}", config, route, db, view_params)
140
- end
124
+ base_path = config.dig(:views, :path) || "views"
125
+
126
+ file_or_nil = lambda do |path|
127
+ ::File.read(path)
128
+ rescue Errno::ENOENT
129
+ nil
130
+ end
131
+
132
+ file_content = if paths.is_a?(Array)
133
+ paths.map { |path| ::File.read("#{base_path}/#{path}.html.erb") }.join
134
+ else
135
+ ::File.read("#{base_path}/#{paths}.html.erb")
136
+ end
137
+
138
+ erb = erb(
139
+ [
140
+ file_or_nil.call("#{base_path}/layout/_header.html.erb"),
141
+ file_content,
142
+ file_or_nil.call("#{base_path}/layout/_footer.html.erb")
143
+ ].join,
144
+ config,
145
+ route,
146
+ db,
147
+ view_params
148
+ )
141
149
 
142
150
  if response_instance
143
151
  return Rack::Response.new(
@@ -150,6 +158,30 @@ module Rack
150
158
  [status, { 'Content-Type' => 'text/html' }, [erb]]
151
159
  end
152
160
 
161
+ def layout(layout_path, file_path)
162
+ [
163
+ "layout/#{layout_path}/_header",
164
+ file_path,
165
+ "layout/#{layout_path}/_footer"
166
+ ]
167
+ end
168
+
169
+ def assign(obj, hash)
170
+ hash.each do |k, v|
171
+ obj.define_singleton_method(k) { v }
172
+ end
173
+
174
+ obj
175
+ end
176
+
177
+ def html(content, status: 200)
178
+ [status, { 'Content-Type' => 'text/html' }, [content]]
179
+ end
180
+
181
+ def html_response(content, status: 200)
182
+ Rack::Response.new(content, status, { 'Content-Type' => 'text/html' })
183
+ end
184
+
153
185
  def json(content = {}, status: 200)
154
186
  [status, { 'Content-Type' => 'application/json' }, [Oj.dump(content, mode: :compat)]]
155
187
  end
@@ -175,10 +207,10 @@ module Rack
175
207
  end
176
208
 
177
209
  # rubocop:disable Lint/UnusedMethodArgument
178
- def erb(path, config, route, db, view_params = {})
210
+ def erb(content, config, route, db, view_params = {})
179
211
  @view = OpenStruct.new(view_params)
180
212
 
181
- eval(Erubi::Engine.new(::File.read("#{path}.html.erb")).src)
213
+ eval(Erubi::Engine.new(content).src)
182
214
  end
183
215
  # rubocop:enable Lint/UnusedMethodArgument
184
216
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-http_router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.35
4
+ version: 0.0.36
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique F. Teixeira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-29 00:00:00.000000000 Z
11
+ date: 2023-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: erubi
@@ -58,7 +58,7 @@ dependencies:
58
58
  - - "<"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '4.0'
61
- description: A complete http router solution that fit well with pure rack apps
61
+ description: A complete http router solution that fit well with pure rack apps.
62
62
  email: hriqueft@gmail.com
63
63
  executables: []
64
64
  extensions: []
@@ -91,6 +91,5 @@ requirements: []
91
91
  rubygems_version: 3.4.3
92
92
  signing_key:
93
93
  specification_version: 4
94
- summary: '"rack-http_router" come with a router and helper functions to build pure
95
- Rack projects.'
94
+ summary: A complete http router solution that fit well with pure rack apps.
96
95
  test_files: []