Class: HashPath::Part
- Inherits:
-
Object
- Object
- HashPath::Part
- Includes:
- BBLib::Effortless
- Defined in:
- lib/bblib/core/hash_path/part.rb
Overview
This class encapsulates a single portion of a hash path path
Instance Method Summary collapse
- #evaluates?(object) ⇒ Boolean
- #key_match?(key, object) ⇒ Boolean
- #matches(object) ⇒ Object
- #parse(path) ⇒ Object
- #special_selector? ⇒ Boolean
Methods included from BBLib::Effortless
Instance Method Details
#evaluates?(object) ⇒ Boolean
62 63 64 65 66 67 68 |
# 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) ⇒ Boolean
17 18 19 20 21 22 23 24 25 26 27 28 |
# 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) ⇒ Object
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 |
# 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) ⇒ Object
10 11 12 13 14 15 |
# 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? ⇒ Boolean
30 31 32 |
# File 'lib/bblib/core/hash_path/part.rb', line 30 def special_selector? selector.is_a?(String) && /^\{.*\}$/ =~ selector end |