rack-http_router 0.0.35 → 0.0.37

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 +45 -25
  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: d8116a8bf789a572ba51ac9b1644a88bcf7cffdd5d47533afccea57040d48556
4
+ data.tar.gz: 2c8f9d461996645b791e8087921d03d577576f56778669e8851c2cf2c3de9a70
5
5
  SHA512:
6
- metadata.gz: a79812b7fb6d9d93f78d3e4dae086ee14e39036a6f0a82b946d6c68b0b0b3de3f9e0945e6fedad92efb51ccb2d4665bdfdc565cd4e9718efe400fe449a17152a
7
- data.tar.gz: f5619bcb193f626bbc0b191798391b63e02a5eb98e04da9f63775f2e69665d502b92536dd5c6dee56feefbeed894132334abc1fcddef9bf10b4accd2ecf0da89
6
+ metadata.gz: 932d5f2c0b87140b7b0f6f9fb8bdf897af1335fd950af076204449153312013cbd0d866331b3a36dd4dde5563c57ebcbd99c4d213e7fd2f7100ab8729eebc7db
7
+ data.tar.gz: 460cf2a15bfb6e3f2837f59470127a77c15cd83abb0df2c264bd9c1c70db13b733351e5534d689dbcbad3285053c1ab2bcd7e5d5159a98b14480ebd352b9a034
@@ -44,8 +44,8 @@ module Rack
44
44
  end
45
45
  end
46
46
 
47
- def assign(obj, hash)
48
- Rack::HttpRouter::Action.assign(obj, hash)
47
+ def layout(layout_path, file_path)
48
+ Rack::HttpRouter::Action.layout(layout_path, file_path)
49
49
  end
50
50
 
51
51
  def html(content, status: 200)
@@ -89,22 +89,6 @@ module Rack
89
89
  end
90
90
 
91
91
  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
92
  def view_response(
109
93
  paths,
110
94
  view_params = {},
@@ -133,11 +117,31 @@ module Rack
133
117
  db: nil,
134
118
  response_instance: false
135
119
  )
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
120
+ base_path = config.dig(:views, :path) || "views"
121
+
122
+ file_or_nil = lambda do |path|
123
+ ::File.read(path)
124
+ rescue Errno::ENOENT
125
+ nil
126
+ end
127
+
128
+ file_content = if paths.is_a?(Array)
129
+ paths.map { |path| ::File.read("#{base_path}/#{path}.html.erb") }.join
130
+ else
131
+ ::File.read("#{base_path}/#{paths}.html.erb")
132
+ end
133
+
134
+ erb = erb(
135
+ [
136
+ file_or_nil.call("#{base_path}/layout/_header.html.erb"),
137
+ file_content,
138
+ file_or_nil.call("#{base_path}/layout/_footer.html.erb")
139
+ ].join,
140
+ config,
141
+ route,
142
+ db,
143
+ view_params
144
+ )
141
145
 
142
146
  if response_instance
143
147
  return Rack::Response.new(
@@ -150,6 +154,22 @@ module Rack
150
154
  [status, { 'Content-Type' => 'text/html' }, [erb]]
151
155
  end
152
156
 
157
+ def layout(layout_path, file_path)
158
+ [
159
+ "layout/#{layout_path}/_header",
160
+ file_path,
161
+ "layout/#{layout_path}/_footer"
162
+ ]
163
+ end
164
+
165
+ def html(content, status: 200)
166
+ [status, { 'Content-Type' => 'text/html' }, [content]]
167
+ end
168
+
169
+ def html_response(content, status: 200)
170
+ Rack::Response.new(content, status, { 'Content-Type' => 'text/html' })
171
+ end
172
+
153
173
  def json(content = {}, status: 200)
154
174
  [status, { 'Content-Type' => 'application/json' }, [Oj.dump(content, mode: :compat)]]
155
175
  end
@@ -175,10 +195,10 @@ module Rack
175
195
  end
176
196
 
177
197
  # rubocop:disable Lint/UnusedMethodArgument
178
- def erb(path, config, route, db, view_params = {})
198
+ def erb(content, config, route, db, view_params = {})
179
199
  @view = OpenStruct.new(view_params)
180
200
 
181
- eval(Erubi::Engine.new(::File.read("#{path}.html.erb")).src)
201
+ eval(Erubi::Engine.new(content).src)
182
202
  end
183
203
  # rubocop:enable Lint/UnusedMethodArgument
184
204
 
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.37
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-08-01 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: []