enumpath 0.1.1 → 0.2.0
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/README.md +196 -42
- data/lib/enumpath/logger.rb +14 -11
- data/lib/enumpath/operator/base.rb +25 -10
- data/lib/enumpath/operator/child.rb +2 -2
- data/lib/enumpath/operator/filter_expression.rb +32 -26
- data/lib/enumpath/operator/recursive_descent.rb +7 -7
- data/lib/enumpath/operator/slice.rb +2 -2
- data/lib/enumpath/operator/subscript_expression.rb +16 -14
- data/lib/enumpath/operator/union.rb +1 -1
- data/lib/enumpath/operator/wildcard.rb +1 -1
- data/lib/enumpath/operator.rb +47 -26
- data/lib/enumpath/path/normalized_path.rb +24 -15
- data/lib/enumpath/path.rb +19 -10
- data/lib/enumpath/resolver/object_inspector.rb +70 -0
- data/lib/enumpath/resolver/property.rb +227 -8
- data/lib/enumpath/resolver/simple.rb +35 -1
- data/lib/enumpath/version.rb +1 -1
- data/lib/enumpath.rb +21 -10
- metadata +67 -39
- data/.gitignore +0 -15
- data/.rspec +0 -3
- data/.travis.yml +0 -5
- data/.yardopts +0 -1
- data/CONTRIBUTING.md +0 -91
- data/Gemfile +0 -6
- data/Gemfile.lock +0 -56
- data/Rakefile +0 -11
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/enumpath.gemspec +0 -42
|
@@ -17,7 +17,7 @@ module Enumpath
|
|
|
17
17
|
# @param operator (see Enumpath::Operator::Base.detect?)
|
|
18
18
|
# @return (see Enumpath::Operator::Base.detect?)
|
|
19
19
|
def detect?(operator)
|
|
20
|
-
|
|
20
|
+
!(operator =~ OPERATOR_REGEX).nil?
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
|
|
@@ -31,26 +31,26 @@ module Enumpath
|
|
|
31
31
|
# the filter
|
|
32
32
|
def apply(remaining_path, enum, resolved_path, &block)
|
|
33
33
|
Enumpath.log('Evaluating filter expression') { { expression: operator, to: enum } }
|
|
34
|
-
|
|
35
34
|
_match, unpacked_operator = OPERATOR_REGEX.match(operator).to_a
|
|
36
35
|
expressions = unpacked_operator.split(LOGICAL_OPERATORS_REGEX).map(&:strip)
|
|
37
|
-
|
|
38
|
-
keys(enum).each do |key|
|
|
39
|
-
value = Enumpath::Resolver::Simple.resolve(key, enum)
|
|
40
|
-
Enumpath.log('Applying filter to key') { { key: key, enum: value } }
|
|
41
|
-
if pass?(expressions.dup, value)
|
|
42
|
-
Enumpath.log('Applying filtered key') { { 'filtered key': key, 'filtered enum': value } }
|
|
43
|
-
yield(remaining_path, value, resolved_path + [key.to_s])
|
|
44
|
-
end
|
|
45
|
-
end
|
|
36
|
+
keys(enum).each { |key| apply_to_key(key, expressions, remaining_path, enum, resolved_path, &block) }
|
|
46
37
|
end
|
|
47
38
|
|
|
48
39
|
private
|
|
49
40
|
|
|
41
|
+
def apply_to_key(key, expressions, remaining_path, enum, resolved_path)
|
|
42
|
+
value = Enumpath::Resolver::Simple.resolve(key, enum)
|
|
43
|
+
Enumpath.log('Applying filter to key') { { key: key, enum: value } }
|
|
44
|
+
return unless pass?(expressions.dup, value)
|
|
45
|
+
|
|
46
|
+
Enumpath.log('Applying filtered key') { { 'filtered key': key, 'filtered enum': value } }
|
|
47
|
+
yield(remaining_path, value, resolved_path + [key.to_s])
|
|
48
|
+
end
|
|
49
|
+
|
|
50
50
|
def pass?(expressions, enum)
|
|
51
51
|
running_result = evaluate(expressions.shift, enum)
|
|
52
52
|
Enumpath.log('Initial result') { { result: running_result } }
|
|
53
|
-
while expressions.any?
|
|
53
|
+
while expressions.any?
|
|
54
54
|
logical_operator, expression = expressions.shift(2)
|
|
55
55
|
running_result = evaluate(expression, enum, logical_operator, running_result)
|
|
56
56
|
Enumpath.log('Running result') { { result: running_result } }
|
|
@@ -66,34 +66,28 @@ module Enumpath
|
|
|
66
66
|
{ property => value, operator: operator, operand: operand, result: expression_result,
|
|
67
67
|
logical_operator: logical_operator }.compact
|
|
68
68
|
end
|
|
69
|
-
|
|
70
|
-
Enumpath.log('&&=')
|
|
71
|
-
running_result &&= expression_result
|
|
72
|
-
elsif logical_operator == '||'
|
|
73
|
-
Enumpath.log('||=')
|
|
74
|
-
running_result ||= expression_result
|
|
75
|
-
else
|
|
76
|
-
expression_result
|
|
77
|
-
end
|
|
69
|
+
running_result(logical_operator, running_result, expression_result)
|
|
78
70
|
end
|
|
79
71
|
|
|
80
72
|
def resolve(property, enum)
|
|
81
73
|
return enum if property == '@'
|
|
74
|
+
|
|
82
75
|
value = Enumpath::Resolver::Simple.resolve(property.gsub(/^@\./, ''), enum)
|
|
83
76
|
value = Enumpath::Resolver::Property.resolve(property.gsub(/^@\./, ''), enum) if value.nil?
|
|
84
77
|
value
|
|
85
78
|
end
|
|
86
79
|
|
|
87
80
|
def test(operator, operand, value)
|
|
88
|
-
return
|
|
81
|
+
return (value ? true : false) if operator.nil? || operand.nil?
|
|
82
|
+
|
|
89
83
|
typecast_operand = variable_typecaster(operand)
|
|
90
|
-
|
|
84
|
+
value.public_send(operator.to_sym, typecast_operand) ? true : false
|
|
91
85
|
rescue NoMethodError
|
|
92
86
|
Enumpath.log('Filter could not be evaluated!')
|
|
93
87
|
false
|
|
94
88
|
end
|
|
95
89
|
|
|
96
|
-
def variable_typecaster(variable)
|
|
90
|
+
def variable_typecaster(variable) # rubocop:disable Metrics/MethodLength
|
|
97
91
|
if variable =~ /\A('|").+\1\z/
|
|
98
92
|
# It quacks like a string
|
|
99
93
|
variable.gsub(/\A('|")|('|")\z/, '')
|
|
@@ -102,8 +96,8 @@ module Enumpath
|
|
|
102
96
|
variable.gsub(/\A:/, '').to_sym
|
|
103
97
|
elsif variable =~ /true|false|nil/i
|
|
104
98
|
# It quacks like an unquoted boolean operator
|
|
105
|
-
variable == 'true'
|
|
106
|
-
elsif regexp = variable.to_regexp(literal: false, detect: false)
|
|
99
|
+
variable == 'true'
|
|
100
|
+
elsif (regexp = variable.to_regexp(literal: false, detect: false))
|
|
107
101
|
# It quacks like a regex
|
|
108
102
|
regexp
|
|
109
103
|
else
|
|
@@ -111,6 +105,18 @@ module Enumpath
|
|
|
111
105
|
variable.to_f
|
|
112
106
|
end
|
|
113
107
|
end
|
|
108
|
+
|
|
109
|
+
def running_result(logical_operator, running_result, expression_result)
|
|
110
|
+
if logical_operator == '&&'
|
|
111
|
+
Enumpath.log('&&=')
|
|
112
|
+
running_result && expression_result
|
|
113
|
+
elsif logical_operator == '||'
|
|
114
|
+
Enumpath.log('||=')
|
|
115
|
+
running_result || expression_result
|
|
116
|
+
else
|
|
117
|
+
expression_result
|
|
118
|
+
end
|
|
119
|
+
end
|
|
114
120
|
end
|
|
115
121
|
end
|
|
116
122
|
end
|
|
@@ -13,7 +13,7 @@ module Enumpath
|
|
|
13
13
|
# @param operator (see Enumpath::Operator::Base.detect?)
|
|
14
14
|
# @return (see Enumpath::Operator::Base.detect?)
|
|
15
15
|
def detect?(operator)
|
|
16
|
-
|
|
16
|
+
operator == OPERATOR
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
|
|
@@ -28,17 +28,17 @@ module Enumpath
|
|
|
28
28
|
# enumerable member
|
|
29
29
|
# @yieldparam resolved_path [Array] resolved_path for the enumerable itself, or resolved_path plus the key for
|
|
30
30
|
# each direct enumerable member
|
|
31
|
-
def apply(remaining_path, enum, resolved_path
|
|
31
|
+
def apply(remaining_path, enum, resolved_path)
|
|
32
32
|
Enumpath.log('Applying remaining path recursively to enum') { { 'remaining path': remaining_path } }
|
|
33
33
|
yield(remaining_path, enum, resolved_path)
|
|
34
34
|
keys(enum).each do |key|
|
|
35
35
|
value = Enumpath::Resolver::Simple.resolve(key, enum)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
yield(['..'] + remaining_path, value, resolved_path + [key])
|
|
36
|
+
next unless recursable?(value)
|
|
37
|
+
|
|
38
|
+
Enumpath.log('Applying remaining path recursively to key') do
|
|
39
|
+
{ key: key, 'remaining path': ['..'] + remaining_path }
|
|
41
40
|
end
|
|
41
|
+
yield(['..'] + remaining_path, value, resolved_path + [key])
|
|
42
42
|
end
|
|
43
43
|
end
|
|
44
44
|
|
|
@@ -12,7 +12,7 @@ module Enumpath
|
|
|
12
12
|
# @param operator (see Enumpath::Operator::Base.detect?)
|
|
13
13
|
# @return (see Enumpath::Operator::Base.detect?)
|
|
14
14
|
def detect?(operator)
|
|
15
|
-
|
|
15
|
+
!(operator =~ OPERATOR_REGEX).nil?
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
|
|
@@ -25,7 +25,7 @@ module Enumpath
|
|
|
25
25
|
# @yieldparam remaining_path [Array] the included index plus remaining_path
|
|
26
26
|
# @yieldparam enum [Enumerable] enum
|
|
27
27
|
# @yieldparam resolved_path [Array] resolved_path
|
|
28
|
-
def apply(remaining_path, enum, resolved_path
|
|
28
|
+
def apply(remaining_path, enum, resolved_path)
|
|
29
29
|
_match, start, length, step = OPERATOR_REGEX.match(operator).to_a
|
|
30
30
|
max_length = enum.size
|
|
31
31
|
slices(start, length, step, max_length).each do |index|
|
|
@@ -5,7 +5,7 @@ module Enumpath
|
|
|
5
5
|
# Implements JSONPath subscript expressions operator syntax. See
|
|
6
6
|
# {file:README.md#label-Subscript+expressions+operator} for syntax and examples
|
|
7
7
|
class SubscriptExpression < Base
|
|
8
|
-
ARITHMETIC_OPERATOR_REGEX =
|
|
8
|
+
ARITHMETIC_OPERATOR_REGEX = %r{(\+|-|\*\*|\*|\/|%)}
|
|
9
9
|
OPERATOR_REGEX = /^\((.*)\)$/
|
|
10
10
|
|
|
11
11
|
class << self
|
|
@@ -14,7 +14,7 @@ module Enumpath
|
|
|
14
14
|
# @param operator (see Enumpath::Operator::Base.detect?)
|
|
15
15
|
# @return (see Enumpath::Operator::Base.detect?)
|
|
16
16
|
def detect?(operator)
|
|
17
|
-
|
|
17
|
+
!(operator =~ OPERATOR_REGEX).nil?
|
|
18
18
|
end
|
|
19
19
|
end
|
|
20
20
|
|
|
@@ -25,17 +25,15 @@ module Enumpath
|
|
|
25
25
|
# @yieldparam remaining_path [Array] remaining_path
|
|
26
26
|
# @yieldparam enum [Enumerable] the member of the enumerable at the value of the subscript expression
|
|
27
27
|
# @yieldparam resolved_path [Array] resolved_path plus the value of the subscript expression
|
|
28
|
-
def apply(remaining_path, enum, resolved_path
|
|
28
|
+
def apply(remaining_path, enum, resolved_path)
|
|
29
29
|
Enumpath.log('Applying subscript expression') { { expression: operator, to: enum } }
|
|
30
30
|
|
|
31
31
|
_match, unpacked_operator = OPERATOR_REGEX.match(operator).to_a
|
|
32
32
|
result = evaluate(unpacked_operator, enum)
|
|
33
33
|
|
|
34
34
|
value = Enumpath::Resolver::Simple.resolve(result, enum)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
yield(remaining_path, value, resolved_path + [result.to_s])
|
|
38
|
-
end
|
|
35
|
+
Enumpath.log('Applying subscript') { { 'enum at subscript': value } } unless value.nil?
|
|
36
|
+
yield(remaining_path, value, resolved_path + [result.to_s]) unless value.nil?
|
|
39
37
|
end
|
|
40
38
|
|
|
41
39
|
private
|
|
@@ -47,6 +45,7 @@ module Enumpath
|
|
|
47
45
|
|
|
48
46
|
def resolve(property, enum)
|
|
49
47
|
return enum if property == '@'
|
|
48
|
+
|
|
50
49
|
value = Enumpath::Resolver::Simple.resolve(property.gsub(/^@\./, ''), enum)
|
|
51
50
|
value = Enumpath::Resolver::Property.resolve(property.gsub(/^@\./, ''), enum) if value.nil?
|
|
52
51
|
value
|
|
@@ -57,19 +56,22 @@ module Enumpath
|
|
|
57
56
|
Enumpath.log('Simple subscript') { { subscript: value } }
|
|
58
57
|
value
|
|
59
58
|
else
|
|
60
|
-
|
|
61
|
-
typecast_operand = variable_typecaster(operand)
|
|
62
|
-
result = value.public_send(operator.to_sym, typecast_operand)
|
|
63
|
-
Enumpath.log('Evaluated subscript') do
|
|
64
|
-
{ value: value, operator: operator, operand: typecast_operand, result: result }
|
|
65
|
-
end
|
|
66
|
-
result
|
|
59
|
+
evaluate_with_operator(operator, operand, value)
|
|
67
60
|
end
|
|
68
61
|
rescue NoMethodError
|
|
69
62
|
Enumpath.log('Subscript could not be evaluated') { { subscript: nil } }
|
|
70
63
|
nil
|
|
71
64
|
end
|
|
72
65
|
|
|
66
|
+
def evaluate_with_operator(operator, operand, value)
|
|
67
|
+
typecast_operand = variable_typecaster(operand)
|
|
68
|
+
result = value.public_send(operator.to_sym, typecast_operand)
|
|
69
|
+
Enumpath.log('Evaluated subscript') do
|
|
70
|
+
{ value: value, operator: operator, operand: typecast_operand, result: result }
|
|
71
|
+
end
|
|
72
|
+
result
|
|
73
|
+
end
|
|
74
|
+
|
|
73
75
|
def variable_typecaster(variable)
|
|
74
76
|
if variable =~ /\A('|").+\1\z/ || variable =~ /^:.+/
|
|
75
77
|
# It quacks like a string or symbol
|
|
@@ -32,7 +32,7 @@ module Enumpath
|
|
|
32
32
|
# @yieldparam remaining_path [Array] the union member plus remaining_path
|
|
33
33
|
# @yieldparam enum [Enumerable] enum
|
|
34
34
|
# @yieldparam resolved_path [Array] resolved_path
|
|
35
|
-
def apply(remaining_path, enum, resolved_path
|
|
35
|
+
def apply(remaining_path, enum, resolved_path)
|
|
36
36
|
parts = operator.split(SPLIT_REGEX).map { |part| part.strip.gsub(/^['"]|['"]$/, '') }
|
|
37
37
|
Enumpath.log('Applying union parts') { { parts: parts } }
|
|
38
38
|
parts.each { |part| yield([part] + remaining_path, enum, resolved_path) }
|
|
@@ -23,7 +23,7 @@ module Enumpath
|
|
|
23
23
|
# @yieldparam remaining_path [Array] the key of the given member plus remaining_path
|
|
24
24
|
# @yieldparam enum [Enumerable] enum
|
|
25
25
|
# @yieldparam resolved_path [Array] resolved_path
|
|
26
|
-
def apply(remaining_path, enum, resolved_path
|
|
26
|
+
def apply(remaining_path, enum, resolved_path)
|
|
27
27
|
keys = keys(enum)
|
|
28
28
|
Enumpath.log('Applying wildcard to keys') { { keys: keys } }
|
|
29
29
|
keys.each { |key| yield([key.to_s] + remaining_path, enum, resolved_path) }
|
data/lib/enumpath/operator.rb
CHANGED
|
@@ -21,32 +21,53 @@ module Enumpath
|
|
|
21
21
|
# @param enum [Enumerable] the enumerable to assist in detecting child operators
|
|
22
22
|
# @return an instance of a subclass of Enumpath::Operator based on what was detected, or nil if nothing was
|
|
23
23
|
# detected
|
|
24
|
-
def detect(operator, enum)
|
|
25
|
-
if
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
24
|
+
def detect(operator, enum) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
25
|
+
return operator(:Child, operator) if child?(operator, enum)
|
|
26
|
+
return operator(:Wildcard, operator) if wildcard?(operator)
|
|
27
|
+
return operator(:RecursiveDescent, operator) if recursive_descent?(operator)
|
|
28
|
+
return operator(:Union, operator) if union?(operator)
|
|
29
|
+
return operator(:SubscriptExpression, operator) if subscript_expression?(operator)
|
|
30
|
+
return operator(:FilterExpression, operator) if filter_expression?(operator)
|
|
31
|
+
return operator(:Slice, operator) if slice?(operator)
|
|
32
|
+
|
|
33
|
+
Enumpath.log('Not a valid operator for enum')
|
|
34
|
+
nil
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def child?(operator, enum)
|
|
40
|
+
Enumpath::Operator::Child.detect?(operator, enum)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def wildcard?(operator)
|
|
44
|
+
Enumpath::Operator::Wildcard.detect?(operator)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def recursive_descent?(operator)
|
|
48
|
+
Enumpath::Operator::RecursiveDescent.detect?(operator)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def union?(operator)
|
|
52
|
+
Enumpath::Operator::Union.detect?(operator)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def subscript_expression?(operator)
|
|
56
|
+
Enumpath::Operator::SubscriptExpression.detect?(operator)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def filter_expression?(operator)
|
|
60
|
+
Enumpath::Operator::FilterExpression.detect?(operator)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def slice?(operator)
|
|
64
|
+
Enumpath::Operator::Slice.detect?(operator)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def operator(operator_class, operator)
|
|
68
|
+
Enumpath.log("#{operator_class} operator detected")
|
|
69
|
+
klass = Object.const_get("Enumpath::Operator::#{operator_class}")
|
|
70
|
+
klass.new(operator)
|
|
50
71
|
end
|
|
51
72
|
end
|
|
52
73
|
end
|
|
@@ -16,34 +16,43 @@ module Enumpath
|
|
|
16
16
|
|
|
17
17
|
def normalize(path)
|
|
18
18
|
return path if path.is_a?(Array)
|
|
19
|
+
|
|
19
20
|
normalized_path = path.dup
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
normalized_path = restore_filter_expressions(normalized_path, filter_expressions)
|
|
25
|
-
normalized_path.gsub!(/\A\$(;|\z)/, '') # Remove root operator
|
|
21
|
+
filter_expressions = remove_filter_expressions!(normalized_path)
|
|
22
|
+
replace_tokens!(normalized_path)
|
|
23
|
+
restore_filter_expressions!(normalized_path, filter_expressions)
|
|
24
|
+
remove_root!(normalized_path)
|
|
26
25
|
normalized_path = normalized_path.split(';') # Split into segment parts
|
|
27
|
-
normalized_path.reject!
|
|
28
|
-
segment.nil? || segment.size == 0
|
|
29
|
-
end
|
|
26
|
+
normalized_path.reject! { |segment| segment.nil? || segment.size.zero? } # Remove blanks
|
|
30
27
|
Enumpath.log('Path normalized') { { original: path, normalized: normalized_path } }
|
|
31
28
|
normalized_path
|
|
32
29
|
end
|
|
33
30
|
|
|
34
31
|
# Move filter expressions (`[?(expr)]`) to the temporary array and replace with an index notation
|
|
35
|
-
def remove_filter_expressions(path)
|
|
32
|
+
def remove_filter_expressions!(path)
|
|
36
33
|
filter_expressions = []
|
|
37
|
-
|
|
38
|
-
filter_expressions <<
|
|
34
|
+
path.gsub!(FILTER_EXPRESSION_REGEX) do
|
|
35
|
+
filter_expressions << Regexp.last_match(1)
|
|
39
36
|
"[##{filter_expressions.size - 1}]"
|
|
40
37
|
end
|
|
41
|
-
|
|
38
|
+
filter_expressions
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def replace_tokens!(path)
|
|
42
|
+
path.gsub!(/'?\.'?|\['?/, ';') # Replace "'?.'?" or "['?" with ";"
|
|
43
|
+
path.gsub!(/;;;|;;/, ';..;') # Replace ";;;" or ";;" with ";..;"
|
|
44
|
+
path.gsub!(/;\z|'?\]|'\z/, '') # Replace ";$" or "'?]" or "'$" with ""
|
|
42
45
|
end
|
|
43
46
|
|
|
44
47
|
# Replace index notations with their corresponding filter expressions (`?(expr)`) from the temporary array
|
|
45
|
-
def restore_filter_expressions(
|
|
46
|
-
|
|
48
|
+
def restore_filter_expressions!(path, filter_expressions)
|
|
49
|
+
path.gsub!(INDEX_NOTATION_REGEX) do
|
|
50
|
+
filter_expressions[Regexp.last_match(1).to_i]
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def remove_root!(path)
|
|
55
|
+
path.gsub!(/\A\$(;|\z)/, '') # Remove root operator
|
|
47
56
|
end
|
|
48
57
|
end
|
|
49
58
|
end
|
data/lib/enumpath/path.rb
CHANGED
|
@@ -11,7 +11,8 @@ module Enumpath
|
|
|
11
11
|
# @return [Enumpath::Results] the current results array
|
|
12
12
|
attr_reader :results
|
|
13
13
|
|
|
14
|
-
# @param path [String, Array<String>] the path expression to apply to the enumerable
|
|
14
|
+
# @param path [String, Array<String>] the path expression to apply to the enumerable, or an array of path segments
|
|
15
|
+
# @raise [ArgumentError] if the path is neither a String nor an Array
|
|
15
16
|
# @param result_type (see Enumpath::Results#initialize)
|
|
16
17
|
def initialize(path, result_type: nil)
|
|
17
18
|
@path = path
|
|
@@ -46,13 +47,8 @@ module Enumpath
|
|
|
46
47
|
def trace(path_segments, enum, resolved_path = [], nesting_level = 0)
|
|
47
48
|
Enumpath.logger.level = nesting_level
|
|
48
49
|
if path_segments.any?
|
|
49
|
-
Enumpath.log(
|
|
50
|
-
|
|
51
|
-
remaining_path = path_segments[1..-1]
|
|
52
|
-
operator = Enumpath::Operator.detect(segment, enum)
|
|
53
|
-
operator&.apply(remaining_path, enum, resolved_path) do |s, e, c|
|
|
54
|
-
trace(s, e, c, nesting_level + 1)
|
|
55
|
-
end
|
|
50
|
+
Enumpath.log('Applying') { { operator: path_segments, to: enum } }
|
|
51
|
+
apply_segments(path_segments, enum, resolved_path, nesting_level)
|
|
56
52
|
else
|
|
57
53
|
Enumpath.log('Storing') { { resolved_path: resolved_path, enum: enum } }
|
|
58
54
|
results.store(resolved_path, enum)
|
|
@@ -60,14 +56,27 @@ module Enumpath
|
|
|
60
56
|
Enumpath.logger.level = nesting_level
|
|
61
57
|
end
|
|
62
58
|
|
|
63
|
-
|
|
59
|
+
def apply_segments(path_segments, enum, resolved_path, nesting_level)
|
|
60
|
+
segment = path_segments.first
|
|
61
|
+
remaining_path = path_segments[1..]
|
|
62
|
+
operator = Enumpath::Operator.detect(segment, enum)
|
|
63
|
+
operator&.apply(remaining_path, enum, resolved_path) do |s, e, c|
|
|
64
|
+
trace(s, e, c, nesting_level + 1)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
64
67
|
|
|
65
68
|
def cache
|
|
66
69
|
@cache ||= Enumpath.path_cache
|
|
67
70
|
end
|
|
68
71
|
|
|
72
|
+
# String paths are normalized once and cached. An array is already a list of path segments, so it only has its
|
|
73
|
+
# segments converted to strings and never touches the cache.
|
|
69
74
|
def normalize!
|
|
70
|
-
@path =
|
|
75
|
+
@path = case @path
|
|
76
|
+
when String then cache.get_or_set(@path) { Enumpath::Path::NormalizedPath.new(@path) }
|
|
77
|
+
when Array then Enumpath::Path::NormalizedPath.new(@path.map(&:to_s))
|
|
78
|
+
else raise ArgumentError, 'path must be a String or an Array of path segments'
|
|
79
|
+
end
|
|
71
80
|
end
|
|
72
81
|
end
|
|
73
82
|
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rbconfig'
|
|
4
|
+
|
|
5
|
+
module Enumpath
|
|
6
|
+
module Resolver
|
|
7
|
+
# Helpers for inspecting an object without trusting it. Kernel methods are called through
|
|
8
|
+
# `Kernel.instance_method(...).bind`, so an object that overrides `is_a?`, `respond_to?`, `method` or the instance
|
|
9
|
+
# variable methods cannot change the answer.
|
|
10
|
+
#
|
|
11
|
+
# @private
|
|
12
|
+
module ObjectInspector
|
|
13
|
+
# The Kernel methods {.kernel} can call
|
|
14
|
+
KERNEL = %i[is_a? respond_to? method instance_variable_defined? instance_variable_get].map do |name|
|
|
15
|
+
[name, Kernel.instance_method(name)]
|
|
16
|
+
end.to_h.freeze
|
|
17
|
+
|
|
18
|
+
# Ruby's standard library directory (`RbConfig::CONFIG['rubylibdir']`). Installed gems, including default and
|
|
19
|
+
# bundled gems such as Logger or CSV, live elsewhere and are not covered by it.
|
|
20
|
+
RUBY_LIBRARY_DIR = File.join(RbConfig::CONFIG['rubylibdir'], '')
|
|
21
|
+
|
|
22
|
+
module_function
|
|
23
|
+
|
|
24
|
+
# Calls one of the {KERNEL} methods on an object
|
|
25
|
+
#
|
|
26
|
+
# @raise [TypeError] if the object does not include Kernel (a BasicObject)
|
|
27
|
+
def kernel(object, method_name, *args)
|
|
28
|
+
KERNEL[method_name].bind(object).call(*args)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The public method of that name, if the object has one that expects no arguments
|
|
32
|
+
#
|
|
33
|
+
# @return [Method, nil]
|
|
34
|
+
def public_method_without_required_args(object, name)
|
|
35
|
+
return unless kernel(object, :respond_to?, name)
|
|
36
|
+
|
|
37
|
+
method = kernel(object, :method, name)
|
|
38
|
+
method unless method.parameters.any? { |type, _| %i[req keyreq].include?(type) }
|
|
39
|
+
rescue NameError, TypeError
|
|
40
|
+
nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Whether `:readers` may call a method: it is written in Ruby (it has a source location, so it is not
|
|
44
|
+
# implemented in C), it is not one of Ruby's built-in methods (`<internal:...>`), and it is not in the Ruby
|
|
45
|
+
# standard library directory ({RUBY_LIBRARY_DIR}). Methods from installed gems pass this check, including
|
|
46
|
+
# default and bundled gems such as Logger and CSV, and so do attr_readers and methods made with
|
|
47
|
+
# `define_method` or `class_eval`.
|
|
48
|
+
#
|
|
49
|
+
# @param method [Method, nil]
|
|
50
|
+
# @return [true, false]
|
|
51
|
+
def application_method?(method)
|
|
52
|
+
location = method&.source_location
|
|
53
|
+
return false if location.nil? || location.empty?
|
|
54
|
+
|
|
55
|
+
file = location.first.to_s
|
|
56
|
+
!file.start_with?('<internal:') && !file.start_with?(RUBY_LIBRARY_DIR)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Whether the object is an instance of any of the classes. Objects that do not include Kernel count as a match,
|
|
60
|
+
# so callers treat them as off limits.
|
|
61
|
+
#
|
|
62
|
+
# @return [true, false]
|
|
63
|
+
def instance_of_any?(object, classes)
|
|
64
|
+
classes.any? { |klass| kernel(object, :is_a?, klass) }
|
|
65
|
+
rescue TypeError
|
|
66
|
+
true
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|