rblade 1.0.0 → 1.0.2

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: 76b47c51f5796359252d33fe3865bb7189cd4de068579fee57520c881879755d
4
- data.tar.gz: ba1288cd0382290deb526f4faf4c5fe7c15d78372fca5248d41b4861f1ac6abb
3
+ metadata.gz: 80db67de9379396f85ba9450898e55f81b563cb1ca1ace73080719ad555455da
4
+ data.tar.gz: 9cc81c55f3893fbe1628dbe29c0d1dae16bdd65a789451e7f79ca6221e857488
5
5
  SHA512:
6
- metadata.gz: 4783a57cbe6b7f2173f0435b2024ed3c83dff68819637d2eb759addd7ca485fd7b6d3c89987c8a07ff2561bded344037161bbb67b7bbc8147575e9adc568ce1d
7
- data.tar.gz: 91f27168157d7448e7e26979a4dc60a731a6471027a8c5ddceb5ac4e23d51f3b39de0d61a2ba1970ec58bc726ccdb3316ed5e6389c463b63c61dd358e8e64f2f
6
+ metadata.gz: 50092cc06cf7104210d23a47044f9ce7de728812e2c561120d59434480f6fb7cc5ca4e1ef299d3a7bf7368af45ffe8b113299c744eb14679e9a2572d4923610c
7
+ data.tar.gz: fa8797d09d7621f8122b8460c634e94e6db39ae6940a909e516ee20ef7d2ef1ab9963c2f549aff2a0a002a76899fc79fd5ff8e9b2bceb862364417c3a01af05f
data/CHANGELOG.md CHANGED
@@ -1,4 +1,11 @@
1
- ## 1.0.0 [UNRELEASED]
1
+ ## 1.0.2 [2024-08-09]
2
+ - Fix non-string `@props` defaults being converted to slots
3
+
4
+ ## 1.0.1 [2024-08-09]
5
+ - Automatically detect when properties are used as slots
6
+ - Change `_required` to `required` in @props
7
+
8
+ ## 1.0.0 [2024-08-06]
2
9
  - Add quick reference and examples
3
10
  - Add @shouldRender directive
4
11
  - Add support for ERB style `<%==` unsafe prints
data/README.md CHANGED
@@ -5,11 +5,25 @@ RBlade is a simple, yet powerful templating engine for Ruby on Rails, inspired b
5
5
 
6
6
  RBlade template files use the `.rblade` file extension and are typically stored in the `app/views` directory.
7
7
 
8
- <a name="displaying-data"></a>
8
+
9
+ <a name="getting-started"></a>
10
+ ## Getting Started
11
+
12
+ Add RBlade to your Rails project by adding it to your Gemfile:
13
+
14
+ ```
15
+ bundle add rblade
16
+ ```
17
+
18
+ RBlade will automatically be detected and start parsing templates ending in `.rblade`.
19
+
20
+ For a quick overview of RBlade's capabilities, refer to the [reference file](REFERENCE.md) or take a look at the [examples](examples/).
21
+
22
+ <a name="table-of-contents"></a>
9
23
  ## Table of Contents
10
24
  - [RBlade Templates](#rblade-templates)
11
- * [Table of Contents](#table-of-contents)
12
25
  * [Getting Started](#getting-started)
26
+ * [Table of Contents](#table-of-contents)
13
27
  * [Displaying Data](#displaying-data)
14
28
  + [HTML Entity Encoding](#html-entity-encoding)
15
29
  + [RBlade and JavaScript Frameworks](#rblade-and-javascript-frameworks)
@@ -45,20 +59,6 @@ RBlade template files use the `.rblade` file extension and are typically stored
45
59
  + [Method Field](#method-field)
46
60
  * [Stacks](#stacks)
47
61
 
48
- <a name="getting-started"></a>
49
- ## Getting Started
50
-
51
- Add RBlade to your Rails project by adding it to your Gemfile:
52
-
53
- ```
54
- bundle add rblade
55
- ```
56
-
57
- RBlade will automatically be detected and start parsing templates ending in `.rblade`.
58
-
59
- For a quick overview of RBlade's capabilities, refer to the [reference file](REFERENCE.md) or take a look at the [examples](examples/README.md).
60
-
61
-
62
62
  <a name="displaying-data"></a>
63
63
  ## Displaying Data
64
64
 
@@ -475,11 +475,11 @@ You can define a component's data properties using a `@props` directive at the t
475
475
 
476
476
  ```rblade
477
477
  {{-- alert.rblade --}}
478
- @props({type: "warning", message: _required})
478
+ @props({type: "warning", message: required})
479
479
  <div class="{{ type }}">{{ message }}</div>
480
480
  ```
481
481
 
482
- The `@props` directive accepts a Hash where the key is the name of the attribute, and the value is the default value for the property. You can use the special `_required` value to represent a property with no default that must always be defined:
482
+ The `@props` directive accepts a Hash where the key is the name of the attribute, and the value is the default value for the property. You can use the special `required` value to represent a property with no default that must always be defined:
483
483
 
484
484
  ```rblade
485
485
  {{-- This will give an error because the alert component requires a message propery --}}
@@ -489,7 +489,7 @@ The `@props` directive accepts a Hash where the key is the name of the attribute
489
489
  All properties in the `@props` directive are automatically removed from `attributes`. Properties with names that aren't valid Ruby variable names or are Ruby reserved keywords are not created as local variables. However, you can reference them via the `attributes` local variable:
490
490
 
491
491
  ```rblade
492
- @props({"for": _required, "data-value": nil})
492
+ @props({"for": required, "data-value": nil})
493
493
  <div>{{ attributes[:for] }} {{ attributes[:'data-value'] }}</div>
494
494
  ```
495
495
 
@@ -681,7 +681,7 @@ Sometimes a component may need to render multiple different slots in different l
681
681
 
682
682
  ```rblade
683
683
  {{-- /app/views/components/alert.rblade --}}
684
- @props({title: _required})
684
+ @props({title: required})
685
685
  <span class="alert-title">{{ title }}</span>
686
686
  <div class="alert alert-danger">
687
687
  {{ slot }}
@@ -737,8 +737,8 @@ To interact with slot attributes, you can access the `attributes` property of th
737
737
 
738
738
  ```rblade
739
739
  @props({
740
- "heading": _required,
741
- "footer": _required,
740
+ "heading": required,
741
+ "footer": required,
742
742
  })
743
743
 
744
744
  <div {{ attributes.class('border') }}>
data/REFERENCE.md CHANGED
@@ -31,7 +31,7 @@ By default, RBlade will look for components in the `app/views/components` folder
31
31
  | `<x-name attribute/>` | Pass an attribute to a component with value `true` |
32
32
  | `<x-name {{ attributes }}/>` | Pass attributes to a child component |
33
33
  | `@props({header: "Header"})` | Remove `header` from the attributes Hash and introduce it as a local variable, using the specified value as a default |
34
- | `@props({header: _required})` | Remove `header` from the attributes Hash and introduce it as a local variable, raising an error if it is not set |
34
+ | `@props({header: required})` | Remove `header` from the attributes Hash and introduce it as a local variable, raising an error if it is not set |
35
35
  | `{{ slot }}` | Output the block content passed into the current component |
36
36
  | `<x-name><x-slot::header><h1>Header</h1><//>Content<//>` | Pass a named block to a component |
37
37
  | `{{ header }}` | Output the contents of a named block |
@@ -1,4 +1,4 @@
1
- @props({alert: _required})
1
+ @props({alert: required})
2
2
  @shouldRender(alert.present?)
3
3
  <p class="py-2 px-3 bg-amber-100 border border-amber-500">
4
4
  @@alert
@@ -1,4 +1,4 @@
1
- @props({title: _required})
1
+ @props({title: required})
2
2
  <!DOCTYPE html>
3
3
  <html lang="en">
4
4
  <head>
@@ -11,7 +11,7 @@ module RBlade
11
11
  "unless(#{args[0]});return'';end;"
12
12
  end
13
13
 
14
- def compileProps args
14
+ def compileProps args, tokens
15
15
  if args&.count != 1
16
16
  raise StandardError.new "Props statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
17
17
  end
@@ -19,14 +19,20 @@ module RBlade
19
19
  props = extractProps args[0]
20
20
  props.map do |key, value|
21
21
  compiled_code = ""
22
- if value == "_required"
23
- compiled_code << "if !attributes.has?(:'#{RBlade.escape_quotes(key)}');raise \"Props statement: #{key} is not defined\";end;"
24
- compiled_code << "#{key}=attributes[:'#{RBlade.escape_quotes(key)}'];attributes.delete :'#{RBlade.escape_quotes(key)}';"
25
- elsif isValidVariableName key
26
- compiled_code << "#{key}=attributes[:'#{RBlade.escape_quotes(key)}'].nil? ? #{value} : attributes[:'#{RBlade.escape_quotes(key)}'];"
27
- compiled_code << "attributes.delete :'#{RBlade.escape_quotes(key)}';"
22
+
23
+ # `_required` is deprecated. Use `required`. To be removed in 2.0.0
24
+ compiled_code << if value == "_required" || value == "required"
25
+ "if !attributes.has?(:'#{RBlade.escape_quotes(key)}');raise \"Props statement: #{key} is not defined\";end;"
28
26
  else
29
- compiled_code << "attributes.default(:'#{RBlade.escape_quotes(key)}', #{value});"
27
+ "attributes.default(:'#{RBlade.escape_quotes(key)}', #{value});"
28
+ end
29
+
30
+ if isValidVariableName key
31
+ compiled_code << if variableIsSlot key, tokens
32
+ "#{key}=RBlade::SlotManager.wrap(attributes.delete :'#{RBlade.escape_quotes(key)}');"
33
+ else
34
+ "#{key}=attributes.delete :'#{RBlade.escape_quotes(key)}';"
35
+ end
30
36
  end
31
37
 
32
38
  compiled_code
@@ -83,6 +89,11 @@ module RBlade
83
89
 
84
90
  true
85
91
  end
92
+
93
+ # Automatically detect if a variable is a slot by looking for "<var>.attributes"
94
+ def variableIsSlot name, tokens
95
+ tokens.any? { |token| token.value.to_s.match "#{name}.attributes" }
96
+ end
86
97
  end
87
98
  end
88
99
  end
@@ -24,6 +24,11 @@ module RBlade
24
24
  @content.respond_to?(method_name)
25
25
  end
26
26
 
27
+ # Wraps var in a slot manager if it's a string
28
+ def self.wrap var
29
+ var.is_a?(String) ? new(var) : var
30
+ end
31
+
27
32
  attr_reader :attributes
28
33
  end
29
34
  end
data/rblade.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "rblade"
3
- s.version = "1.0.0"
3
+ s.version = "1.0.2"
4
4
  s.summary = "A component-first templating engine for Rails"
5
5
  s.description = "RBlade is a simple, yet powerful templating engine for Ruby on Rails, inspired by Laravel Blade."
6
6
  s.authors = ["Simon J"]
@@ -8,6 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|storage)/}) }
9
9
  s.require_paths = ["lib"]
10
10
  s.homepage = "https://rubygems.org/gems/rblade"
11
+ s.metadata = { "source_code_uri" => "https://github.com/mwnciau/rblade" }
11
12
  s.license = "MIT"
12
13
  s.required_ruby_version = ">= 3.0.0"
13
14
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rblade
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon J
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-06 00:00:00.000000000 Z
11
+ date: 2024-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -122,7 +122,8 @@ files:
122
122
  homepage: https://rubygems.org/gems/rblade
123
123
  licenses:
124
124
  - MIT
125
- metadata: {}
125
+ metadata:
126
+ source_code_uri: https://github.com/mwnciau/rblade
126
127
  post_install_message:
127
128
  rdoc_options: []
128
129
  require_paths: