class HashPath::Part
This class encapsulates a single portion of a hash path path
Public Instance Methods
evaluates?(object)
click to toggle source
# File lib/bblib/core/hash_path/part.rb, line 62 def evaluates?(object) return true unless evaluation eval(evaluation.gsub('$', 'object.value')) rescue => e # The eval resulted in an error so we return false false end
key_match?(key, object)
click to toggle source
# File lib/bblib/core/hash_path/part.rb, line 17 def key_match?(key, object) case selector when String selector == '*' || key.to_s == selector when Integer key.to_i == selector when Range selector === key || object.size.times.to_a.include?(key) else selector === key end end
matches(object)
click to toggle source
# File lib/bblib/core/hash_path/part.rb, line 34 def matches(object) matches = [] if special_selector? begin [object.send(*selector.uncapsulate('{').split(':'))].flatten(1).compact.each do |match| matches << match if evaluates?(match) end rescue StandardError => e # Nothing, the special selector failed # puts e end elsif object.children? object.children.each do |k, v| matches << v if key_match?(k, object) && evaluates?(v) matches += matches(v) if recursive? && v.children? end else casted = case when !object.value.is_a?(Hash) && !object.value.is_a?(Array) && object.value.respond_to?(:to_tree_hash) && object.method(:to_tree_hash).arity <= 0 object.value.to_tree_hash when object.value.is_a?(BBLib::Effortless) object.value.serialize.to_tree_hash end matches += matches(casted) if casted end matches end
parse(path)
click to toggle source
# File lib/bblib/core/hash_path/part.rb, line 10 def parse(path) evl = path.scan(/\(.*\)$/).first self.evaluation = evl ? evl.uncapsulate('(', limit: 1) : evl self.recursive = path.start_with?('[[:recursive:]]') self.selector = parse_selector(evl ? path.sub(evl, '') : path) end
special_selector?()
click to toggle source
# File lib/bblib/core/hash_path/part.rb, line 30 def special_selector? selector.is_a?(String) && /^\{.*\}$/ =~ selector end
Protected Instance Methods
parse_selector(str)
click to toggle source
# File lib/bblib/core/hash_path/part.rb, line 76 def parse_selector(str) str = str.gsub('.[[:recursive:]]', '..') if str =~ /^\[\d+.*\d+\]$/ str = str.gsub('[[:recursive:]]', '') if recursive? if str =~ /^\/.*\/[imx]?$/ str.to_regex elsif str =~ /^\[\d+\]$/ str.uncapsulate('[').to_i elsif str =~ /\[\-?\d+\.{2,3}\-?\d+\]/ Range.new(*str.scan(/\-?\d+/).map(&:to_i)) else str.gsub('\\.', '.') end end
simple_init(*args)
click to toggle source
# File lib/bblib/core/hash_path/part.rb, line 72 def simple_init(*args) parse(args.first) if args.first.is_a?(String) end