rbexy 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de2d3389ec5f783be2e913921071bc0920baa342980360445e5756a8d6cd2d72
4
- data.tar.gz: 7dfdd99aa0852253e491889010f87c5a2ca828a57b16fe8749d9231135c785a2
3
+ metadata.gz: 563f7d14586235a18249e38e8859824b7952617b4984ab794e69963c218b41b2
4
+ data.tar.gz: a20d77a9a3d2fc439d425033fcb3230d18f59a98990a50be022569de163e67cf
5
5
  SHA512:
6
- metadata.gz: a6e669a7babe4bb38c7a31be00c0a98d1035c1c7fec980b713a4bb17a2ba461c127745a7b7fee50f6419830eb7e26bf505243662c08245973fac4a3c0ce20dd4
7
- data.tar.gz: 91c71f51bd42958d86a5584a85a0a634fb35306e95e4a022bc82508c2d5d7cb05df901197d276a870bbcab19b6d1d632a0de4a43518d39f2dd9b00b08559f316
6
+ metadata.gz: 48a530ceee5c8b30f7c41900db8b5b0658351a5ab397af004bc9b38c1047d1bb62eecc0ee4807bc992d690d50dabf5d291a15aadc7c27975cc6901bae8985ec1
7
+ data.tar.gz: 4e5713408292969214aeb92909f522dd59f823f847ece59bb016e55e0993eb2d3c51fd0434ebaab3447f939f32833600da4c466fdacb5d3632b4201dea7c7e13
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  spec/dummy/log/
2
2
  pkg/
3
+ spec/dummy/tmp/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rbexy (0.1.0)
4
+ rbexy (0.2.0)
5
5
  actionview (>= 5.0, < 7.0)
6
6
  activesupport (>= 5.0, < 7.0)
7
7
  railties (>= 5.0, < 7.0)
data/README.md CHANGED
@@ -375,6 +375,16 @@ Rbexy.configure do |config|
375
375
  end
376
376
  ```
377
377
 
378
+ Or in Rails you can customize the component provider just for a controller:
379
+
380
+ ```ruby
381
+ class ThingsController < ApplicationController
382
+ def rbexy_component_provider
383
+ MyComponentProvider.new
384
+ end
385
+ end
386
+ ```
387
+
378
388
  See `lib/rbexy/component_providers/` for example implementations.
379
389
 
380
390
  ## Usage outside of Rails
@@ -64,6 +64,14 @@ module Rbexy
64
64
  raise(ContextNotFound, "no parent context `#{name}`")
65
65
  end
66
66
 
67
+ def view_renderer
68
+ view_context.view_renderer
69
+ end
70
+
71
+ def component_name
72
+ self.class.name.underscore
73
+ end
74
+
67
75
  private
68
76
 
69
77
  attr_reader :view_context, :content_block, :old_lookup_context
@@ -88,14 +96,6 @@ module Rbexy
88
96
  LookupContext.new(paths, LookupContext.details_hash(existing_context))
89
97
  end
90
98
 
91
- def view_renderer
92
- view_context.view_renderer
93
- end
94
-
95
- def component_name
96
- self.class.name.underscore
97
- end
98
-
99
99
  def method_missing(meth, *args, &block)
100
100
  if view_context.respond_to?(meth)
101
101
  view_context.send(meth, *args, &block)
@@ -0,0 +1,20 @@
1
+ module Rbexy
2
+ module ComponentProviders
3
+ class NamespacedRbexyProvider < Rbexy::ComponentProviders::RbexyProvider
4
+ attr_reader :namespaces
5
+
6
+ def initialize(*namespaces)
7
+ @namespaces = namespaces
8
+ end
9
+
10
+ def find(name)
11
+ namespaces.each do |namespace|
12
+ result = super("#{namespace}::#{name}")
13
+ return result if result != nil
14
+ end
15
+
16
+ super
17
+ end
18
+ end
19
+ end
20
+ end
@@ -10,8 +10,6 @@ module Rbexy
10
10
  find(name).new(context, **props).render(&block)
11
11
  end
12
12
 
13
- private
14
-
15
13
  def find(name)
16
14
  ActiveSupport::Inflector.constantize("#{name}Component")
17
15
  rescue NameError => e
@@ -10,8 +10,6 @@ module Rbexy
10
10
  find(name).new(**props).render_in(context, &block)
11
11
  end
12
12
 
13
- private
14
-
15
13
  def find(name)
16
14
  ActiveSupport::Inflector.constantize("#{name}Component")
17
15
  rescue NameError => e
@@ -115,7 +115,7 @@ module Rbexy
115
115
  expression_inner_single_quote
116
116
  elsif scanner.scan(Patterns.open_tag_def)
117
117
  potential_expression_inner_tag
118
- elsif scanner.scan(Patterns.expression_content)
118
+ elsif expression_content?
119
119
  self.curr_expr += scanner.matched
120
120
  else
121
121
  raise SyntaxError, self
@@ -132,7 +132,7 @@ module Rbexy
132
132
  expression_inner_single_quote
