erb_component 0.1.2 → 0.1.7

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: ee2b998e877e3c08de6cb4cbda5260c0c31d880ec3dd16ac384e15f6748bfecb
4
- data.tar.gz: d7ddde77ae4ed69ac45288207562d2e7c1138e8f88e61e30e561ee964b6872b7
3
+ metadata.gz: 10aa5ea6bca7d3b177903d1aa535be7e306d7a0e939ca05fcb021ccbfff9d248
4
+ data.tar.gz: e0fcaeed8b0aae415ed31fc13d1911ed1fa0bdd5843218320adb9dbf5874046c
5
5
  SHA512:
6
- metadata.gz: cfc2d6b77717d164d6acb71c6080b191fe2e1e5c7beb6b7c338aed4f2b69a23eae96720a55a1e1aab7cc442a723716807c23bd81d8f2c6690130ac134ce1d1e9
7
- data.tar.gz: 735687627b740aeb6f405daa7e6b723ba9ebd0778c23f9b5f1b6034fd8cfc3f96602bba69dea446fb8a6149930ecef494a3a8aa6ec2f30a0fb1ea26f1ad394d9
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; end
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
- attr_reader :params, :path, :parent
25
+ def path_hash
26
+ @path_hash ||= begin
27
+ split = path.split('/')
28
+ split.shift
8
29
 
9
- def initialize(opts = {})
10
- @params = opts[:params] || {}
11
- @path = opts[:path]
12
- @parent = self.class.superclass == ErbComponent ? nil : self.class.superclass.new(opts)
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
- parent ? parent.render.gsub("{{VIEW}}", str) : str
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 template
61
+ def template_file_path
25
62
  file_name = "#{self.class.name.underscore}.erb"
26
- a = "components/#{file_name}"
27
- return File.read a if File.exists? a
28
- a = "pages/#{file_name}"
29
- return File.read a if File.exists? a
30
- fail "not found: #{file_name}"
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
- possible_const = "#{self.class}::#{m}"
35
- clazz = if Kernel.const_defined?(possible_const)
36
- Kernel.const_get possible_const
37
- else
38
- Kernel.const_get m.to_s
39
- end
40
- opts = {path: path, params: params}
41
- opts.merge!(args[0]) if args.size > 0
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
@@ -1,3 +1,3 @@
1
1
  class ErbComponent
2
- VERSION = "0.1.2"
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.2
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-18 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: