rux 1.3.0 → 1.4.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: e2874b5336b991b08b274df04219d2c7b97247580f0587dfc3b6860e913d8af8
4
- data.tar.gz: ba582dd96d6c58da357295e60267f4db9a6cdf47ddc99aa2740d590a1cec530d
3
+ metadata.gz: f1e472f55eed0269091c2f5efa3a1d27fb1d16ed1959c32a84633be8b192940d
4
+ data.tar.gz: 46e848d6871ff5ea01638d6e50e24968ad3aa9867324580417151287ed932b3d
5
5
  SHA512:
6
- metadata.gz: c6c663d6d237fb6fc044dc2ebfa9c706d54eb5cdd3bc7a50011d82578ac7aab36a8f6c1ca466754f44bfed73ce708fc57bbd0ea3951681febd79f2730e7fd1d7
7
- data.tar.gz: fb52a3a10cb3db47817acc7d760210758bc733b3b74b4ebfc9d241039d8d38ce00d4622957fd26effa7593acefe4d9387ebce9bcc6bab7dd20d83e9523b62322
6
+ metadata.gz: 1681c3f2cedc43ee87559663ada3f92d9061ed004cb4ea100a588d847b7de3cf2e4a490ec23f5d0cd653602d3f0fee1c97045c9c786e38249663a73534ab559c
7
+ data.tar.gz: 94c7c21726d2b38750162fb36ceb1f8ff9321fe99425efb078b51a042568b5914e5821341bb45fa5b18f1f6df1040f2fa706edd7ce32b834207a0046e8ac0479
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 1.4.0
2
+ * Implement React-style contexts for passing arguments across arbitrary levels of component nesting.
3
+ - Define a new context via `Rux.create_context`.
4
+ - Read that context later via `Rux.use_context`.
5
+
1
6
  # 1.3.0
2
7
  * Automatically add generated files to an ignore file, eg. .gitignore.
3
8
  - Pass the --ignore-path=PATH flag to ruxc to indicate the file to update.
data/README.md CHANGED
@@ -268,6 +268,51 @@ Notice that the rux attribute "first-name" is passed to `MyComponent#initialize`
268
268
 
269
269
  Attributes on regular tags, i.e. non-component tags like `<div>` and `<span>`, are not modified. In other words, `<div data-foo="foo">` does _not_ become `<div data_foo="foo">` because that would be very annoying.
270
270
 
