erb_component 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd03e76bea77333a67118883f08c6111aa35ae798fa197603ffddd82b5dfd6e0
4
- data.tar.gz: db14f5f4f7f3a9f0ab8975ce1742d13826ad5e8d87b70a6f82780a01b730be18
3
+ metadata.gz: 10aa5ea6bca7d3b177903d1aa535be7e306d7a0e939ca05fcb021ccbfff9d248
4
+ data.tar.gz: e0fcaeed8b0aae415ed31fc13d1911ed1fa0bdd5843218320adb9dbf5874046c
5
5
  SHA512:
6
- metadata.gz: d8f56a30324d2027400a433e1ececbc4300a7c3d52e9738f6e1b422c8060f2e3c5105651d2de5ec044d816834b272c3e83fc91bb177b05cd7972b9f38c5f8bc1
7
- data.tar.gz: 0d38db836dcfde8b3dd0b78ec175f26a1904e4213b41d16e5ebdcffb992a45bd61d06c4c3cae82393f74357263df1303c077c584b7512b856a4cf5b7bfd5e770
6
+ metadata.gz: fda9e18ee25ee98d10641cb1f67e345a0cfffd1e10a2bef99b16b5b973009db8f13a98af0baf373c1e887116c41cb53a37fbc4caf2cad38a50accb694d87bc86
7
+ data.tar.gz: 263523df144487e5f25f6f03d48c27852f20ba787a650f23b5054e355cb322dddf762f306f4870dea21dc9dd7c201a1693ec2da5f38fbfdcd56266328062b028
@@ -7,22 +7,51 @@ class ErbComponent
7
7
 
8
8
  attr_reader :req, :parent
9
9
 
10
- def initialize(req)
10
+ def initialize(req, opts = {})
11
11
  @req = req
12
- @parent = self.class.superclass == ErbComponent ? nil : self.class.superclass.new(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
13
19
  end
14
20
 
15
21
  def path
16
22
  @req.path
17
23
  end
18
24
 
25
+ def path_hash
26
+ @path_hash ||= begin
27
+ split = path.split('/')
28
+ split.shift
29
+
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
+
19
40
  def params
20
- @req.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
21
46
  end
22
47
 
23
48
  def render
24
49
  str = ERB.new(template).result(binding)
25
- parent ? parent.render.gsub("{{VIEW}}", str).gsub("{{view}}", str) : str
50
+ if parent
51
+ parent.render.gsub("{{VIEW}}", str).gsub("{{view}}", str)
52
+ else
53
+ str
54
+ end
26
55
  end
27
56
 
28
57
  def self.render(opts = {})
@@ -41,9 +70,7 @@ class ErbComponent
41
70
  end
42
71
 
43
72
  def template
44
- if template_file_path
45
- File.read template_file_path
46
- end
73
+ return File.read(template_file_path) if template_file_path
47
74
  fail "not found: #{template_file_path}"
48
75
  end
49
76
 
@@ -51,11 +78,11 @@ class ErbComponent
51
78
  m = m.to_s
52
79
  str = Kernel.const_defined?("#{self.class}::#{m}") ? "#{self.class}::#{m}" : m
53
80
  clazz = Kernel.const_get(str)
54
- opts = {path: path, params: params}
55
- opts.merge!(args[0]) if args.size > 0
56
- component = clazz.new(opts)
81
+ if args.size > 0
82
+ component = clazz.new(req, *args)
83
+ else
84
+ component = clazz.new(req)
85
+ end
57
86
  component.render
58
- rescue
59
- super
60
87
  end
61
88
  end
@@ -1,3 +1,3 @@
1
1
  class ErbComponent
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  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.6
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-20 00:00:00.000000000 Z
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: