-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenumpath.rb
62 lines (55 loc) · 1.83 KB
/
enumpath.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
require 'mini_cache'
require 'enumpath/logger'
require 'enumpath/operator'
require 'enumpath/path'
require 'enumpath/results'
require 'enumpath/resolver/simple'
require 'enumpath/resolver/property'
require 'enumpath/version'
# A JSONPath-compatible library for navigating Ruby objects using path expressions
module Enumpath
@verbose = false
class << self
# Whether verbose mode is enabled. When enabled, the {Enumpath::Logger} will print
# information to the logging stream to assist in debugging path expressions.
# Defaults to false
#
# @return [true,false]
attr_accessor :verbose
# Resolve a path expression against an enumerable
#
# @param path (see Enumpath::Path#initialize)
# @param enum (see Enumpath::Path#apply)
# @param options [optional, Hash]
# @option options [Symbol] :result_type (:value) The type of results to return, `:value` or `:path`
# @option options [true, false] :verbose (false) Whether to enable additional output for debugging
# @return (see Enumpath::Path#apply)
def apply(path, enum, options = {})
logger.level = 0
@verbose = options.delete(:verbose) || false
Enumpath::Path.new(path, result_type: options.delete(:result_type)).apply(enum)
end
# The {Enumpath::Logger} instance to use with verbose mode
#
# @private
# @return [Enumpath::Logger]
def logger
@logger ||= Enumpath::Logger.new
end
# A shortcut to {Enumpath::logger.log}
#
# @private
# @see Enumpath::Logger#log
def log(title)
block_given? ? logger.log(title, &Proc.new) : logger.log(title)
end
# A lightweight in-memory cache for caching normalized path expressions
#
# @private
# @return [MiniCache::Store]
def path_cache
@path_cache ||= MiniCache::Store.new
end
end
end