rack-http_router 0.0.34 → 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 +59 -15
  3. metadata +4 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 540161f25e3b163ac4fa031715aeb5fa88d08d18fa4e2064f531929a1cc5a71a
4
- data.tar.gz: 4902e738c689c9e83d4fc8147da221187e86318a699545c6f81052d6d267d93a
3
+ metadata.gz: d815663c5e328d7c57d595fa667bf3137e780388ec74e59ef1ff607120ba9c8d
4
+ data.tar.gz: fa7cbeb64c3ca10814cbabaeabebfd97cd4c90973d837e46534d50d37d0b355d
5
5
  SHA512:
6
- metadata.gz: a28d4e0c6173d667f69c11d48472e9c2766398aae894757fc2d8a87b3f3026d4c502c2a2a9d7582659fef5e7f8e31c1a0fe10af3f341b291757e8b2624cf87af
7
- data.tar.gz: fe5f4cc387c3296b98ba1ec1e832cd8c8bed3ef44a64d25e772dedc5ec3095122f109b0233d1e65477692881e8a0fbfa75f937198812c57a81eee151999f9df0
6
+ metadata.gz: 6a13a2597ac73c3dc785b63cc8c0b0fa1de86a3437e3fc6f48bf2eaf56de71a1070a8a5efd630ae34f867196bb91176a30cc7c84d87c7ea4363e41d27122c489
7
+ data.tar.gz: b614c1cf570854cffdf80f99b2bd2a2f1da1e1fc34b56e51d7043e6011f9183a83bc36391a12f57a7bf249839576de08e1a4dd7d209b2be3cdb06ef1b7d5d1b6
@@ -44,6 +44,14 @@ 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
+
51
+ def assign(obj, hash)
52
+ Rack::HttpRouter::Action.assign(obj, hash)
53
+ end
54
+
47
55
  def html(content, status: 200)
48
56
  Rack::HttpRouter::Action.html(content, status: status)
49
57
  end
@@ -85,14 +93,6 @@ module Rack
85
93
  end
86
94
 
87
95
  class << self
88
- def html(content, status: 200)
89
- [status, { 'Content-Type' => 'text/html' }, [content]]
90
- end
91
-
92
- def html_response(content, status: 200)
93
- Rack::Response.new(content, status, { 'Content-Type' => 'text/html' })
94
- end
95
-
96
96
  def view_response(
97
97
  paths,
98
98
  view_params = {},
@@ -121,11 +121,31 @@ module Rack
121
121
  db: nil,
122
122
  response_instance: false
123
123
  )
124
- erb = if paths.is_a?(Array)
125
- paths.map { |path| erb("#{config.dig(:views, :path) || "views"}/#{path}", config, route, db, view_params) }.join
126
- else
127
- erb("#{config.dig(:views, :path) || "views"}/#{paths}", config, route, db, view_params)
128
- 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
+ )
129
149
 
130
150
  if response_instance
131
151
  return Rack::Response.new(
@@ -138,6 +158,30 @@ module Rack
138
158
  [status, { 'Content-Type' => 'text/html' }, [erb]]
139
159
  end
140
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
+
141
185
  def json(content = {}, status: 200)
142
186
  [status, { 'Content-Type' => 'application/json' }, [Oj.dump(content, mode: :compat)]]
143
187
  end
@@ -163,10 +207,10 @@ module Rack
163
207
  end
164
208
 
165
209
  # rubocop:disable Lint/UnusedMethodArgument
166
- def erb(path, config, route, db, view_params = {})
210
+ def erb(content, config, route, db, view_params = {})
167
211
  @view = OpenStruct.new(view_params)
168
212
 
169
- eval(Erubi::Engine.new(::File.read("#{path}.html.erb")).src)
213
+ eval(Erubi::Engine.new(content).src)
170
214
  end
171
215
  # rubocop:enable Lint/UnusedMethodArgument
172
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.34
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: []