133
133
  elsif scanner.scan(Patterns.open_tag_def)
134
134
  potential_expression_inner_tag
135
- elsif scanner.scan(Patterns.expression_content)
135
+ elsif expression_content?
136
136
  self.curr_expr += scanner.matched
137
137
  else
138
138
  raise SyntaxError, self
@@ -237,6 +237,7 @@ module Rbexy
237
237
  end
238
238
 
239
239
  def potential_expression_inner_tag
240
+ # binding.pry
240
241
  if self.curr_expr =~ Patterns.expression_internal_tag_prefixes
241
242
  tokens << [:EXPRESSION_BODY, curr_expr]
242
243
  self.curr_expr = ""
@@ -275,5 +276,16 @@ module Rbexy
275
276
  self.curr_expr += scanner.getch
276
277
  stack.pop unless curr_expr.end_with?('\\')
277
278
  end
279
+
280
+ def expression_content?
281
+ # Patterns.expression_content ends at `<` characters, because we need to
282
+ # separately scan for allowed open_tag_defs within expressions. We should
283
+ # support any found open_tag_ends as expression content, as that means the
284
+ # open_tag_def was not considered allowed (or stack would be inside
285
+ # :tag_def instead of :expression) so we should thus also consider the
286
+ # open_tag_end to just be a part of the expression (maybe its in a string,
287
+ # etc).
288
+ scanner.scan(Patterns.expression_content) || scanner.scan(Patterns.open_tag_end)
289
+ end
278
290
  end
279
291
  end
@@ -2,7 +2,6 @@ module Rbexy
2
2
  autoload :Component, "rbexy/component"
3
3
 
4
4
  module Rails
5
- autoload :TemplateHandler, "rbexy/rails/template_handler"
6
5
  autoload :Engine, "rbexy/rails/engine"
7
6
  end
8
7
  end
@@ -4,10 +4,17 @@ module Rbexy
4
4
  module Rails
5
5
  class Engine < ::Rails::Engine
6
6
  initializer "rbexy" do |app|
7
- ActionView::Template.register_template_handler(:rbx, Rbexy::Rails::TemplateHandler)
7
+ template_handler = ::Rails.version.to_f >= 6.0 ?
8
+ proc { |template, source| Rbexy.compile(source) } :
9
+ proc { |template| Rbexy.compile(template.source) }
8
10
 
9
- ActiveSupport.on_load :action_controller do
11
+ ActionView::Template.register_template_handler(:rbx, template_handler)
12
+
13
+ ActiveSupport.on_load :action_controller_base do
10
14
  helper Rbexy::ViewContextHelper
15
+ helper_method :rbexy_component_provider
16
+
17
+ def rbexy_component_provider; end
11
18
  end
12
19
 
13
20
  if defined?(ViewComponent)
@@ -11,7 +11,11 @@ module Rbexy
11
11
 
12
12
  DefaultTagBuilder = ActionView::Helpers::TagHelper::TagBuilder
13
13
 
14
- def self.create_tag_builder(context, provider = Rbexy.configuration.component_provider)
14
+ def self.create_tag_builder(context, provider = nil)
15
+ provider = provider ||
16
+ provider_from_context(context) ||
17
+ Rbexy.configuration.component_provider
18
+
15
19
  if provider
16
20
  ComponentTagBuilder.new(context, provider)
17
21
  else
@@ -19,6 +23,12 @@ module Rbexy
19
23
  end
20
24
  end
21
25
 
26
+ def self.provider_from_context(context)
27
+ if context.respond_to?(:rbexy_component_provider)
28
+ context.rbexy_component_provider
29
+ end
30
+ end
31
+
22
32
  def initialize(component_provider = nil)
23
33
  @rbexy_tag = self.class.create_tag_builder(self, component_provider)
24
34
  end
@@ -1,3 +1,3 @@
1
1
  module Rbexy
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbexy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Giancola
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-06 00:00:00.000000000 Z
11
+ date: 2020-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -224,6 +224,7 @@ files:
224
224
  - example.rb
225
225
  - lib/rbexy.rb
226
226
  - lib/rbexy/component.rb
227
+ - lib/rbexy/component_providers/namespaced_rbexy_provider.rb
227
228
  - lib/rbexy/component_providers/rbexy_provider.rb
228
229
  - lib/rbexy/component_providers/view_component_provider.rb
229
230
  - lib/rbexy/component_tag_builder.rb
@@ -235,7 +236,6 @@ files:
235
236
  - lib/rbexy/parser.rb
236
237
  - lib/rbexy/rails.rb
237
238
  - lib/rbexy/rails/engine.rb
238
- - lib/rbexy/rails/template_handler.rb
239
239
  - lib/rbexy/runtime.rb
240
240
  - lib/rbexy/version.rb
241
241
  - lib/rbexy/view_context_helper.rb
@@ -1,9 +0,0 @@
1
- module Rbexy
2
- module Rails
3
- class TemplateHandler
4
- def self.call(template, source)
5
- Rbexy.compile(source)
6
- end
7
- end
8
- end
9
- end