theo-rails 0.2.0 → 0.3.0

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 +46 -24
  3. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f522cd4a9045802867fb16443c798ed1cb1da0a07b20dfa00fe4c9b1f58c616
4
- data.tar.gz: 8373f7e46efb666a7cbd07b15f51dd5001375131b2f8fec3bcdd7adb0957c812
3
+ metadata.gz: a08de40e9205127777dc501ee41131f7c56b5d49ce3d793171a4f5fee114c087
4
+ data.tar.gz: 7285c684000a300aefd5c71b9b287656153303fb7eea410a4d59af474e21d494
5
5
  SHA512:
6
- metadata.gz: '081b4756de992bdfc8fd63a17f6c593d639b2c586bd5a3bfc9cc8555757fac559d59f24e81142edd2b55333f07e1f22ef10147c3fc16b6dc1d53e30f427a7fbc'
7
- data.tar.gz: 7221da0b1ef26ef372d5765a776bc89aa1af725b4595b02814b7832476edc05ee96ddde16d2877f9d8ffb16929f912bb82c7bf9014fc5efa56b4f9af541fc9d7
6
+ metadata.gz: b5e8ae5ee062f9891934034acaf20e6d652162acdb08c587a2330989b6b10a7fd19a674dbc4ad13a4ce7e370300f3b6cc18de1d990bb6d8ab452855ae4563108
7
+ data.tar.gz: aa2cee291cfc7ec96588b068cd52645e0502c53d87bec1f3c69d6e83e74879aede4aa47ef0b25c69219171a669d4ad6bcff430e3fef7de26efb6b9906c00de07
@@ -5,45 +5,67 @@ module Theo
5
5
  ATTRIBUTE = /(?:(?:(?<name>#{ATTRIBUTE_NAME.source})\s*=\s*#{ATTRIBUTE_VALUE.source})|(?<name>#{ATTRIBUTE_NAME.source}))/
6
6
  DYNAMIC_ATTRIBUTE = /(?:(?<name>#{ATTRIBUTE_NAME.source})\s*%=\s*#{ATTRIBUTE_VALUE.source})/
7
7
  ATTRIBUTES = /(?<attrs>(?:\s+#{ATTRIBUTE.source})*)/
8
- LITERAL_ATTRIBUTES = %i[path as yields].freeze
9
- PARTIAL_TAG = /(?<partial>_[\w-]+)/
10
- PARTIAL = /(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*>(?<content>.*?)<\/\k<partial>>)|(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*\/>)/im
8
+ LITERAL_ATTRIBUTES = %i[path as yields collection].freeze
9
+ PARTIAL_TAG = /(?<partial>_\w+)/
10
+ PARTIAL = /(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*>(?<content>.*?)<\/\k<partial>>)|(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*\/>)/
11
+ COMPONENT_TAG = /(?<component>[A-Z]\w+)/
12
+ COMPONENT = /(?:<#{COMPONENT_TAG.source}#{ATTRIBUTES.source}\s*>(?<content>.*?)<\/\k<component>>)|(?:<#{COMPONENT_TAG.source}#{ATTRIBUTES.source}\s*\/>)/
13
+ TEMPLATE = /(?:#{PARTIAL.source})|(?:#{COMPONENT.source})/m
11
14
  DYNAMIC_EXPRESSION = /^<%=([^%]*)%>$/
12
15
 
13
16
  class Theo
14
17
  def process(source)
15
18
  # Attributes
16
- source = source.gsub(DYNAMIC_ATTRIBUTE, '\k<name>="<%=\k<value>%>"')
19
+ source = source.gsub(DYNAMIC_ATTRIBUTE, '\k<name>="<%= \k<value> %>"')
17
20
 
18
21
  # Partials
19
- source.gsub(PARTIAL) do |_|
22
+ source.gsub(TEMPLATE) do |_|
20
23
  match = Regexp.last_match
21
- partial = (match[:partial]).delete_prefix('_')
24
+
25
+ partial = match[:partial]
26
+ component = match[:component]
27
+
22
28
  attributes = match[:attrs] || ''
23
- content = match[:content]&.strip
29
+ content = match[:content]
24
30
 
25
31
  attributes = process_attributes(attributes)
26
32
 
27
- partial = "#{attributes.delete(:path)}/#{partial}" if attributes[:path]
33
+ path = attributes.delete(:path)
28
34
 
29
- collection = ''
30
- if attributes[:collection]
31
- collection = attributes.delete(:collection)
35
+ collection = attributes.delete(:collection)
36
+ as = attributes.delete(:as)
32
37
 
33
- as = ''
34
- if attributes[:as]
35
- as = attributes.delete(:as)
36
- as = ", as: '#{as}'"
37
- end
38
- collection = ", collection: #{collection}#{as}"
39
- end
38
+ yields = attributes.delete(:yields)
39
+ yields = " |#{yields}|" if yields
40
+
41
+ locals = attributes.empty? ? '' : attributes.map { |k, v| "'#{k}': #{v}" }.join(', ')
40
42
 
41
- yields = "|#{attributes.delete(:yields)}|" if attributes[:yields]
43
+ if partial
44
+ partial = partial.delete_prefix('_')
42
45
 
43
- if content
44
- output = "<%= render '#{partial}', {#{attributes.map {|k,v| "'#{k}': #{v}"}.join(', ')}} do #{yields || ''} %>#{process(content)}<% end %>"
46
+ partial = "#{path}/#{partial}" if path
47
+
48
+ as = as ? ", as: '#{as}'" : ''
49
+ collection = ", collection: #{collection}#{as}" if collection
50
+
51
+ if content
52
+ locals = ", {#{locals}}" unless locals.empty?
53
+ output = "<%= render '#{partial}'#{locals} do#{yields} %>#{process(content)}<% end %>"
54
+ else
55
+ locals = ", locals: {#{locals}}" unless locals.empty?
56
+ output = "<%= render partial: '#{partial}'#{collection}#{locals} %>"
57
+ end
45
58
  else
46
- output = "<%= render partial: '#{partial}'#{collection}, locals: {#{attributes.map {|k,v| "'#{k}': #{v}"}.join(', ')}} %>"
59
+ component = "#{component}Component"
60
+
61
+ if content
62
+ output = "<%= render #{component}.new(#{locals}) do#{yields} %>#{process(content)}<% end %>"
63
+ elsif collection
64
+ locals = ", #{locals}" unless locals.empty?
65
+ output = "<%= render #{component}.with_collection(#{collection}#{locals}) %>"
66
+ else
67
+ output = "<%= render #{component}.new(#{locals}) %>"
68
+ end
47
69
  end
48
70
 
49
71
  output
@@ -57,7 +79,7 @@ module Theo
57
79
  .map do |attr|
58
80
  name = attr[:name].to_sym
59
81
  value = attr[:value]
60
- value = attribute(value) if LITERAL_ATTRIBUTES.exclude?(name)
82
+ value = attribute(value) unless LITERAL_ATTRIBUTES.include?(name)
61
83
  [name, value]
62
84
  end
63
85
  .to_h
@@ -67,7 +89,7 @@ module Theo
67
89
  # TODO: support attributes like "a<%= b %>c
68
90
 
69
91
  match = DYNAMIC_EXPRESSION.match(source)
70
- return match[1] if match
92
+ return match[1].strip if match
71
93
 
72
94
  "'#{source}'"
73
95
  end
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.2.0
4
+ version: 0.3.0
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-09-13 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: HTML-like template language for Rails with natural partial syntax
14
28
  email: jarek@jareklipski.com
15
29
  executables: []