enumpath 0.3.0 → 0.4.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 +7 -3
- data/lib/enumpath/logger.rb +26 -12
- data/lib/enumpath/operator/filter_expression.rb +19 -9
- data/lib/enumpath/path.rb +3 -7
- data/lib/enumpath/path_cache.rb +88 -0
- data/lib/enumpath/regexp_parser.rb +38 -0
- data/lib/enumpath/version.rb +1 -1
- data/lib/enumpath.rb +59 -17
- metadata +8 -28
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bb4cccab1574db3fb57f5d6a2474f25e9764756b67a7bbcfc93207c4977bfd84
|
|
4
|
+
data.tar.gz: 41a2f9fe5476f4aaf83080de4e63eb12736a4095a0bf04514119fe95d126c436
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 621df8576d98f0ab80bad2fa33d09abb14c37e798c0785493248441b07ce49f223256f79828bb9495c0f2fb50902f9c94446a3892e959da19153b88818d625e7
|
|
7
|
+
data.tar.gz: c9c88108adde2dc418da96d81137f2c792b603464ce93aef4631f2b25594f432a0a99409e8ad6a1cbebf82de84229458b448b79b366f7cbdbae458ddb48fb26f
|
data/README.md
CHANGED
|
@@ -230,7 +230,7 @@ The following are all valid filter expressions:
|
|
|
230
230
|
- `[?(@.price > 2 && @.price < 10)]`: members with a `price` greater than 2 _and_ less than 10
|
|
231
231
|
- `[?(@.name =~ /bob/i || @.name == 'Mark')]`: any member whose `name` matches the regex `/bob/i` or equals `'Mark'`
|
|
232
232
|
|
|
233
|
-
> Regular expression operands
|
|
233
|
+
> Regular expression operands (`/pattern/flags` or `%r{pattern}flags`, with the flags `i`, `m`, `x`, `n` and `u`) are parsed as text and never evaluated as Ruby code. A regular expression that is not valid matches nothing.
|
|
234
234
|
|
|
235
235
|
### Subscript expression operator
|
|
236
236
|
|
|
@@ -551,7 +551,7 @@ Enumpath: Child operator detected
|
|
|
551
551
|
result: [{:category=>"reference", :author=>"Nigel Rees", :...
|
|
552
552
|
```
|
|
553
553
|
|
|
554
|
-
You can also
|
|
554
|
+
You can also turn verbose mode on for every call with `Enumpath.verbose = true`. The `verbose:` option of a call overrides that for the call, and only in its own thread; `Enumpath.verbose = false` turns the default off again. Set the default once, at start up.
|
|
555
555
|
|
|
556
556
|
## Path normalization
|
|
557
557
|
|
|
@@ -559,7 +559,11 @@ When you give a string path to Enumpath it will automatically normalize it to an
|
|
|
559
559
|
|
|
560
560
|
### Normalized path caching
|
|
561
561
|
|
|
562
|
-
To save a little bit of time on consecutive calls Enumpath caches the normalized version of each path. This is an implementation detail that can generally be ignored
|
|
562
|
+
To save a little bit of time on consecutive calls Enumpath caches the normalized version of each path. This is an implementation detail that can generally be ignored. The cache is safe to share between threads and keeps at most 1,000 paths, dropping the least recently used one when it is full, so applying many distinct dynamic paths does not grow memory without limit. If you run into trouble with it you can clear the cache with `Enumpath.path_cache.reset`.
|
|
563
|
+
|
|
564
|
+
## Thread safety
|
|
565
|
+
|
|
566
|
+
`Enumpath.apply` can be called from several threads (or fibers) at the same time. Each call keeps its own options (`:verbose`, `:property_resolver`, `:regexp_timeout`) and results, and the logger's indentation belongs to the thread that is logging. The two things threads share are the normalized path cache, which is guarded by a mutex (see [Normalized path caching](#normalized-path-caching)), and the logger: each log message is written in one piece, so messages from different threads do not interleave their lines. Give `Enumpath.logger.logger` a logger that is itself safe to share (Ruby's `Logger` is) and set `Enumpath.verbose` and `Enumpath.logger.logger` once, before you start threads.
|
|
563
567
|
|
|
564
568
|
## Deviations from the Original JSONPath Spec
|
|
565
569
|
|
data/lib/enumpath/logger.rb
CHANGED
|
@@ -10,8 +10,8 @@ module Enumpath
|
|
|
10
10
|
|
|
11
11
|
SEPARATOR = '--------------------------------------'
|
|
12
12
|
|
|
13
|
-
#
|
|
14
|
-
|
|
13
|
+
# The `Thread.current` key that holds the indentation level of the current thread
|
|
14
|
+
LEVEL_KEY = :enumpath_log_level
|
|
15
15
|
|
|
16
16
|
# @return [::Logger, #<<] a {::Logger}-compatible logger instance
|
|
17
17
|
attr_accessor :logger
|
|
@@ -19,10 +19,22 @@ module Enumpath
|
|
|
19
19
|
# @param logdev [String, IO] The log device. See Ruby's {::Logger.new} documentation.
|
|
20
20
|
def initialize(logdev = STDOUT)
|
|
21
21
|
@logger = ::Logger.new(logdev)
|
|
22
|
-
@level = 0
|
|
23
22
|
@padding = {}
|
|
24
23
|
end
|
|
25
24
|
|
|
25
|
+
# The indentation level to apply to log messages. It belongs to the current thread, so the calls that threads
|
|
26
|
+
# make at the same time do not change each other's indentation.
|
|
27
|
+
#
|
|
28
|
+
# @return [Integer]
|
|
29
|
+
def level
|
|
30
|
+
Thread.current[LEVEL_KEY] || 0
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# @param value [Integer] the indentation level for the current thread
|
|
34
|
+
def level=(value)
|
|
35
|
+
Thread.current[LEVEL_KEY] = value
|
|
36
|
+
end
|
|
37
|
+
|
|
26
38
|
# Generates a log message for debugging. Returns fast if {Enumpath.verbose} is false. Accepts an optional block
|
|
27
39
|
# which must contain a single hash, the contents of which will be added to the log message, and which are lazily
|
|
28
40
|
# evaluated only if {Enumpath.verbose} is true.
|
|
@@ -32,10 +44,14 @@ module Enumpath
|
|
|
32
44
|
def log(title)
|
|
33
45
|
return unless Enumpath.verbose
|
|
34
46
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
47
|
+
pad = padding
|
|
48
|
+
message = +"#{pad}#{SEPARATOR}\n#{pad}Enumpath: #{title}\n"
|
|
49
|
+
if block_given?
|
|
50
|
+
message << "#{pad}#{SEPARATOR}\n"
|
|
51
|
+
message << vars_message(yield, pad)
|
|
52
|
+
end
|
|
53
|
+
# One write per message, so messages from threads that log at the same time do not interleave their lines
|
|
54
|
+
append_log(message)
|
|
39
55
|
end
|
|
40
56
|
|
|
41
57
|
private
|
|
@@ -44,13 +60,11 @@ module Enumpath
|
|
|
44
60
|
logger << message
|
|
45
61
|
end
|
|
46
62
|
|
|
47
|
-
def
|
|
48
|
-
return unless vars.is_a?(Hash)
|
|
63
|
+
def vars_message(vars, pad)
|
|
64
|
+
return '' unless vars.is_a?(Hash)
|
|
49
65
|
|
|
50
66
|
label_size = vars.keys.map(&:size).max
|
|
51
|
-
vars.
|
|
52
|
-
append_log "#{padding}#{label.to_s.ljust(label_size)}: #{massaged_value(value)}\n"
|
|
53
|
-
end
|
|
67
|
+
vars.map { |label, value| "#{pad}#{label.to_s.ljust(label_size)}: #{massaged_value(value)}\n" }.join
|
|
54
68
|
end
|
|
55
69
|
|
|
56
70
|
def massaged_value(value) # rubocop:disable Metrics/MethodLength
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'to_regexp'
|
|
4
|
-
|
|
5
3
|
module Enumpath
|
|
6
4
|
module Operator
|
|
7
5
|
# Implements JSONPath filter expression operator syntax. See {file:README.md#label-Filter+expression+operator} for
|
|
@@ -86,14 +84,26 @@ module Enumpath
|
|
|
86
84
|
def test(operator, operand, value)
|
|
87
85
|
return (value ? true : false) if operator.nil? || operand.nil?
|
|
88
86
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
87
|
+
matching_nothing_on_error do
|
|
88
|
+
value.public_send(operator.to_sym, variable_typecaster(operand)) ? true : false
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# A comparison that cannot be evaluated, or a regular expression that is not valid or ran out of time, does not
|
|
93
|
+
# match
|
|
94
|
+
def matching_nothing_on_error
|
|
95
|
+
yield
|
|
94
96
|
rescue Enumpath::RegexpTimeout::ERROR
|
|
95
97
|
Enumpath::RegexpTimeout.expire!
|
|
96
|
-
|
|
98
|
+
no_match('Regular expression ran out of time!')
|
|
99
|
+
rescue RegexpError
|
|
100
|
+
no_match('Regular expression is not valid!')
|
|
101
|
+
rescue NoMethodError
|
|
102
|
+
no_match('Filter could not be evaluated!')
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def no_match(reason)
|
|
106
|
+
Enumpath.log(reason)
|
|
97
107
|
false
|
|
98
108
|
end
|
|
99
109
|
|
|
@@ -107,7 +117,7 @@ module Enumpath
|
|
|
107
117
|
elsif variable =~ /true|false|nil/i
|
|
108
118
|
# It quacks like an unquoted boolean operator
|
|
109
119
|
variable == 'true'
|
|
110
|
-
elsif (regexp =
|
|
120
|
+
elsif (regexp = Enumpath::RegexpParser.parse(variable))
|
|
111
121
|
# It quacks like a regex
|
|
112
122
|
Enumpath::RegexpTimeout.limit(regexp)
|
|
113
123
|
else
|
data/lib/enumpath/path.rb
CHANGED
|
@@ -45,7 +45,7 @@ module Enumpath
|
|
|
45
45
|
# @param nesting_level [Integer] used to set the indentation level for {Enumpath::Logger}
|
|
46
46
|
# @return [void]
|
|
47
47
|
def trace(path_segments, enum, resolved_path = [], nesting_level = 0)
|
|
48
|
-
Enumpath.logger.level = nesting_level
|
|
48
|
+
Enumpath.logger.level = nesting_level if Enumpath.verbose
|
|
49
49
|
if path_segments.any?
|
|
50
50
|
Enumpath.log('Applying') { { operator: path_segments, to: enum } }
|
|
51
51
|
apply_segments(path_segments, enum, resolved_path, nesting_level)
|
|
@@ -53,7 +53,7 @@ module Enumpath
|
|
|
53
53
|
Enumpath.log('Storing') { { resolved_path: resolved_path, enum: enum } }
|
|
54
54
|
results.store(resolved_path, enum)
|
|
55
55
|
end
|
|
56
|
-
Enumpath.logger.level = nesting_level
|
|
56
|
+
Enumpath.logger.level = nesting_level if Enumpath.verbose
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def apply_segments(path_segments, enum, resolved_path, nesting_level)
|
|
@@ -65,15 +65,11 @@ module Enumpath
|
|
|
65
65
|
end
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
-
def cache
|
|
69
|
-
@cache ||= Enumpath.path_cache
|
|
70
|
-
end
|
|
71
|
-
|
|
72
68
|
# String paths are normalized once and cached. An array is already a list of path segments, so it only has its
|
|
73
69
|
# segments converted to strings and never touches the cache.
|
|
74
70
|
def normalize!
|
|
75
71
|
@path = case @path
|
|
76
|
-
when String then
|
|
72
|
+
when String then Enumpath.path_cache.get_or_set(@path) { Enumpath::Path::NormalizedPath.new(@path) }
|
|
77
73
|
when Array then Enumpath::Path::NormalizedPath.new(@path.map(&:to_s))
|
|
78
74
|
else raise ArgumentError, 'path must be a String or an Array of path segments'
|
|
79
75
|
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Enumpath
|
|
4
|
+
# A small cache of normalized path expressions. It is guarded by a mutex, so threads can share it, and it holds at
|
|
5
|
+
# most `max_size` entries: when it is full the least recently used entry is dropped, so a long-running process that
|
|
6
|
+
# applies many distinct dynamic paths does not grow without limit.
|
|
7
|
+
class PathCache
|
|
8
|
+
# The number of entries kept when no size is given
|
|
9
|
+
DEFAULT_MAX_SIZE = 1_000
|
|
10
|
+
|
|
11
|
+
# @return [Integer] the most entries the cache keeps
|
|
12
|
+
attr_reader :max_size
|
|
13
|
+
|
|
14
|
+
# @param max_size [Integer] the most entries to keep
|
|
15
|
+
# @raise [ArgumentError] if max_size is not a positive integer
|
|
16
|
+
def initialize(max_size = DEFAULT_MAX_SIZE)
|
|
17
|
+
raise ArgumentError, "max_size must be a positive Integer, got #{max_size.inspect}" unless valid_size?(max_size)
|
|
18
|
+
|
|
19
|
+
@max_size = max_size
|
|
20
|
+
@data = {}
|
|
21
|
+
@mutex = Mutex.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @param key [#to_s] the key to look up
|
|
25
|
+
# @return the cached value, or nil if there is none. A hit counts as a use.
|
|
26
|
+
def get(key)
|
|
27
|
+
key = key.to_s
|
|
28
|
+
@mutex.synchronize { touch(key) ? @data[key] : nil }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @param key [#to_s] the key to store the value under
|
|
32
|
+
# @param value the value to store
|
|
33
|
+
# @return the value
|
|
34
|
+
def set(key, value)
|
|
35
|
+
key = key.to_s
|
|
36
|
+
@mutex.synchronize { store(key, value) }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Returns the cached value for the key. When there is none, calls the block with the key, stores its result and
|
|
40
|
+
# returns that. Two threads that miss at the same time may both call the block; the last result is kept.
|
|
41
|
+
#
|
|
42
|
+
# @param key [#to_s] the key to look up
|
|
43
|
+
# @yieldparam key [String] the key as a string
|
|
44
|
+
# @return the cached or computed value
|
|
45
|
+
def get_or_set(key)
|
|
46
|
+
key = key.to_s
|
|
47
|
+
hit, value = @mutex.synchronize { touch(key) ? [true, @data[key]] : [false, nil] }
|
|
48
|
+
return value if hit
|
|
49
|
+
|
|
50
|
+
@mutex.synchronize { store(key, yield(key)) }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Removes every entry
|
|
54
|
+
#
|
|
55
|
+
# @return [void]
|
|
56
|
+
def reset
|
|
57
|
+
@mutex.synchronize { @data.clear }
|
|
58
|
+
nil
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# @return [Integer] the number of entries
|
|
62
|
+
def size
|
|
63
|
+
@mutex.synchronize { @data.size }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
def valid_size?(max_size)
|
|
69
|
+
max_size.is_a?(Integer) && max_size.positive?
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Moves an entry to the most recently used end. Call with the mutex held.
|
|
73
|
+
def touch(key)
|
|
74
|
+
return false unless @data.key?(key)
|
|
75
|
+
|
|
76
|
+
@data[key] = @data.delete(key)
|
|
77
|
+
true
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Call with the mutex held.
|
|
81
|
+
def store(key, value)
|
|
82
|
+
@data.delete(key)
|
|
83
|
+
@data[key] = value
|
|
84
|
+
@data.shift while @data.size > @max_size
|
|
85
|
+
value
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Enumpath
|
|
4
|
+
# Parses the regular expression literals that a filter expression accepts (`/pattern/flags` and `%r{pattern}flags`)
|
|
5
|
+
# into a Regexp. The pattern is passed to `Regexp.new` as text and never evaluated as Ruby code, so `#{...}` in it
|
|
6
|
+
# stays plain text.
|
|
7
|
+
module RegexpParser
|
|
8
|
+
LITERAL = %r{\A(?:/(.*)/|%r\{(.*)\})([a-z]*)\z}m.freeze
|
|
9
|
+
FLAGS = { 'i' => Regexp::IGNORECASE, 'm' => Regexp::MULTILINE, 'x' => Regexp::EXTENDED, 'n' => Regexp::NOENCODING,
|
|
10
|
+
'u' => 0 }.freeze
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
# @param string [String] an operand from a path
|
|
14
|
+
# @raise [RegexpError] if the string is a regular expression literal whose pattern is not valid
|
|
15
|
+
# @return [Regexp, nil] the regular expression, or nil if the string is not a regular expression literal (this
|
|
16
|
+
# includes flags other than `i`, `m`, `x`, `n` and `u`)
|
|
17
|
+
def parse(string)
|
|
18
|
+
match = LITERAL.match(string)
|
|
19
|
+
return unless match
|
|
20
|
+
|
|
21
|
+
options = options_for(match[3])
|
|
22
|
+
return if options.nil?
|
|
23
|
+
|
|
24
|
+
# An escaped delimiter and a plain delimiter mean the same inside a pattern
|
|
25
|
+
pattern = (match[1] || match[2]).gsub('\\/', '/')
|
|
26
|
+
Regexp.new(pattern, options)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def options_for(flags)
|
|
32
|
+
return if flags.each_char.any? { |flag| !FLAGS.key?(flag) }
|
|
33
|
+
|
|
34
|
+
flags.each_char.reduce(0) { |options, flag| options | FLAGS.fetch(flag) }
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/enumpath/version.rb
CHANGED
data/lib/enumpath.rb
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'mini_cache'
|
|
4
3
|
require 'enumpath/logger'
|
|
5
4
|
require 'enumpath/operator'
|
|
6
5
|
require 'enumpath/path'
|
|
6
|
+
require 'enumpath/path_cache'
|
|
7
|
+
require 'enumpath/regexp_parser'
|
|
7
8
|
require 'enumpath/regexp_timeout'
|
|
8
9
|
require 'enumpath/results'
|
|
9
10
|
require 'enumpath/resolver/object_inspector'
|
|
@@ -13,15 +14,38 @@ require 'enumpath/version'
|
|
|
13
14
|
|
|
14
15
|
# A JSONPath-compatible library for navigating Ruby objects using path expressions
|
|
15
16
|
module Enumpath
|
|
17
|
+
# The `Thread.current` key that holds the `:verbose` option of the current {Enumpath.apply} call
|
|
18
|
+
VERBOSE_KEY = :enumpath_verbose
|
|
19
|
+
|
|
20
|
+
# The process-wide default for verbose mode
|
|
16
21
|
@verbose = false
|
|
22
|
+
# How many {Enumpath.apply} calls are running with a `:verbose` option. While it is zero, {Enumpath.verbose} is just
|
|
23
|
+
# the default, without a look at the thread's own setting, which keeps the common case fast.
|
|
24
|
+
@verbose_overrides = 0
|
|
25
|
+
@verbose_lock = Mutex.new
|
|
26
|
+
# The logger and the path cache are created when Enumpath loads, so threads never race to create them
|
|
27
|
+
@logger = Enumpath::Logger.new
|
|
28
|
+
@path_cache = Enumpath::PathCache.new
|
|
17
29
|
|
|
18
30
|
class << self
|
|
31
|
+
# Sets whether verbose mode is on by default, for every call that does not pass a `:verbose` option. Set this once
|
|
32
|
+
# at start up; to change it for one call, or one thread, pass the `:verbose` option to {Enumpath.apply}.
|
|
33
|
+
#
|
|
34
|
+
# @param value [true, false] the default
|
|
35
|
+
attr_writer :verbose
|
|
36
|
+
|
|
19
37
|
# Whether verbose mode is enabled. When enabled, the {Enumpath::Logger} will print
|
|
20
|
-
# information to the logging stream to assist in debugging path expressions.
|
|
21
|
-
#
|
|
38
|
+
# information to the logging stream to assist in debugging path expressions. During an {Enumpath.apply} call this
|
|
39
|
+
# is the call's `:verbose` option, if it has one, and only in that call's thread; otherwise it is the default set
|
|
40
|
+
# with {Enumpath.verbose=}. Defaults to false
|
|
22
41
|
#
|
|
23
42
|
# @return [true,false]
|
|
24
|
-
|
|
43
|
+
def verbose
|
|
44
|
+
return @verbose if @verbose_overrides.zero?
|
|
45
|
+
|
|
46
|
+
override = Thread.current[VERBOSE_KEY]
|
|
47
|
+
override.nil? ? @verbose : override
|
|
48
|
+
end
|
|
25
49
|
|
|
26
50
|
# Resolve a path expression against an enumerable
|
|
27
51
|
#
|
|
@@ -29,7 +53,8 @@ module Enumpath
|
|
|
29
53
|
# @param enum (see Enumpath::Path#apply)
|
|
30
54
|
# @param options [optional, Hash]
|
|
31
55
|
# @option options [Symbol] :result_type (:value) The type of results to return, `:value` or `:path`
|
|
32
|
-
# @option options [true, false] :verbose (
|
|
56
|
+
# @option options [true, false] :verbose (Enumpath.verbose) Whether to enable additional output for debugging, for
|
|
57
|
+
# this call only
|
|
33
58
|
# @option options [Symbol, #call] :property_resolver (:safe) Which methods a path may call on an object when a
|
|
34
59
|
# segment is not an index, key, or member: `:safe` (read-only methods only), `:readers` (`:safe` plus public
|
|
35
60
|
# no-argument methods written in Ruby, including those of installed gems, and ActiveModel-style `attributes`;
|
|
@@ -45,11 +70,12 @@ module Enumpath
|
|
|
45
70
|
# a positive number
|
|
46
71
|
# @return (see Enumpath::Path#apply)
|
|
47
72
|
def apply(path, enum, options = {})
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
73
|
+
with_verbose(options[:verbose]) do
|
|
74
|
+
logger.level = 0 if verbose
|
|
75
|
+
Enumpath::Resolver::Property.with_policy(options[:property_resolver]) do
|
|
76
|
+
Enumpath::RegexpTimeout.with_limit(options.fetch(:regexp_timeout, Enumpath::RegexpTimeout::DEFAULT)) do
|
|
77
|
+
Enumpath::Path.new(path, result_type: options[:result_type]).apply(enum)
|
|
78
|
+
end
|
|
53
79
|
end
|
|
54
80
|
end
|
|
55
81
|
end
|
|
@@ -58,24 +84,40 @@ module Enumpath
|
|
|
58
84
|
#
|
|
59
85
|
# @private
|
|
60
86
|
# @return [Enumpath::Logger]
|
|
61
|
-
|
|
62
|
-
@logger ||= Enumpath::Logger.new
|
|
63
|
-
end
|
|
87
|
+
attr_reader :logger
|
|
64
88
|
|
|
65
89
|
# A shortcut to {Enumpath::logger.log}
|
|
66
90
|
#
|
|
67
91
|
# @private
|
|
68
92
|
# @see Enumpath::Logger#log
|
|
69
93
|
def log(title)
|
|
94
|
+
return unless verbose
|
|
95
|
+
|
|
70
96
|
block_given? ? logger.log(title, &-> { yield }) : logger.log(title)
|
|
71
97
|
end
|
|
72
98
|
|
|
73
|
-
# A
|
|
99
|
+
# A bounded in-memory cache for normalized path expressions that threads can share
|
|
74
100
|
#
|
|
75
101
|
# @private
|
|
76
|
-
# @return [
|
|
77
|
-
|
|
78
|
-
|
|
102
|
+
# @return [Enumpath::PathCache]
|
|
103
|
+
attr_reader :path_cache
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
# Runs the block with the call's `:verbose` option in effect for this thread, then restores the previous value,
|
|
108
|
+
# even if the block raises. A nil option leaves the setting alone.
|
|
109
|
+
def with_verbose(option)
|
|
110
|
+
return yield if option.nil?
|
|
111
|
+
|
|
112
|
+
@verbose_lock.synchronize { @verbose_overrides += 1 }
|
|
113
|
+
previous = Thread.current[VERBOSE_KEY]
|
|
114
|
+
Thread.current[VERBOSE_KEY] = option ? true : false
|
|
115
|
+
begin
|
|
116
|
+
yield
|
|
117
|
+
ensure
|
|
118
|
+
Thread.current[VERBOSE_KEY] = previous
|
|
119
|
+
@verbose_lock.synchronize { @verbose_overrides -= 1 }
|
|
120
|
+
end
|
|
79
121
|
end
|
|
80
122
|
end
|
|
81
123
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: enumpath
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Chris Bloom
|
|
@@ -16,6 +16,9 @@ dependencies:
|
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
18
|
version: '1.4'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '3'
|
|
19
22
|
type: :runtime
|
|
20
23
|
prerelease: false
|
|
21
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -23,34 +26,9 @@ dependencies:
|
|
|
23
26
|
- - ">="
|
|
24
27
|
- !ruby/object:Gem::Version
|
|
25
28
|
version: '1.4'
|
|
26
|
-
-
|
|
27
|
-
name: mini_cache
|
|
28
|
-
requirement: !ruby/object:Gem::Requirement
|
|
29
|
-
requirements:
|
|
30
|
-
- - "~>"
|
|
31
|
-
- !ruby/object:Gem::Version
|
|
32
|
-
version: 1.1.0
|
|
33
|
-
type: :runtime
|
|
34
|
-
prerelease: false
|
|
35
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
-
requirements:
|
|
37
|
-
- - "~>"
|
|
38
|
-
- !ruby/object:Gem::Version
|
|
39
|
-
version: 1.1.0
|
|
40
|
-
- !ruby/object:Gem::Dependency
|
|
41
|
-
name: to_regexp
|
|
42
|
-
requirement: !ruby/object:Gem::Requirement
|
|
43
|
-
requirements:
|
|
44
|
-
- - "~>"
|
|
45
|
-
- !ruby/object:Gem::Version
|
|
46
|
-
version: 0.2.1
|
|
47
|
-
type: :runtime
|
|
48
|
-
prerelease: false
|
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
-
requirements:
|
|
51
|
-
- - "~>"
|
|
29
|
+
- - "<"
|
|
52
30
|
- !ruby/object:Gem::Version
|
|
53
|
-
version:
|
|
31
|
+
version: '3'
|
|
54
32
|
- !ruby/object:Gem::Dependency
|
|
55
33
|
name: null-logger
|
|
56
34
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -193,6 +171,8 @@ files:
|
|
|
193
171
|
- lib/enumpath/operator/wildcard.rb
|
|
194
172
|
- lib/enumpath/path.rb
|
|
195
173
|
- lib/enumpath/path/normalized_path.rb
|
|
174
|
+
- lib/enumpath/path_cache.rb
|
|
175
|
+
- lib/enumpath/regexp_parser.rb
|
|
196
176
|
- lib/enumpath/regexp_timeout.rb
|
|
197
177
|
- lib/enumpath/resolver/object_inspector.rb
|
|
198
178
|
- lib/enumpath/resolver/property.rb
|