Class: Mobj::Token
Instance Method Summary collapse
- #extract(obj, path) ⇒ Object
- #find(obj, match) ⇒ Object
-
#initialize(type, *args) ⇒ Token
constructor
A new instance of Token.
- #to_s ⇒ Object
- #walk(obj, root = obj) ⇒ Object
Constructor Details
#initialize(type, *args) ⇒ Token
Returns a new instance of Token.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mobj.rb', line 15 def initialize(type, *args) @type, @path, = type.to_sym, nil, {} tokens = [] args.each do |arg| if arg.is_a? Hash .merge!(arg) elsif arg.is_a? String tokens << arg.sym else tokens << arg end end @path = tokens.sequester end |
Instance Method Details
#extract(obj, path) ⇒ Object
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 |
# File 'lib/mobj.rb', line 32 def extract(obj, path) obj.__mobj__reparent if path == :* || obj.nil? obj elsif obj.is_a?(Array) if path[0] == '*' && obj.respond_to?(path[1..-1].sym) obj.__send__(path[1..-1].sym) else obj.map { |o| extract(o, path) } end elsif path.is_a?(Array) path.map { |pth| obj[pth.sym] } elsif path[0] == '*' && obj.respond_to?(path[1..-1].sym) obj.__send__(path[1..-1].sym) elsif obj.respond_to?(:[]) && (obj.is_a?(Hash) || path.to_s =~ /[0-9.-]+/) if obj.is_a?(Hash) obj[path.sym] else obj[path.to_s.to_i] if path.to_s =~ /^\d+$/ end elsif obj.respond_to?(path.sym) obj.__send__(path.sym) else nil end end |
#find(obj, match) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mobj.rb', line 59 def find(obj, match) if obj.is_a?(Array) obj.map do |child| find(child, match) end elsif obj.respond_to?(:keys) found = obj.keys.each.with_object({}) { |key, fnd| m = key.to_s.match(match) fnd[key] = m if m } flat = found.values.flat_map(&:captures).empty? found.each.with_object(flat ? [] : {}) { |(key, m), map| if map.is_a?(Array) map << obj[key] else named = m.to_h.invert name = if named.empty? m.captures.empty? ? key : m.captures.sequester else named.find { |k, v| !k.nil? }.attempt(key).last end map[name] = obj[key] end } end end |
#to_s ⇒ Object
30 |
# File 'lib/mobj.rb', line 30 def to_s() "#{@type.to_s.upcase}(#@path#{ " => #@options" unless @options.empty?})" end |
#walk(obj, root = obj) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/mobj.rb', line 89 def walk(obj, root = obj) obj, root = Circle.wrap(obj), Circle.wrap(root) val = case @type when :literal @path.to_s when :path extract(obj, @path) when :regex find(obj, @path) when :up if obj.respond_to? :parent obj.__mobj__parent || obj.__mobj__parent else obj.__mobj__parent end when :any if obj.is_a?(Array) obj.map { |o| walk(o, root) } else @path.return_first { |token| token.walk(obj, root) } end when :all matches = @path.map { |token| token.walk(obj, root) } matches.compact.size == @path.size ? matches : nil when :each @path.map { |token| token.walk(obj, root) } when :lookup lookup = @path.walk(obj) if lookup.is_a?(Array) lookup.flatten.map { |lu| lu.tokenize.walk(root) }.flatten(1) else lookup.tokenize.walk(root) end when :inverse raise "not implemented yet. not sure how to implement yet, actually. please continue to hold. your call is important to us." when :root tree = [@path].flatten while (path = tree.shift) obj = path.walk(obj) end obj.is_a?(Array) ? obj.flatten : obj end val = [:indexes] ? val.values_at(*[:indexes]) : val val end |