nay 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +55 -0
- data/lib/nay/evaluator.rb +6 -6
- data/lib/nay/template.rb +43 -0
- data/lib/nay/version.rb +1 -1
- data/lib/nay.rb +3 -7
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b57f5f0b2fa5d4029a0929787441104e328b7d3a2a90a10ad31d916dc240980c
|
4
|
+
data.tar.gz: 3304ea0e737b31138c7962e0279ba0ffc648c5ef5a4f5ceb24a73c8f7a3d2314
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07a0c0cfde1458fe322091287277128757514a581c0fabc9cc782efe38d6a23572bd9e1d43f88e96c8c7fbba6a6fd052ac7cda7c840a77e13f0f5f1598d23684
|
7
|
+
data.tar.gz: 8924a857891e42d710de59cafbd54247f46e018f02195e9d762cec56893d53ed3015ed31658f5eeeba32eba3f48e0e22fee2bd4120596fe2ab1026479ac18457
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
#### 0.0.3 - February 18th, 2022
|
2
|
+
|
3
|
+
* Works with not just string expressions but with also hash and arrays.
|
4
|
+
#### 0.0.2 - February 18th, 2022
|
5
|
+
|
6
|
+
* Add underscore and hyphen to list of acceptable tokens without qualifying.
|
7
|
+
|
1
8
|
#### 0.0.1 - February 17th, 2022
|
2
9
|
|
3
10
|
* Initial version.
|
data/README.md
CHANGED
@@ -22,6 +22,8 @@ bundle add nay
|
|
22
22
|
|
23
23
|
## Examples
|
24
24
|
|
25
|
+
### Simple String-based Expression
|
26
|
+
|
25
27
|
````ruby
|
26
28
|
animal = {
|
27
29
|
'animal' => {
|
@@ -41,6 +43,59 @@ Notes:
|
|
41
43
|
* Tokens are passed through #[] method for the passed in object. Any object providing a brackets interface will work.
|
42
44
|
* A valid token only contains the following characters: [a-zA-Z0-9_-]. If a token needs to contain something not in that list then qualify it with double quotes (i.e. `hello, << person."first name" >>`)
|
43
45
|
|
46
|
+
### More Complex Expressions
|
47
|
+
|
48
|
+
Expressions are not limited to just being a string. Here are the basic heuristics:
|
49
|
+
|
50
|
+
* hash: then each key and value are recursively traversed
|
51
|
+
* array: then each entry is recursively traversed
|
52
|
+
* string: evaluated with parameters
|
53
|
+
* anything not a hash, array, or string: do nothing
|
54
|
+
|
55
|
+
An example of this would be a somewhat complicated object:
|
56
|
+
|
57
|
+
````ruby
|
58
|
+
object = {
|
59
|
+
'<< id_key >>' => '<< id >>',
|
60
|
+
'pets' => [
|
61
|
+
'<< pets."pet 1" >>',
|
62
|
+
'<< pets."pet 2" >>'
|
63
|
+
],
|
64
|
+
zyx: :vut
|
65
|
+
}
|
66
|
+
|
67
|
+
parameters = {
|
68
|
+
'id_key' => 'id',
|
69
|
+
'id' => 123,
|
70
|
+
'pets' => {
|
71
|
+
'pet 1' => 'dog',
|
72
|
+
'pet 2' => 'cat'
|
73
|
+
},
|
74
|
+
zyx: :vut
|
75
|
+
}
|
76
|
+
|
77
|
+
evaluated_object = Nay.evaluate(object, parameters)
|
78
|
+
````
|
79
|
+
|
80
|
+
In the above example evaluated_object would be equivalent to:
|
81
|
+
|
82
|
+
````ruby
|
83
|
+
{
|
84
|
+
'id' => '123',
|
85
|
+
'pets' => [
|
86
|
+
'dog',
|
87
|
+
'cat'
|
88
|
+
],
|
89
|
+
zyx: :vut
|
90
|
+
}
|
91
|
+
````
|
92
|
+
|
93
|
+
Notes:
|
94
|
+
|
95
|
+
* A hash, hash key, and hash value are represented here
|
96
|
+
* An array with entries are represented here
|
97
|
+
* A non-string is represented here (as the zyx/vut key/value)
|
98
|
+
|
44
99
|
## Contributing
|
45
100
|
|
46
101
|
### Development Environment Configuration
|
data/lib/nay/evaluator.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Nay
|
4
|
-
# Knows how to take a stream of lexically analyzed tokens along with
|
4
|
+
# Knows how to take a stream of lexically analyzed tokens along with a parameters hash and
|
5
5
|
# evaluate interpolated key paths.
|
6
6
|
class Evaluator
|
7
7
|
attr_reader :lexer
|
@@ -10,16 +10,16 @@ module Nay
|
|
10
10
|
@lexer = lexer
|
11
11
|
end
|
12
12
|
|
13
|
-
def evaluate(
|
13
|
+
def evaluate(parameters = {})
|
14
14
|
io = StringIO.new
|
15
|
-
pointer =
|
15
|
+
pointer = parameters
|
16
16
|
|
17
17
|
while (token = lexer.next_token)
|
18
18
|
case token.type
|
19
19
|
when Lexer::CONTENT
|
20
20
|
io << token.literal
|
21
21
|
when Lexer::OPEN_EXPRESSION
|
22
|
-
pointer =
|
22
|
+
pointer = parameters
|
23
23
|
when Lexer::IDENTIFIER
|
24
24
|
pointer = traverse(pointer, token.literal)
|
25
25
|
when Lexer::CLOSE_EXPRESSION
|
@@ -32,8 +32,8 @@ module Nay
|
|
32
32
|
|
33
33
|
private
|
34
34
|
|
35
|
-
def traverse(
|
36
|
-
|
35
|
+
def traverse(parameters, value)
|
36
|
+
parameters[value] if parameters.respond_to?(:[])
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
data/lib/nay/template.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'evaluator'
|
4
|
+
require_relative 'lexer'
|
5
|
+
|
6
|
+
module Nay
|
7
|
+
# Built on top of the string-based Lexer/Evaluator but instead of only working for a string,
|
8
|
+
# this will recursively evaluate all strings within an object. Heuristics:
|
9
|
+
# - Hashes will have their keys and parameters traversed
|
10
|
+
# - Arrays will have their entries traversed
|
11
|
+
# - Strings evaluate using Lexer
|
12
|
+
# - All other types will simply return themselves (not be traversed or use the Lexer)
|
13
|
+
class Template
|
14
|
+
attr_reader :object
|
15
|
+
|
16
|
+
def initialize(object)
|
17
|
+
@object = object
|
18
|
+
|
19
|
+
freeze
|
20
|
+
end
|
21
|
+
|
22
|
+
def evaluate(parameters = {})
|
23
|
+
recursive_evaluate(object, parameters)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def recursive_evaluate(expression, parameters)
|
29
|
+
case expression
|
30
|
+
when Array
|
31
|
+
expression.map { |o| recursive_evaluate(o, parameters) }
|
32
|
+
when Hash
|
33
|
+
expression.to_h do |k, v|
|
34
|
+
[recursive_evaluate(k, parameters), recursive_evaluate(v, parameters)]
|
35
|
+
end
|
36
|
+
when String
|
37
|
+
Evaluator.new(Lexer.new(expression)).evaluate(parameters)
|
38
|
+
else
|
39
|
+
expression
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/nay/version.rb
CHANGED
data/lib/nay.rb
CHANGED
@@ -3,17 +3,13 @@
|
|
3
3
|
require 'stringio'
|
4
4
|
require 'strscan'
|
5
5
|
|
6
|
-
require_relative 'nay/
|
7
|
-
require_relative 'nay/lexer'
|
6
|
+
require_relative 'nay/template'
|
8
7
|
|
9
8
|
# Top-level API
|
10
9
|
module Nay
|
11
10
|
class << self
|
12
|
-
def evaluate(
|
13
|
-
|
14
|
-
evaluator = Evaluator.new(lexer)
|
15
|
-
|
16
|
-
evaluator.evaluate(input)
|
11
|
+
def evaluate(object, parameters = {})
|
12
|
+
Template.new(object).evaluate(parameters)
|
17
13
|
end
|
18
14
|
end
|
19
15
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Ruggio
|
@@ -188,6 +188,7 @@ files:
|
|
188
188
|
- lib/nay/evaluator.rb
|
189
189
|
- lib/nay/lexer.rb
|
190
190
|
- lib/nay/state.rb
|
191
|
+
- lib/nay/template.rb
|
191
192
|
- lib/nay/token.rb
|
192
193
|
- lib/nay/version.rb
|
193
194
|
- nay.gemspec
|