Module: Enumpath

Defined in:
lib/enumpath.rb,
lib/enumpath/path.rb,
lib/enumpath/logger.rb,
lib/enumpath/results.rb,
lib/enumpath/version.rb,
lib/enumpath/operator.rb,
lib/enumpath/path_cache.rb,
lib/enumpath/operator/base.rb,
lib/enumpath/regexp_parser.rb,
lib/enumpath/operator/child.rb,
lib/enumpath/operator/slice.rb,
lib/enumpath/operator/union.rb,
lib/enumpath/regexp_timeout.rb,
lib/enumpath/resolver/simple.rb,
lib/enumpath/operator/wildcard.rb,
lib/enumpath/resolver/property.rb,
lib/enumpath/path/normalized_path.rb,
lib/enumpath/resolver/object_inspector.rb,
lib/enumpath/operator/filter_expression.rb,
lib/enumpath/operator/recursive_descent.rb,
lib/enumpath/operator/subscript_expression.rb

Overview

TODO: Investigate supporting anchored paths ($.foo.bar..., @.foo.bar...)

Defined Under Namespace

Modules: Operator, RegexpParser, RegexpTimeout, Resolver Classes: Path, PathCache, Results

Constant Summary collapse

VERBOSE_KEY =

The Thread.current key that holds the :verbose option of the current apply call

:enumpath_verbose
VERSION =
'0.4.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.verbose ⇒ true, false

Whether verbose mode is enabled. When enabled, the Logger will print information to the logging stream to assist in debugging path expressions. During an apply call this is the call's :verbose option, if it has one, and only in that call's thread; otherwise it is the default set with verbose=. Defaults to false

Returns:

  • (true, false)


43
44
45
46
47
48
# File 'lib/enumpath.rb', line 43

def verbose
  return @verbose if @verbose_overrides.zero?

  override = Thread.current[VERBOSE_KEY]
  override.nil? ? @verbose : override
end

Class Method Details

.apply(path, enum, options = {}) ⇒ Object

Resolve a path expression against an enumerable

Parameters:

  • options (optional, Hash) (defaults to: {})

Options Hash (options):

  • :result_type (Symbol) — default: :value —

    The type of results to return, :value or :path

  • :verbose (true, false) — default: Enumpath.verbose —

    Whether to enable additional output for debugging, for this call only

  • :property_resolver (Symbol, #call) — default: :safe —

    Which methods a path may call on an object when a segment is not an index, key, or member: :safe (read-only methods only), :readers (:safe plus public no-argument methods written in Ruby, including those of installed gems, and ActiveModel-style attributes; not read-only, for trusted data only), :none, :all (any public method that expects no arguments, the behavior of version 0.1), or a callable ->(object, name) { ... } that returns true to allow the call. Applies only to this call; a chained Enumpath::Results#apply uses the default unless the option is passed again. See Enumpath::Resolver::Property.

  • :regexp_timeout (Numeric, nil) — default: 1.0 —

    The most seconds a regular expression in a filter may take to match, or nil for no limit. A match that runs out of time does not match, and so do the regular expressions after it in the same call. Needs Ruby 3.2 or later; older Rubies ignore it. See RegexpTimeout.

Raises:

  • (ArgumentError) —

    if :property_resolver is not one of the values above, or :regexp_timeout is not nil or a positive number



72
73
74
75
76
77
78
79
80
81
# File 'lib/enumpath.rb', line 72

def apply(path, enum, options = {})
  with_verbose(options[:verbose]) do
    logger.level = 0 if verbose
    Enumpath::Resolver::Property.with_policy(options[:property_resolver]) do
      Enumpath::RegexpTimeout.with_limit(options.fetch(:regexp_timeout, Enumpath::RegexpTimeout::DEFAULT)) do
        Enumpath::Path.new(path, result_type: options[:result_type]).apply(enum)
      end
    end
  end
end