271
+ ## Context
272
+
273
+ Occasionally you might find it necessary to pass values across multiple component boundaries. A grandparent component might want to pass values to its grandchildren but not the parent components (i.e. the grandparent's direct children).
274
+
275
+ For example, imagine you'd like to support multiple themes on your website, like dark and light mode. While it would be possible to add a `theme:` argument to all your components, doing so could be quite invasive - you'd have to modify every single one.
276
+
277
+ Contexts are modeled after the React concept by the same name. They allow you to set values at one level of the component hierarchy and consume them at a deeper one, without having to pass the values through every intermediate level.
278
+
279
+ First, create a new context, passing an optional default value or default block. Note that the value stored in a context can be anything, i.e. any Ruby object.
280
+
281
+ ```ruby
282
+ # with a default value
283
+ ThemeContext = Rux.create_context("dark")
284
+
285
+ # with a default block
286
+ ThemeContext = Rux.create_context { "dark" }
287
+ ```
288
+
289
+ NOTE: passing a default block takes precedence over default values. In other words, if you pass both an argument _and_ a block to `create_context`, only the block will be used to produce default values.
290
+
291
+ The `create_context` method returns a component class. Rux requires that all components start with an uppercase letter, i.e. be a Ruby constant, which is why we assigned it to the `ThemeContext` constant in the example above. You can now render the context component like you would any other component:
292
+
293
+ ```ruby
294
+ <ThemeContext value="light">
295
+ <Heading>Welcome!</Heading>
296
+ <WelcomePage />
297
+ </ThemeContext>
298
+ ```
299
+
300
+ Now that we've defined a context and rendered it, any component rendered inside `ThemeContext` (i.e. any of its children) can access the context like this:
301
+
302
+ ```ruby
303
+ class WelcomePage < ViewComponent::Base
304
+ def call
305
+ theme = Rux.use_context(ThemeContext)
306
+
307
+ <div>
308
+ <h2 style={theme == "dark" ? "color: #FFF" : "color: #000"}>
309
+ Welcome to my site!
310
+ </h2>
311
+ </div>
312
+ end
313
+ end
314
+ ```
315
+
271
316
  ## How it Works
272
317
 
273
318
  Translating rux code (Ruby + HTML tags) into Ruby code happens in three phases: lexing, parsing, and emitting. The lexer phase is implemented as a wrapper around the lexer from the [Parser gem](https://github.com/whitequark/parser) that looks for specific patterns in the token stream. When it finds an opening HTML tag, it hands off lexing to the rux lexer. When the tag ends, the lexer continues emitting Ruby tokens, and so on.
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "use_context"
4
+ require "securerandom"
5
+
6
+ module Rux
7
+ class ContextBase
8
+ include UseContext::ContextMethods
9
+
10
+ class << self
11
+ attr_accessor :default_value, :default_value_block
12
+
13
+ def context_key
14
+ @context_key ||= "#{context_name}_context"
15
+ end
16
+
17
+ def context_name
18
+ @context_name ||= self.name || SecureRandom.hex
19
+ end
20
+ end
21
+
22
+ attr_reader :value
23
+
24
+ def initialize(**kwargs)
25
+ if kwargs.include?(:value)
26
+ @value = kwargs[:value]
27
+ else
28
+ @value = if self.class.default_value_block
29
+ self.class.default_value_block.call
30
+ else
31
+ self.class.default_value
32
+ end
33
+ end
34
+ end
35
+
36
+ def render_in(_view_context, &block)
37
+ provide_context(self.class.context_key, { value: value }) do
38
+ block.call if block
39
+ end
40
+ end
41
+ end
42
+
43
+ module Context
44
+ class << self
45
+ include UseContext::ContextMethods
46
+
47
+ def create(default_value = nil, &default_value_block)
48
+ Class.new(ContextBase).tap do |klass|
49
+ klass.default_value = default_value
50
+ klass.default_value_block = default_value_block
51
+ end
52
+ end
53
+
54
+ def use(context_class)
55
+ use_context(context_class.context_key, :value)
56
+ end
57
+ end
58
+ end
59
+ end
data/lib/rux/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rux
2
- VERSION = '1.3.0'
2
+ VERSION = '1.4.0'
3
3
  end
data/lib/rux.rb CHANGED
@@ -20,6 +20,7 @@ module Rux
20
20
  autoload :AST, 'rux/ast'
21
21
  autoload :Buffer, 'rux/buffer'
22
22
  autoload :Component, 'rux/component'
23
+ autoload :Context, 'rux/context'
23
24
  autoload :DefaultTagBuilder, 'rux/default_tag_builder'
24
25
  autoload :DefaultVisitor, 'rux/default_visitor'
25
26
  autoload :File, 'rux/file'
@@ -65,6 +66,14 @@ module Rux
65
66
  def library_paths
66
67
  @library_paths ||= []
67
68
  end
69
+
70
+ def create_context(...)
71
+ Rux::Context.create(...)
72
+ end
73
+
74
+ def use_context(...)
75
+ Rux::Context.use(...)
76
+ end
68
77
  end
69
78
 
70
79
  self.tag_builder = self.default_tag_builder
data/rux.gemspec CHANGED
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
13
13
  s.add_dependency 'onload', '~> 1.1'
14
14
  s.add_dependency 'parser', '~> 3.0'
15
15
  s.add_dependency 'unparser', '~> 0.8'
16
+ s.add_dependency 'use_context', '~> 1.2'
16
17
 
17
18
  s.require_path = 'lib'
18
19
  s.executables << 'ruxc'
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'context', type: :render do
4
+ FooContext = Rux.create_context("foo")
5
+ FooContextWithBlock = Rux.create_context { "foo from block" }
6
+
7
+ class FooComponent < ViewComponent::Base
8
+ def call
9
+ context = Rux.use_context(FooContext)
10
+ Rux::SafeString.new("<p>#{context}</p>")
11
+ end
12
+ end
13
+
14
+ class FooComponentWithBlock < ViewComponent::Base
15
+ def call
16
+ context = Rux.use_context(FooContextWithBlock)
17
+ Rux::SafeString.new("<p>#{context}</p>")
18
+ end
19
+ end
20
+
21
+ describe '#context_key' do
22
+ it 'uses the class name in the key' do
23
+ expect(FooContext.context_key).to eq("FooContext_context")
24
+ expect(FooContextWithBlock.context_key).to eq("FooContextWithBlock_context")
25
+ end
26
+
27
+ it "uses a random ID if the class doesn't have a name" do
28
+ no_name = Rux.create_context
29
+ expect(no_name.context_key).to match(/[a-f0-9-]{32}_context/)
30
+ end
31
+
32
+ it 'returns the same value when called multiple times' do
33
+ no_name = Rux.create_context
34
+ key = no_name.context_key
35
+ expect(no_name.context_key).to eq(key)
36
+ end
37
+ end
38
+
39
+ describe 'rendering' do
40
+ it 'uses the given context value' do
41
+ result = render(<<~RUX)
42
+ <FooContext value="bar">
43
+ <FooComponent />
44
+ </FooContext>
45
+ RUX
46
+
47
+ expect(result).to eq("<p>bar</p>")
48
+ end
49
+
50
+ it 'uses the default value when no value is provided' do
51
+ result = render(<<~RUX)
52
+ <FooContext>
53
+ <FooComponent />
54
+ </FooContext>
55
+ RUX
56
+
57
+ expect(result).to eq("<p>foo</p>")
58
+ end
59
+
60
+ it 'calls the default block when provided' do
61
+ result = render(<<~RUX)
62
+ <FooContextWithBlock>
63
+ <FooComponentWithBlock />
64
+ </FooContextWithBlock>
65
+ RUX
66
+
67
+ expect(result).to eq("<p>foo from block</p>")
68
+ end
69
+
70
+ it 'allows context to be temporarily overridden' do
71
+ result = render(<<~RUX)
72
+ <FooContext value="bar">
73
+ <FooComponent />
74
+ <FooContext value="baz">
75
+ <FooComponent />
76
+ </FooContext>
77
+ <FooComponent />
78
+ </FooContext>
79
+ RUX
80
+
81
+ expect(result).to eq("<p>bar</p><p>baz</p><p>bar</p>")
82
+ end
83
+ end
84
+ end
@@ -7,8 +7,7 @@ module ViewComponent
7
7
  end
8
8
 
9
9
  def to_s
10
- @component_instance.content = @content_block.call(@component_instance) if @content_block
11
- @component_instance.call
10
+ @component_instance.render_in(nil, &@content_block)
12
11
  end
13
12
  end
14
13
 
@@ -40,14 +39,18 @@ module ViewComponent
40
39
  end
41
40
  end
42
41
 
43
- attr_accessor :content
44
-
45
42
  def render(component, &block)
46
- if block
47
- component.content = block.call(component)
48
- end
43
+ component.render_in(nil, &block)
44
+ end
45
+
46
+ def render_in(_view_context, &block)
47
+ @content_block = block
48
+ content # fill in slots
49
+ call
50
+ end
49
51
 
50
- component.call
52
+ def content
53
+ @content ||= @content_block&.call(self)
51
54
  end
52
55
 
53
56
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rux
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
@@ -51,6 +51,20 @@ dependencies:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0.8'
54
+ - !ruby/object:Gem::Dependency
55
+ name: use_context
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.2'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.2'
54
68
  description: A jsx-inspired way to write view components.
55
69
  email:
56
70
  - camertron@gmail.com
@@ -80,6 +94,7 @@ files:
80
94
  - lib/rux/ast/tag_node.rb
81
95
  - lib/rux/ast/text_node.rb
82
96
  - lib/rux/buffer.rb
97
+ - lib/rux/context.rb
83
98
  - lib/rux/default_tag_builder.rb
84
99
  - lib/rux/default_visitor.rb
85
100
  - lib/rux/file.rb
@@ -96,6 +111,7 @@ files:
96
111
  - lib/rux/version.rb
97
112
  - lib/rux/visitor.rb
98
113
  - rux.gemspec
114
+ - spec/context_spec.rb
99
115
  - spec/parser/attributes_spec.rb
100
116
  - spec/parser/fragment_spec.rb
101
117
  - spec/parser/html_safety_spec.rb