erb_component 0.1.3 → 0.1.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 047edea23f0069c76f0c151b62a2316678d3dd6930bba6d451f29fed1ff9624e
4
- data.tar.gz: 2a33aca807d6852f69df70f0430e80b848968e91d0eced171826a1a99bfacdbe
3
+ metadata.gz: d4becb713a71333702f1e9666fca7490a928b957269cf5129fd78e6670d73edd
4
+ data.tar.gz: e74b2838a3144406b64f07807d40da29bd52d1a833bcec7e9525c028941fa298
5
5
  SHA512:
6
- metadata.gz: e943437c69d2b2c2f85cd2b162018905401e7fddf64849449f1f5a18eefc62529194a367f263e25e4fb112509a4538e66673f13bf00f5f9d4fa13f16cf960633
7
- data.tar.gz: 9f02bad3f0abe2e5afb1e345040d9dc1d07b6883d4080eb7dd250a570eda4036af7b3b09df66699b01716e338618dd7bb911b0162c0395c07adfe09acedbf55a
6
+ metadata.gz: 294198283f9d87a012b24fb7ae8e5d62ee1769180478170547daeed9f07069c76160801076c5b8247d20333aac57c2fb277a1a9fc9858e9c57691a8e1dc98dce
7
+ data.tar.gz: 18fec35a95417996d5b9e3590186acd3540e69c2ddedee49409d17ee1cdd5c842cce39bec3d487bba7011bf9b178686e988f0bb249e15136ad13c1c5a1c70c19
@@ -7,46 +7,90 @@ 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(opts)
12
+ @template = opts[:template]
13
+ begin
14
+ @parent = self.class.superclass == ErbComponent ? nil : self.class.superclass.new(req)
15
+ if @parent && !(parent.template['{{VIEW}}'] || parent.template['{{view}}'])
16
+ @parent = parent.parent
17
+ end
18
+ rescue ArgumentError
19
+ end
13
20
  end
14
21
 
15
22
  def path
16
23
  @req.path
17
24
  end
18
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
+
19
41
  def params
20
- @req.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
21
48
  end
22
49
 
23
50
  def render
24
51
  str = ERB.new(template).result(binding)
25
- parent ? parent.render.gsub("{{VIEW}}", str).gsub("{{view}}", str) : str
52
+ if parent
53
+ parent.render.gsub("{{VIEW}}", str).gsub("{{view}}", str)
54
+ else
55
+ str
56
+ end
26
57
  end
27
58
 
28
59
  def self.render(opts = {})
29
60
  new(opts).render
30
61
  end
31
62
 
32
- def template
63
+ def self.call(req, opts = {})
64
+ new(req, opts).render
65
+ end
66
+
67
+ def template_file_path
33
68
  file_name = "#{self.class.name.underscore}.erb"
34
- a = "components/#{file_name}"
35
- return File.read a if File.exists? a
36
- a = "pages/#{file_name}"
37
- return File.read a if File.exists? a
38
- fail "not found: #{file_name}"
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
76
+ end
77
+
78
+ def template
79
+ return @template if @template
80
+ return File.read(template_file_path) if template_file_path
81
+ fail "not found: #{template_file_path}"
39
82
  end
40
83
 
41
84
  def method_missing(m, *args, &block)
85
+ return super unless m.to_s[0].upcase == m.to_s[0]
42
86
  m = m.to_s
43
87
  str = Kernel.const_defined?("#{self.class}::#{m}") ? "#{self.class}::#{m}" : m
44
88
  clazz = Kernel.const_get(str)
45
- opts = {path: path, params: params}
46
- opts.merge!(args[0]) if args.size > 0
47
- component = clazz.new(opts)
89
+ if args.size > 0
90
+ component = clazz.new(req, *args)
91
+ else
92
+ component = clazz.new(req)
93
+ end
48
94
  component.render
49
- rescue
50
- super
51
95
  end
52
96
  end
@@ -1,3 +1,3 @@
1
1
  class ErbComponent
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.8"
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.3
4
+ version: 0.1.8
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-19 00:00:00.000000000 Z
11
+ date: 2019-12-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: React-style front-end components but for ERB?
14
14
  email: