erb_component 0.1.2 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/erb_component/erb_component.rb +65 -24
- data/lib/erb_component/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10aa5ea6bca7d3b177903d1aa535be7e306d7a0e939ca05fcb021ccbfff9d248
|
4
|
+
data.tar.gz: e0fcaeed8b0aae415ed31fc13d1911ed1fa0bdd5843218320adb9dbf5874046c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fda9e18ee25ee98d10641cb1f67e345a0cfffd1e10a2bef99b16b5b973009db8f13a98af0baf373c1e887116c41cb53a37fbc4caf2cad38a50accb694d87bc86
|
7
|
+
data.tar.gz: 263523df144487e5f25f6f03d48c27852f20ba787a650f23b5054e355cb322dddf762f306f4870dea21dc9dd7c201a1693ec2da5f38fbfdcd56266328062b028
|
@@ -2,46 +2,87 @@ require "erb_component/version"
|
|
2
2
|
require 'erb'
|
3
3
|
|
4
4
|
class ErbComponent
|
5
|
-
class Error < StandardError;
|
5
|
+
class Error < StandardError;
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :req, :parent
|
9
|
+
|
10
|
+
def initialize(req, opts = {})
|
11
|
+
@req = req
|
12
|
+
begin
|
13
|
+
@parent = self.class.superclass == ErbComponent ? nil : self.class.superclass.new(req)
|
14
|
+
if @parent && !(parent.template['{{VIEW}}'] || parent.template['{{view}}'])
|
15
|
+
@parent = parent.parent
|
16
|
+
end
|
17
|
+
rescue ArgumentError
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def path
|
22
|
+
@req.path
|
23
|
+
end
|
6
24
|
|
7
|
-
|
25
|
+
def path_hash
|
26
|
+
@path_hash ||= begin
|
27
|
+
split = path.split('/')
|
28
|
+
split.shift
|
8
29
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
30
|
+
res = {}
|
31
|
+
split.size.times do |i|
|
32
|
+
if split[i].to_i.to_s == split[i]
|
33
|
+
res[split[i - 1].singularize + "_id"] = split[i]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
res.with_indifferent_access
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def params
|
41
|
+
@params ||= begin
|
42
|
+
res = @req.params
|
43
|
+
res.merge!(JSON.parse(req.body.read)) if req.post? || req.put? || req.patch?
|
44
|
+
res.with_indifferent_access
|
45
|
+
end
|
13
46
|
end
|
14
47
|
|
15
48
|
def render
|
16
49
|
str = ERB.new(template).result(binding)
|
17
|
-
|
50
|
+
if parent
|
51
|
+
parent.render.gsub("{{VIEW}}", str).gsub("{{view}}", str)
|
52
|
+
else
|
53
|
+
str
|
54
|
+
end
|
18
55
|
end
|
19
56
|
|
20
57
|
def self.render(opts = {})
|
21
58
|
new(opts).render
|
22
59
|
end
|
23
60
|
|
24
|
-
def
|
61
|
+
def template_file_path
|
25
62
|
file_name = "#{self.class.name.underscore}.erb"
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
63
|
+
if File.exists? "components/#{file_name}"
|
64
|
+
return "components/#{file_name}"
|
65
|
+
elsif File.exists? "pages/#{file_name}"
|
66
|
+
return "pages/#{file_name}"
|
67
|
+
else
|
68
|
+
nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def template
|
73
|
+
return File.read(template_file_path) if template_file_path
|
74
|
+
fail "not found: #{template_file_path}"
|
31
75
|
end
|
32
76
|
|
33
77
|
def method_missing(m, *args, &block)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
component = clazz.new(opts)
|
78
|
+
m = m.to_s
|
79
|
+
str = Kernel.const_defined?("#{self.class}::#{m}") ? "#{self.class}::#{m}" : m
|
80
|
+
clazz = Kernel.const_get(str)
|
81
|
+
if args.size > 0
|
82
|
+
component = clazz.new(req, *args)
|
83
|
+
else
|
84
|
+
component = clazz.new(req)
|
85
|
+
end
|
43
86
|
component.render
|
44
|
-
rescue
|
45
|
-
super
|
46
87
|
end
|
47
88
|
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.7
|
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-11-
|
11
|
+
date: 2019-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: React-style front-end components but for ERB?
|
14
14
|
email:
|