theo-rails 0.3.1 → 0.4.1

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/theo-rails/theo.rb +20 -10
  3. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2b210d661f6fd028694497e83aa3b205c5fd89a6d3136fbc1faca2f4e4ddb29
4
- data.tar.gz: 734ea63e9962dac3a87a207b0cb82d165c0f4d54a6d791b5398c46dfda15cbc4
3
+ metadata.gz: 36c3ddae4b6c1dee1bbf309c1ab2c779c81ddaf511b35d7297a5f60b5b91d300
4
+ data.tar.gz: e3c6a104bca5f66f9f0aad687b6d295d55a0759a0cf816eaab0aff8d1408f647
5
5
  SHA512:
6
- metadata.gz: 85d866b6839ed5736b50dd5026b65e925f9dae25de88e24d47a9856e7454820fa9f7e8e855a022534ee636c2dfbc0b78584eb16f31543d97e327c1ff14a63b92
7
- data.tar.gz: 0b21d4fddb3b4a3618117d23fc2e846881ffc2f7e2b5667187ff33d220cea790ea18c6728a7d2167e228cfea83bed0d7dcab86210eb9cfbf4365ab25a2c8692d
6
+ metadata.gz: 78a5be9f8c14fbe5125a321a1df522976fb5282210a1a704be86e35121f71cdd5ac21588348a078b11e218228d363d67ae788837dc50d63257b8abef7377e74d
7
+ data.tar.gz: 85fc477ad21b22d2cef02f9a72ff86d59612bbeb97af0b70c5ff50fb33738e8554d44ac3efbe7a4b1cc8584aabcb397323266f4f398f63c99aa335c582306087
@@ -7,11 +7,8 @@ module Theo
7
7
  RESERVED_ATTRIBUTE_NAME = %w[alias and begin break case class def do else elsif end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield].to_set
8
8
  ATTRIBUTES = /(?<attrs>(?:\s+#{ATTRIBUTE.source})*)/
9
9
  LITERAL_ATTRIBUTES = %i[path as yields collection].freeze
10
- PARTIAL_TAG = /(?<partial>_\w+)/
11
- PARTIAL = /(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*>(?<content>.*?)<\/\k<partial>>)|(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*\/>)/
12
- COMPONENT_TAG = /(?<component>[A-Z]\w+)/
13
- COMPONENT = /(?:<#{COMPONENT_TAG.source}#{ATTRIBUTES.source}\s*>(?<content>.*?)<\/\k<component>>)|(?:<#{COMPONENT_TAG.source}#{ATTRIBUTES.source}\s*\/>)/
14
- TEMPLATE = /(?:#{PARTIAL.source})|(?:#{COMPONENT.source})/m
10
+ PARTIAL_TAG = /(?:(?<partial>[A-Z]\w+)|(?<partial>_[\w-]+))/
11
+ PARTIAL = /(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*>(?<content>.*?)<\/\k<partial>>)|(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*\/>)/m
15
12
  DYNAMIC_EXPRESSION = /^<%=([^%]*)%>$/
16
13
 
17
14
  class Theo
@@ -29,11 +26,10 @@ module Theo
29
26
  end
30
27
 
31
28
  # Partials
32
- source.gsub(TEMPLATE) do |_|
29
+ source.gsub(PARTIAL) do |_|
33
30
  match = Regexp.last_match
34
31
 
35
32
  partial = match[:partial]
36
- component = match[:component]
37
33
 
38
34
  attributes = match[:attrs] || ''
39
35
  content = match[:content]
@@ -50,8 +46,11 @@ module Theo
50
46
 
51
47
  locals = attributes.empty? ? '' : attributes.map { |k, v| "'#{k}': #{v}" }.join(', ')
52
48
 
53
- if partial
54
- partial = partial.delete_prefix('_')
49
+ is_component = view_component_exists?(partial)
50
+ is_partial = !is_component
51
+
52
+ if is_partial
53
+ partial = partial.delete_prefix('_').underscore
55
54
 
56
55
  partial = "#{path}/#{partial}" if path
57
56
 
@@ -66,7 +65,7 @@ module Theo
66
65
  output = "<%= render partial: '#{partial}'#{collection}#{locals} %>"
67
66
  end
68
67
  else
69
- component = "#{component}Component"
68
+ component = "#{partial}Component"
70
69
 
71
70
  if content
72
71
  output = "<%= render #{component}.new(#{locals}) do#{yields} %>#{process(content)}<% end %>"
@@ -104,6 +103,17 @@ module Theo
104
103
  "'#{source}'"
105
104
  end
106
105
 
106
+ def view_component_loaded?
107
+ @view_component_loaded ||= Object.const_defined?('ViewComponent')
108
+ end
109
+
110
+ def view_component_exists?(component)
111
+ return unless view_component_loaded?
112
+
113
+ is_capitalized = /^[A-Z]/.match?(component)
114
+ is_capitalized && Object.const_defined?("#{component}Component")
115
+ end
116
+
107
117
  def call(template, source = nil)
108
118
  theo = process(source)
109
119
 
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: theo-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jarek Lipski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-06 00:00:00.000000000 Z
11
+ date: 2024-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement