antlers 1.5.9 → 1.5.10
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 +4 -4
- data/lib/antlers/version.rb +1 -1
- data/lib/modules/variables.rb +43 -16
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cfe3b5f8944a9969d2ae36247f5cbb0e51639856424e21be84b4ed03b68b6e4c
|
|
4
|
+
data.tar.gz: 172ab39bec6372ef042ac732d7bceca763195153357c16b4a5ba8b07733698a4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 635cab19c80b8e8ce0013c3448aa9b0d1a4ac4218d46c5bd02a30007bc72685a74473c4bf8312f44917bf83cdb41204bef200d86cdacec279f9165e52c1119bd
|
|
7
|
+
data.tar.gz: '0209aabb017ec18e8127b15a823af3cca7e1531dab02a8ccb6132fc634e15693d116d1164e2a385b39b4900f51b1977deca647ef26572c814fef236cf66d768f'
|
data/lib/antlers/version.rb
CHANGED
data/lib/modules/variables.rb
CHANGED
|
@@ -7,6 +7,39 @@ require_relative '../support/queries'
|
|
|
7
7
|
|
|
8
8
|
module Antlers
|
|
9
9
|
module Variables
|
|
10
|
+
# What an expression is depends only on its text, so it's read once rather than on every render: a value, or a
|
|
11
|
+
# variable (an instance variable, or a local variable or method) with the path to read from it.
|
|
12
|
+
Expression = Data.define(:value, :name, :path, :instance_variable) do
|
|
13
|
+
def variable? = !name.nil?
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
def for(text)
|
|
17
|
+
self::CACHE.fetch(text) do
|
|
18
|
+
self::CACHE.clear if self::CACHE.size >= self::CACHE_LIMIT
|
|
19
|
+
self::CACHE[text.dup.freeze] = parse(text)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def parse(text)
|
|
26
|
+
literal = true
|
|
27
|
+
value = Lowkey::ValueParser.parse(text) { literal = false }
|
|
28
|
+
return value(value) if literal
|
|
29
|
+
return value(text[1..-2]) if Queries.user_defined_string?(text)
|
|
30
|
+
|
|
31
|
+
name, *path = text.split('.')
|
|
32
|
+
new(value: nil, name: name.freeze, path: path.each(&:freeze).freeze, instance_variable: name.start_with?('@'))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Shared by every render, so a string is frozen.
|
|
36
|
+
def value(value) = new(value: value.is_a?(String) ? value.freeze : value, name: nil, path: [].freeze, instance_variable: false)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
# A template has a fixed set of expressions, but props can be built, so the cache is bounded.
|
|
40
|
+
Expression::CACHE = {} # rubocop:disable Style/MutableConstant
|
|
41
|
+
Expression::CACHE_LIMIT = 10_000
|
|
42
|
+
|
|
10
43
|
# Evaluation limited to the following for security and to stop a templating language becoming a programming language.
|
|
11
44
|
# 1. A value ('string', 3, 1.5, true, nil or :symbol)
|
|
12
45
|
# 2. Invalid string ("Hi #{name}", 'it's', 'a' + 'b')
|
|
@@ -22,27 +55,21 @@ module Antlers
|
|
|
22
55
|
end
|
|
23
56
|
|
|
24
57
|
def result(value:, current_binding:)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
58
|
+
expression = Expression.for(value)
|
|
59
|
+
return expression.value unless expression.variable?
|
|
60
|
+
return value unless current_binding
|
|
28
61
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
private
|
|
34
|
-
|
|
35
|
-
def variable(value:, current_binding:)
|
|
36
|
-
name, *path = value.split('.')
|
|
37
|
-
|
|
38
|
-
result = method_var(name:, current_binding:)
|
|
39
|
-
return Queries.read_path(value: result, path:) if path.count > 0
|
|
62
|
+
result = method_var(expression:, current_binding:)
|
|
63
|
+
return Queries.read_path(value: result, path: expression.path) unless expression.path.empty?
|
|
40
64
|
|
|
41
65
|
result
|
|
42
66
|
end
|
|
43
67
|
|
|
44
|
-
|
|
45
|
-
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def method_var(expression:, current_binding:)
|
|
71
|
+
name = expression.name
|
|
72
|
+
return current_binding.receiver.instance_variable_get(name) if expression.instance_variable
|
|
46
73
|
return current_binding.local_variable_get(name) if current_binding.local_variable_defined?(name)
|
|
47
74
|
return current_binding.receiver.send(name.to_sym) if current_binding.receiver.respond_to?(name.to_sym)
|
|
48
75
|
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: antlers
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.5.
|
|
4
|
+
version: 1.5.10
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- maedi
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: lowkey
|
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
96
96
|
- !ruby/object:Gem::Version
|
|
97
97
|
version: '0'
|
|
98
98
|
requirements: []
|
|
99
|
-
rubygems_version:
|
|
99
|
+
rubygems_version: 4.0.20
|
|
100
100
|
specification_version: 4
|
|
101
101
|
summary: Templating language inside HTML inside Ruby
|
|
102
102
|
test_files: []
|