rblade 1.0.4 → 1.1.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: 9124479cbf9e4fd7355470943bafcbd009c367abf8593d19bc3f8a967eb0e4cf
4
- data.tar.gz: c0d9f34089ae943df7263298cb9e08951b23b2ebfcbc3a221c4e8a2940ee959c
3
+ metadata.gz: edaa1af6299a8281520a73611def0838ef1017f2ec7bb3fd8a125bcd02e5c7ce
4
+ data.tar.gz: 6363e44a009a29e8771c50ecf4768b0d365b0d2147ba998db72805e9b05c0390
5
5
  SHA512:
6
- metadata.gz: 78e173dcbdb4e085fd46840597f828e08df016874d8cfecccd756155b254be37924d20c81682373c45146518754e3a079d4b35aa5443b57ce67de9ac10c162ce
7
- data.tar.gz: e27154636512a2fc28c6f279c13bfcad0ca8639f1d404b73a91803435de93012d43aed89bcdd2aa02a76e59d3c485e7214a93dc710f8d20adabc285c5aae8208
6
+ metadata.gz: 996e77805b56530e9f136c087753938c10c8dc71d45b1fbaec10e81e890878b9a0da00a10fa024a650cb2e463e155fe8da723fb16a6eb690f5811915ce4fe879
7
+ data.tar.gz: 5bbe562c01ec5fb12e77cb53aa2991cebfd83792dd831a41957497e3b04e29df442511c890d48fcb05bef78107cf5347019c49c7c8dc9e5d3f9ef0557a63f940
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  storage
2
2
  Gemfile.lock
3
+ *.gem
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 1.1.0 [2024-08-23]
2
+ - Add `@eachWithIndex` directive
3
+ - Fix attribute manager output of attributes with no value (#10)
4
+
5
+ ## 1.0.5 [2024-08-14]
6
+ - Allow use of variables in @method directive
7
+
1
8
  ## 1.0.4 [2024-08-14]
2
9
  - Fix printing ruby member variables (#8)
3
10
 
data/README.md CHANGED
@@ -222,6 +222,16 @@ In addition to conditional statements, RBlade provides simple directives for wor
222
222
  <p>This is user {{ user.id }}</p>
223
223
  @endEach
224
224
 
225
+ {{-- Compiles to users.each_with_index do |user, index| ... --}}
226
+ @eachWithIndex (user, index in users)
227
+ <p>This is user {{ index }} {{ user.name }}</p>
228
+ @endEachWithIndex
229
+
230
+ {{-- eachWithIndex has a special case for Hashes: in this example, the result is "Item 1: a A" --}}
231
+ @eachWithIndex (key, value, index in {a: 'A'})
232
+ Item #{{ index + 1 }}: {{ key }} {{ value }}
233
+ @endEachWithIndex
234
+
225
235
  @forElse (name in [])
226
236
  <li>{{ name }}</li>
227
237
  @empty
@@ -234,6 +244,12 @@ In addition to conditional statements, RBlade provides simple directives for wor
234
244
  <p>No users</p>
235
245
  @endEachElse
236
246
 
247
+ @eachWithIndexElse (user, index in users)
248
+ <li>{{ index }}: {{ user.name }}</li>
249
+ @empty
250
+ <p>No users</p>
251
+ @endEachWithIndexElse
252
+
237
253
  @while (true)
238
254
  <p>I'm looping forever.</p>
239
255
  @endWhile
data/REFERENCE.md CHANGED
@@ -36,6 +36,7 @@ By default, RBlade will look for components in the `app/views/components` folder
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 |
38
38
  | `<div {{ header.attributes }}>` | Output the attributes passed into a named block |
39
+ | `@shouldRender(RUBY_EXPRESSION)` | Only renders the component if RUBY_EXPRESSION evaluates to true |
39
40
 
40
41
  <a name="quick-reference-attributes"></a>
41
42
  ## Attributes
@@ -75,19 +76,22 @@ The attributes variable is an instance of a class that manages attributes. As we
75
76
  <a name="quick-reference-loops"></a>
76
77
  ## Loops
77
78
 
78
- | Syntax | Description |
79
- |:------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------|
80
- | `@while( looping ) ... @endWhile` | Compiles to a Ruby while statement |
81
- | `@until( finished ) ... @endUntil` | Compiles to a Ruby until statement |
82
- | `@for( i in 1..10 ) ... @endFor` | Compiles to a Ruby for loop |
83
- | `@each( i in 1..10 ) ... @endEach` | Calls `each` on the given collection, with `\|i\|` as the block argument |
84
- | `@each( key, value in {a: 1} ) ... @endEach` | Calls `each` on the given Hash, with `\|key, value\|` as the block arguments |
85
- | `@forElse( i in 1..10 ) ... @empty ... @endForElse` | Compiles to a for loop as above, but the block after `@empty` is printed if the given collection is empty |
86
- | `@eachElse( i in 1..10 ) ... @empty ... @endEachElse` | Compiles to a each loop as above, but the block after `@empty` is printed if the given collection is empty |
87
- | `@break` | Break out of the current loop |
88
- | `@next` | Go to the next iteration in the current loop |
89
- | `@break( RUBY_EXPRESSION )` | Break out of the current loop if the expression evaluate to true |
90
- | `@next( RUBY_EXPRESSION )` | Go to the next iteration in the current loop if the expression evaluate to true |
79
+ | Syntax | Description |
80
+ |:-----------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------|
81
+ | `@while( looping ) ... @endWhile` | Compiles to a Ruby while statement |
82
+ | `@until( finished ) ... @endUntil` | Compiles to a Ruby until statement |
83
+ | `@for( i in 1..10 ) ... @endFor` | Compiles to a Ruby for loop |
84
+ | `@each( i in 1..10 ) ... @endEach` | Calls `each` on the given collection, with `\|i\|` as the block argument |
85
+ | `@each( key, value in {a: 1} ) ... @endEach` | Calls `each` on the given Hash, with `\|key, value\|` as the block arguments |
86
+ | `@eachWithIndex( value, index in ['a', 'b'] ) ... @endEachWithIndex` | Calls `each_with_index` on the given Array, with `\|value, index\|` as the block arguments |
87
+ | `@eachWithIndex( key, value, index in {a: 1} ) ... @endEachWithIndex` | Calls `each_with_index` on the given Hash, where `key` is the Hash key, `value` is the Hash value, and `index` is the index |
88
+ | `@forElse( i in 1..10 ) ... @empty ... @endForElse` | Compiles to a for loop as above, but the block after `@empty` is printed if the given collection is empty |
89
+ | `@eachElse( i in 1..10 ) ... @empty ... @endEachElse` | Compiles to a each loop as above, but the block after `@empty` is printed if the given collection is empty |
90
+ | `@eachWithIndexElse( value, index in 1..10 ) ... @empty ... @endEachWithIndexElse` | Compiles to a each_with_index loop as above, but the block after `@empty` is printed if the given collection is empty |
91
+ | `@break` | Break out of the current loop |
92
+ | `@next` | Go to the next iteration in the current loop |
93
+ | `@break( RUBY_EXPRESSION )` | Break out of the current loop if the expression evaluate to true |
94
+ | `@next( RUBY_EXPRESSION )` | Go to the next iteration in the current loop if the expression evaluate to true |
91
95
 
92
96
  <a name="quick-reference-forms"></a>
93
97
  ## Forms
@@ -81,6 +81,8 @@ module RBlade
81
81
  "elsif" => [CompilesConditionals, :compileElsif],
82
82
  "each" => [CompilesLoops, :compileEach],
83
83
  "eachelse" => [CompilesLoops, :compileEachElse],
84
+ "eachwithindex" => [CompilesLoops, :compileEachWithIndex],
85
+ "eachwithindexelse" => [CompilesLoops, :compileEachWithIndexElse],
84
86
  "empty" => [CompilesLoops, :compileEmpty],
85
87
  "empty?" => [CompilesConditionals, :compileEmpty],
86
88
  "end" => [CompilesStatements, :compileEnd],
@@ -5,9 +5,8 @@ module RBlade
5
5
  if args&.count != 1
6
6
  raise StandardError.new "Method statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
7
7
  end
8
- method = RBlade.h(args[0].tr("\"'", ""))
9
8
 
10
- %(_out<<'<input type="hidden" name="_method" value="#{method}">';)
9
+ %(_out<<'<input type="hidden" name="_method" value="'<<#{args[0]}<<'">';)
11
10
  end
12
11
 
13
12
  def compileDelete args
@@ -15,7 +14,7 @@ module RBlade
15
14
  raise StandardError.new "Delete statement: wrong number of arguments (given #{args.count}, expecting 0)"
16
15
  end
17
16
 
18
- compileMethod(["DELETE"])
17
+ compileMethod(["'DELETE'"])
19
18
  end
20
19
 
21
20
  def compilePatch args
@@ -23,7 +22,7 @@ module RBlade
23
22
  raise StandardError.new "Patch statement: wrong number of arguments (given #{args.count}, expecting 0)"
24
23
  end
25
24
 
26
- compileMethod(["PATCH"])
25
+ compileMethod(["'PATCH'"])
27
26
  end
28
27
 
29
28
  def compilePut args
@@ -31,7 +30,7 @@ module RBlade
31
30
  raise StandardError.new "Put statement: wrong number of arguments (given #{args.count}, expecting 0)"
32
31
  end
33
32
 
34
- compileMethod(["PUT"])
33
+ compileMethod(["'PUT'"])
35
34
  end
36
35
 
37
36
  def compileOld args
@@ -19,27 +19,74 @@ module RBlade
19
19
 
20
20
  def compileEach args
21
21
  if args.nil? || args.count > 2
22
- raise StandardError.new "Each statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
22
+ raise StandardError.new "Each statement: wrong number of arguments (given #{args&.count || 0}, expecting 1 or 2)"
23
23
  end
24
- # Allow variables to be a key, value pair
25
- args = args.join ","
24
+ last_arg, collection = args.pop.split(" in ")
25
+ args.push(last_arg)
26
26
 
27
- variables, collection = args.split(" in ")
27
+ if collection.nil?
28
+ raise StandardError.new "Each statement: collection not found (expecting 'in')"
29
+ end
28
30
 
29
- "#{collection}.each do |#{variables}|;"
31
+ "#{collection}.each do |#{args.join(",")}|;"
32
+ end
33
+
34
+ def compileEachWithIndex args
35
+ if args.nil? || args.count > 3
36
+ raise StandardError.new "Each with index statement: wrong number of arguments (given #{args&.count || 0}, expecting 1 to 3)"
37
+ end
38
+ last_arg, collection = args.pop.split(" in ")
39
+ args.push(last_arg)
40
+
41
+ if collection.nil?
42
+ raise StandardError.new "Each with index statement: collection not found (expecting 'in')"
43
+ end
44
+
45
+ # Special case for 3 arguments: the first 2 arguments are the key/value pair in a Hash, and
46
+ # the third is the index
47
+ if args.count == 3
48
+ "#{collection}.each_with_index do |_ivar,#{args[2]}|;#{args[0]},#{args[1]}=_ivar;"
49
+ else
50
+ "#{collection}.each_with_index do |#{args.join(",")}|;"
51
+ end
30
52
  end
31
53
 
32
54
  def compileEachElse args
33
55
  if args.nil? || args.count > 2
34
56
  raise StandardError.new "Each else statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
35
57
  end
36
- # Allow variables to be a key, value pair
37
- args = args.join ","
58
+ last_arg, collection = args.pop.split(" in ")
59
+ args.push(last_arg)
60
+
61
+ if collection.nil?
62
+ raise StandardError.new "Each else statement: collection not found (expecting 'in')"
63
+ end
64
+
38
65
  @loop_else_counter += 1
39
66
 
40
- variables, collection = args.split(" in ")
67
+ "_looped_#{@loop_else_counter}=false;#{collection}.each do |#{args.join(",")}|;_looped_#{@loop_else_counter}=true;"
68
+ end
69
+
70
+ def compileEachWithIndexElse args
71
+ if args.nil? || args.count > 3
72
+ raise StandardError.new "Each with index statement: wrong number of arguments (given #{args&.count || 0}, expecting 1 to 3)"
73
+ end
74
+ last_arg, collection = args.pop.split(" in ")
75
+ args.push(last_arg)
76
+
77
+ if collection.nil?
78
+ raise StandardError.new "Each with index statement: collection not found (expecting 'in')"
79
+ end
80
+
81
+ @loop_else_counter += 1
41
82
 
42
- "_looped_#{@loop_else_counter}=false;#{collection}.each do |#{variables}|;_looped_#{@loop_else_counter}=true;"
83
+ # Special case for 3 arguments: the first 2 arguments are the key/value pair in a Hash, and
84
+ # the third is the index
85
+ if args.count == 3
86
+ "_looped_#{@loop_else_counter}=false;#{collection}.each_with_index do |_ivar,#{args[2]}|;#{args[0]},#{args[1]}=_ivar;_looped_#{@loop_else_counter}=true;"
87
+ else
88
+ "_looped_#{@loop_else_counter}=false;#{collection}.each_with_index do |#{args.join(",")}|;_looped_#{@loop_else_counter}=true;"
89
+ end
43
90
  end
44
91
 
45
92
  def compileFor args
@@ -37,7 +37,7 @@ module RBlade
37
37
  attributes ||= @attributes
38
38
 
39
39
  attributes.map do |key, value|
40
- "#{key}=\"#{(value == true) ? key : h(value)}\""
40
+ (value == true) ? key : "#{key}=\"#{h(value)}\""
41
41
  end.join " "
42
42
  end
43
43
 
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.4"
3
+ s.version = "1.1.0"
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"]
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.4
4
+ version: 1.1.0
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-14 00:00:00.000000000 Z
11
+ date: 2024-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0'
142
142
  requirements: []
143
- rubygems_version: 3.0.3.1
143
+ rubygems_version: 3.3.5
144
144
  signing_key:
145
145
  specification_version: 4
146
146
  summary: A component-first templating engine for Rails