erb_component 0.1.4 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40483d823e533c0e6f706d8691d1e8a5e26a3f5ef15654b7e31c3c7531c07a2e
4
- data.tar.gz: 647bccf54f21a52b81824057d845145607ef60a335536234fd6f56c73870d99b
3
+ metadata.gz: e662fbd72853e9bfd5788ceb66debf03d28d8bb13dcbd4ed18142dc26d29332f
4
+ data.tar.gz: 0c3d0e0435280f432bc54bb14c11b1cda6e460fb678e23b53e49e6ed1ea83f1b
5
5
  SHA512:
6
- metadata.gz: e5b1ee9b8daf98072cdfc949ad78189f8c6edb8d8159a79b49c57f41be5b94fce8412a29c64c838eed9b720042fffd0f348a81532338e4027c0918bfcf4ec659
7
- data.tar.gz: dd2dbc6e8b2311054dfa0b39e5588493b11804aeb91d1ccaf4a6d33ff5b37f69e008e1428c247ace942a6df420a7d663a6d8c5998106ff52b33273120271361c
6
+ metadata.gz: 6357e20fb5f55c3e4d6813ff0fb937fba43faa0b15c5fe6cfb340ad6b427e032bee671a1cb0828dd019c658b26811e4738a2510c2900bb9334838847110dedc0
7
+ data.tar.gz: 71bceefd23ee82ee445bf4ec81d4cd6852fd2476b26c9037639360792b523cbd100d83e873b180df221d19a9aa915004d9f67c79046305843dff1160e66b2107
@@ -1,52 +1,59 @@
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
 
8
11
  attr_reader :req, :parent
9
12
 
10
- def initialize(req)
13
+ def initialize(req, opts = {})
11
14
  @req = req
12
- @parent = self.class.superclass == ErbComponent ? nil : self.class.superclass.new(req)
13
- end
14
-
15
- def path
16
- @req.path
17
- end
18
-
19
- def params
20
- @req.params
15
+ @template = opts[:template]
16
+ begin
17
+ @parent = self.class.superclass == ErbComponent ? nil : self.class.superclass.new(req)
18
+ if @parent && !(parent.template['{{VIEW}}'] || parent.template['{{view}}'])
19
+ @parent = parent.parent
20
+ end
21
+ rescue ArgumentError
22
+ end
21
23
  end
22
24
 
23
25
  def render
24
26
  str = ERB.new(template).result(binding)
25
- parent ? parent.render.gsub("{{VIEW}}", str).gsub("{{view}}", str) : str
27
+ if parent
28
+ parent.render.gsub("{{VIEW}}", str).gsub("{{view}}", str)
29
+ else
30
+ str
31
+ end
26
32
  end
27
33
 
28
34
  def self.render(opts = {})
29
35
  new(opts).render
30
36
  end
31
37
 
38
+ def self.call(req, opts = {})
39
+ new(req, opts).render
40
+ end
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
+
50
+ def template_file_path
51
+ self.class.current_file.gsub('.rb', '.erb')
52
+ end
53
+
32
54
  def template
33
- 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}"
39
- end
40
-
41
- def method_missing(m, *args, &block)
42
- m = m.to_s
43
- str = Kernel.const_defined?("#{self.class}::#{m}") ? "#{self.class}::#{m}" : m
44
- 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)
48
- component.render
49
- rescue
50
- super
55
+ return @template if @template
56
+ return File.read(template_file_path) if template_file_path
57
+ fail "not found: #{template_file_path}"
51
58
  end
52
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
@@ -1,3 +1,3 @@
1
1
  class ErbComponent
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.9"
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.4
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-11-19 00:00:00.000000000 Z
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: