svelte-on-rails 7.1.3 → 8.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 970ffe37f898ba6c9f0b9069ae2116c5519f79c37d657e41e395b702a17e3863
4
- data.tar.gz: 2268e86265ba09a36a0c3513263985e670eae3ff06261b7799211161489bbac2
3
+ metadata.gz: b2251e235c9c3ef4744e197e276e77ada5f0530baaf3b17ecbf9fc09a130cf98
4
+ data.tar.gz: b4359024b0fa5ce9ebf02b2384b1ae7931cbb4982670a2a3a000c8b2cdd20d42
5
5
  SHA512:
6
- metadata.gz: 668fc5b19bae1d1da87d864a081e6c5a7eb201f0f5b3df775c754d4c03ebc6c56b5eff69019d9508e9fedf47b399c1ae77c0eef4bbe3cc159fe0822afa2cfc7f
7
- data.tar.gz: 736e4de6627a22755057fc5909ce87f642be1c80cac55bf6d74a461d3b02b23065935951779f989abcd9264dd6602df0f6bcfabb8746e30cafae9ce950c70d48
6
+ metadata.gz: ab0dd3ba7377a4e485840bdddca3425ecacec267d98a37c74224450d2acffd34265dbb6021eaa443ced8e92205b70b4bfac6bc6e42d060670868730d81b7d586
7
+ data.tar.gz: c665f45bbe57aab35cec4601dff88160a6818ad4f7055b8163d26b470feec82c76f75c024779913fc96bfe67aa4f6cfce451f22f78165b051379adf3b6d9dcd3
@@ -44,7 +44,7 @@ module SvelteOnRails
44
44
  end
45
45
 
46
46
  def redis_cache_store
47
- (@configs[:redis_cache_store] || {}).with_indifferent_access
47
+ (@configs[:redis_cache_store] || {}).transform_keys(&:to_sym)
48
48
  end
49
49
 
50
50
  def redis_instance
@@ -3,6 +3,8 @@ module SvelteOnRails
3
3
 
4
4
  module Lib
5
5
  class ViewHelperSupport
6
+
7
+ RENDER_OPTIONS = %i[ssr hydrate debug cache_key expires_in]
6
8
  attr_reader :filename, :options, :html_options, :request, :conf
7
9
 
8
10
  def initialize(file, props, html_options, options, request, caching = false)
@@ -20,7 +22,7 @@ module SvelteOnRails
20
22
  @options, @html_options = prepare_options(
21
23
  options,
22
24
  html_options,
23
- %i[ssr hydrate debug cache_key expires_in]
25
+ RENDER_OPTIONS
24
26
  )
25
27
  @request = request
26
28
  @ssr = determine_ssr
@@ -110,6 +112,70 @@ module SvelteOnRails
110
112
  end
111
113
  end
112
114
 
115
+ def render_cached(view_context, &block)
116
+
117
+ cached_content = conf.redis_instance.get(cache_key)
118
+
119
+ # debug
120
+
121
+ if debug?
122
+
123
+ debug_log("Rendering component: «#{filename}», cache_key: «#{custom_cache_key}»")
124
+ debug_log("Redis configuration: #{conf.redis_cache_store}")
125
+ ttl = conf.redis_instance.ttl(cache_key)
126
+
127
+ key_stat = if cached_content.present?
128
+ 'has content'
129
+ elsif conf.redis_instance.exists(cache_key)
130
+ 'exists but no content'
131
+ else
132
+ 'not exists'
133
+ end
134
+
135
+ ttl_stat = if conf.redis_instance.exists(cache_key)
136
+ ", ttl was: #{ttl} seconds, now set to: #{redis_expiration_seconds} seconds"
137
+ end
138
+
139
+ debug_log("Cache key: «#{cache_key}» (#{key_stat}#{ttl_stat})")
140
+
141
+ end
142
+
143
+ # increase expired time
144
+
145
+ conf.redis_instance.expire(cache_key, redis_expiration_seconds)
146
+
147
+ # render
148
+
149
+ log_message = '?'
150
+ res = if cached_content
151
+ log_message = "Returned #{filename}.svelte from cache"
152
+ cached_content.html_safe
153
+ else
154
+ log_message = "Rendered #{filename}.svelte and stored to cache"
155
+ debug_log("cache recalculating for key: #{cache_key}")
156
+ r = view_context.instance_eval(&block)
157
+ debug_log("cache recalculated")
158
+
159
+ conf.redis_instance.set(cache_key, r)
160
+ r
161
+ end
162
+ log_rendering(log_message)
163
+
164
+ res
165
+
166
+ end
167
+
168
+ def render(view_context, &block)
169
+
170
+ debug_log("Rendering component: #{filename}")
171
+
172
+ r = view_context.instance_eval(&block)
173
+
174
+ log_rendering("Rendered #{filename}.svelte #{'as empty element that will be mounted on the client side' unless ssr?}")
175
+
176
+ r
177
+ end
178
+
113
179
  private
114
180
 
115
181
  def determine_ssr
@@ -135,7 +201,7 @@ module SvelteOnRails
135
201
  ].compact.join('-')
136
202
 
137
203
  @cache_key_primary = [
138
- conf.redis_cache_store[:namespace] ? conf.redis_cache_store[:namespace] : "svelte-on-rails:#{Rails.env}",
204
+ conf.redis_cache_store[:namespace] ? conf.redis_cache_store[:namespace] : "svelte-on-rails:#{Rails.env rescue 'unknown'}",
139
205
  filename_part,
140
206
  ].join(':')
141
207
 
@@ -2,80 +2,47 @@
2
2
  module SvelteOnRails
3
3
  module ViewHelpers
4
4
 
5
- def svelte_component(path, props = {}, html: {}, options: {}, _props: {})
5
+ def svelte_component_ruby2(path, *args)
6
6
 
7
- prp = validate_props(props, _props)
7
+ props, html, options = validate_ruby2_props(args)
8
8
 
9
- support = SvelteOnRails::Lib::ViewHelperSupport.new(path, prp, html, options, request, false)
9
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, false)
10
10
 
11
- support.debug_log("Rendering component: #{path}")
12
- log_message = '?'
13
-
14
- log_message = "Rendered #{support.filename}.svelte #{'as empty element that will be mounted on the client side' unless support.ssr?}"
15
- res = render_component(support)
16
-
17
- support.log_rendering(log_message)
18
-
19
- res
11
+ support.render(self) do
12
+ render_component(support)
13
+ end
20
14
 
21
15
  end
22
16
 
23
- def cached_svelte_component(path, props = {}, html: {}, options: {}, _props: {})
24
-
25
- prp = validate_props(props, _props)
17
+ def cached_svelte_component_ruby2(path, *args)
26
18
 
27
- support = SvelteOnRails::Lib::ViewHelperSupport.new(path, prp, html, options, request, true)
19
+ props, html, options = validate_ruby2_props(args)
28
20
 
29
- log_message = '?'
30
- redis = support.conf.redis_instance
31
- cached_content = redis.get(support.cache_key)
21
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, true)
32
22
 
33
- # TTL
34
- if support.debug?
35
-
36
- support.debug_log("Rendering component: «#{filename}», cache_key: «#{support.custom_cache_key}»")
37
- support.debug_log("Redis configuration: #{support.conf.redis_cache_store}")
38
- ttl = redis.ttl(support.cache_key)
23
+ support.render_cached(self) do
24
+ render_component(support)
25
+ end
39
26
 
40
- key_stat = if cached_content.present?
41
- 'has content'
42
- elsif redis.exists(support.cache_key)
43
- 'exists but no content'
44
- else
45
- 'not exists'
46
- end
27
+ end
47
28
 
48
- ttl_stat = if redis.exists(support.cache_key)
49
- ", ttl was: #{ttl} seconds, now set to: #{support.redis_expiration_seconds} seconds"
50
- end
29
+ def svelte_component(path, props = {}, html: {}, options: {})
51
30
 
52
- support.debug_log("Cache key: «#{support.cache_key}» (#{key_stat}#{ttl_stat})")
31
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, false)
53
32
 
33
+ support.render(self) do
34
+ render_component(support)
54
35
  end
55
36
 
56
- redis.expire(support.cache_key, support.redis_expiration_seconds)
57
-
58
- res = if cached_content
59
- log_message = "Returned #{support.filename}.svelte from cache"
60
- cached_content.html_safe
61
- else
62
- log_message = "Rendered #{support.filename}.svelte and stored to cache"
63
- support.debug_log("cache recalculating for key: #{support.cache_key}")
64
- r = render_component(support)
65
- support.debug_log("cache recalculated")
66
-
67
- # redis.scan_each(match: "#{support.cache_key_primary}:*") do |key|
68
- # redis.del(key)
69
- # support.debug_log("deleted cache, pattern: «#{support.cache_key_primary}:*», key: #{key.sub("#{support.cache_key_primary}:", '[..]')}")
70
- # end
37
+ end
71
38
 
72
- redis.set(support.cache_key, r)
73
- r
74
- end
39
+ def cached_svelte_component(path, props = {}, html: {}, options: {})
75
40
 
76
- support.log_rendering(log_message)
41
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, true)
77
42
 
78
- res
43
+ support.render_cached(self) do
44
+ render_component(support)
45
+ end
79
46
 
80
47
  end
81
48
 
@@ -98,18 +65,32 @@ module SvelteOnRails
98
65
  end
99
66
  end
100
67
 
101
- def validate_props(props, _props)
102
- if props.present? && _props.present?
103
- raise "you can only pass props as the first argument OR use the _props keyword argument. the latter only is made as workaround for Apps <= ruby-3"
104
- end
105
- if RUBY_VERSION.split('.').first.to_i >= 3 && _props.present?
106
- raise "The _props keyword-argument is only meant as workaround for Apps <= ruby-3 because of avoiding misinterpreting hash as keyword arguments"
68
+ def validate_ruby2_props(args)
69
+ last_is_kwargs = false
70
+ kw_err = false
71
+
72
+ if args.length > 2
73
+ kw_err = true
74
+ elsif args.length >= 1
75
+ if args.last.keys & [:html, :options]
76
+ last_is_kwargs = true
77
+ if (args.last.keys - [:html, :options]).present?
78
+ kw_err = true
79
+ end
80
+ end
107
81
  end
108
- if props.present?
109
- props
110
- else
111
- _props
82
+
83
+ if kw_err
84
+ raise "Invalid arguments: First argument can be a hash as svelte-properties, second and third arguments are keyword arguments: :html and/or :options"
112
85
  end
86
+
87
+ first_is_props = args.length == 1 && !last_is_kwargs || args.length == 2
88
+
89
+ [
90
+ (first_is_props ? args.first : nil),
91
+ (last_is_kwargs ? args[1][:html] : nil),
92
+ (last_is_kwargs ? args[1][:options] : nil),
93
+ ]
113
94
  end
114
95
 
115
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svelte-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.1.3
4
+ version: 8.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair