Class: JSONP3::Search
- Inherits:
-
FunctionExtension
- Object
- FunctionExtension
- JSONP3::Search
- Defined in:
- lib/json_p3/function_extensions/search.rb
Overview
The standard search
function.
Constant Summary collapse
- ARG_TYPES =
%i[value_expression value_expression].freeze
- RETURN_TYPE =
:logical_expression
Instance Method Summary collapse
-
#call(value, pattern) ⇒ Object
Boolean.
-
#initialize(cache_size = 128, raise_errors: false) ⇒ Search
constructor
A new instance of Search.
Constructor Details
#initialize(cache_size = 128, raise_errors: false) ⇒ Search
Returns a new instance of Search.
17 18 19 20 21 22 |
# File 'lib/json_p3/function_extensions/search.rb', line 17 def initialize(cache_size = 128, raise_errors: false) super() @cache_size = cache_size @raise_errors = raise_errors @cache = LRUCache.new(cache_size) end |
Instance Method Details
#call(value, pattern) ⇒ Object
Returns Boolean.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/json_p3/function_extensions/search.rb', line 27 def call(value, pattern) return false unless pattern.is_a?(String) && value.is_a?(String) if @cache_size.positive? re = @cache[pattern] || Regexp.new(JSONP3.map_iregexp(pattern)) else re = Regexp.new(JSONP3.map_iregexp(pattern)) @cache[pattern] = re end re.match?(value) rescue RegexpError raise if @raise_errors false end |