erb_component 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/erb_component/erb_component.rb +12 -49
- data/lib/erb_component/req_tools.rb +42 -0
- data/lib/erb_component/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e662fbd72853e9bfd5788ceb66debf03d28d8bb13dcbd4ed18142dc26d29332f
|
4
|
+
data.tar.gz: 0c3d0e0435280f432bc54bb14c11b1cda6e460fb678e23b53e49e6ed1ea83f1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6357e20fb5f55c3e4d6813ff0fb937fba43faa0b15c5fe6cfb340ad6b427e032bee671a1cb0828dd019c658b26811e4738a2510c2900bb9334838847110dedc0
|
7
|
+
data.tar.gz: 71bceefd23ee82ee445bf4ec81d4cd6852fd2476b26c9037639360792b523cbd100d83e873b180df221d19a9aa915004d9f67c79046305843dff1160e66b2107
|
@@ -1,7 +1,10 @@
|
|
1
1
|
require "erb_component/version"
|
2
|
+
require "erb_component/req_tools"
|
2
3
|
require 'erb'
|
3
4
|
|
4
5
|
class ErbComponent
|
6
|
+
include ReqTools
|
7
|
+
|
5
8
|
class Error < StandardError;
|
6
9
|
end
|
7
10
|
|
@@ -19,34 +22,6 @@ class ErbComponent
|
|
19
22
|
end
|
20
23
|
end
|
21
24
|
|
22
|
-
def path
|
23
|
-
@req.path
|
24
|
-
end
|
25
|
-
|
26
|
-
def path_hash
|
27
|
-
@path_hash ||= begin
|
28
|
-
split = path.split('/')
|
29
|
-
split.shift
|
30
|
-
|
31
|
-
res = {}
|
32
|
-
split.size.times do |i|
|
33
|
-
if split[i].to_i.to_s == split[i]
|
34
|
-
res[split[i - 1].singularize + "_id"] = split[i]
|
35
|
-
end
|
36
|
-
end
|
37
|
-
res.with_indifferent_access
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def params
|
42
|
-
@params ||= begin
|
43
|
-
res = @req.params
|
44
|
-
res.merge!(JSON.parse(req.body.read)) if req.post? || req.put? || req.patch?
|
45
|
-
res.merge!(path_hash)
|
46
|
-
res.with_indifferent_access
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
25
|
def render
|
51
26
|
str = ERB.new(template).result(binding)
|
52
27
|
if parent
|
@@ -64,15 +39,16 @@ class ErbComponent
|
|
64
39
|
new(req, opts).render
|
65
40
|
end
|
66
41
|
|
42
|
+
def self.current_file=(file)
|
43
|
+
@current_file = file
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.current_file
|
47
|
+
@current_file
|
48
|
+
end
|
49
|
+
|
67
50
|
def template_file_path
|
68
|
-
|
69
|
-
if File.exists? "components/#{file_name}"
|
70
|
-
return "components/#{file_name}"
|
71
|
-
elsif File.exists? "pages/#{file_name}"
|
72
|
-
return "pages/#{file_name}"
|
73
|
-
else
|
74
|
-
nil
|
75
|
-
end
|
51
|
+
self.class.current_file.gsub('.rb', '.erb')
|
76
52
|
end
|
77
53
|
|
78
54
|
def template
|
@@ -80,17 +56,4 @@ class ErbComponent
|
|
80
56
|
return File.read(template_file_path) if template_file_path
|
81
57
|
fail "not found: #{template_file_path}"
|
82
58
|
end
|
83
|
-
|
84
|
-
def method_missing(m, *args, &block)
|
85
|
-
return super unless m.to_s[0].upcase == m.to_s[0]
|
86
|
-
m = m.to_s
|
87
|
-
str = Kernel.const_defined?("#{self.class}::#{m}") ? "#{self.class}::#{m}" : m
|
88
|
-
clazz = Kernel.const_get(str)
|
89
|
-
if args.size > 0
|
90
|
-
component = clazz.new(req, *args)
|
91
|
-
else
|
92
|
-
component = clazz.new(req)
|
93
|
-
end
|
94
|
-
component.render
|
95
|
-
end
|
96
59
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module ReqTools
|
2
|
+
def path
|
3
|
+
@req.path
|
4
|
+
end
|
5
|
+
|
6
|
+
def path_hash
|
7
|
+
@path_hash ||= begin
|
8
|
+
split = path.split('/')
|
9
|
+
split.shift
|
10
|
+
|
11
|
+
res = {}
|
12
|
+
split.size.times do |i|
|
13
|
+
if split[i].to_i.to_s == split[i]
|
14
|
+
res[split[i - 1].singularize + "_id"] = split[i]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
res.with_indifferent_access
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def params
|
22
|
+
@params ||= begin
|
23
|
+
res = @req.params
|
24
|
+
res.merge!(JSON.parse(req.body.read)) if req.post? || req.put? || req.patch?
|
25
|
+
res.merge!(path_hash)
|
26
|
+
res.with_indifferent_access
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def method_missing(m, *args, &block)
|
31
|
+
return super unless m.to_s[0].upcase == m.to_s[0]
|
32
|
+
m = m.to_s
|
33
|
+
str = Kernel.const_defined?("#{self.class}::#{m}") ? "#{self.class}::#{m}" : m
|
34
|
+
clazz = Kernel.const_get(str)
|
35
|
+
if args.size > 0
|
36
|
+
component = clazz.new(req, *args)
|
37
|
+
else
|
38
|
+
component = clazz.new(req)
|
39
|
+
end
|
40
|
+
component.render
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: erb_component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Karganyan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: React-style front-end components but for ERB?
|
14
14
|
email:
|
@@ -26,6 +26,7 @@ files:
|
|
26
26
|
- erb_component.gemspec
|
27
27
|
- lib/erb_component.rb
|
28
28
|
- lib/erb_component/erb_component.rb
|
29
|
+
- lib/erb_component/req_tools.rb
|
29
30
|
- lib/erb_component/version.rb
|
30
31
|
homepage: https://github.com/arthurkarganyan/erb_component
|
31
32
|
licenses:
|