static_literal_parser 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/static_literal_parser.rb +19 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fd90d1bbf587025109b4bbd0a1458cc031f8b6cef5137fc90b7f0052e1b0f7e
|
4
|
+
data.tar.gz: 161e4c1bc731ff18b1f748abc717e59c47b6906c5456637336754a304671f298
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59fecb14ab67a2550b537fd4e8bf6063027c4b3ac5e30dbdfa10f2c1ac4f8b2db0efdd4df6e45a30ffd6e5ba301e7bb23ba2daf7968e9e4e1351bb7c3699f54a
|
7
|
+
data.tar.gz: a74fbfb68fa4201ccd5adc6ed5fbb7c06884b9ec0e1088a4437c257ad5e9b17dec09a6a5bbadd753aa46934cdd40a5207acac920e089af91b273fe22a66e6fbb
|
@@ -2,7 +2,14 @@
|
|
2
2
|
|
3
3
|
module StaticLiteralParser
|
4
4
|
def self.parse(str, constants)
|
5
|
-
|
5
|
+
case constants
|
6
|
+
when Array
|
7
|
+
parse(str, constant_strs_array_to_hash(constants))
|
8
|
+
when Hash
|
9
|
+
parse_(RubyVM::AbstractSyntaxTree.parse(str), constants)
|
10
|
+
else
|
11
|
+
raise ArgumentError, "Unsupported constants type: #{constants}"
|
12
|
+
end
|
6
13
|
end
|
7
14
|
|
8
15
|
private_class_method def self.parse_(node, constants)
|
@@ -32,4 +39,15 @@ module StaticLiteralParser
|
|
32
39
|
raise "Unexpected node #{node.type}"
|
33
40
|
end
|
34
41
|
end
|
42
|
+
|
43
|
+
private_class_method def self.constant_strs_array_to_hash(constant_strs_array)
|
44
|
+
constant_strs_array.flat_map {|constant|
|
45
|
+
constant.split('::').inject([]) {|memo, part|
|
46
|
+
[*memo, [memo.last, part].compact.join('::')]
|
47
|
+
}
|
48
|
+
}.to_h {|name|
|
49
|
+
c = Object.const_get(name)
|
50
|
+
[name.to_sym, c]
|
51
|
+
}
|
52
|
+
end
|
35
53
|
end
|