Skip to content
This repository has been archived by the owner on Dec 22, 2020. It is now read-only.

Adding array selector array[0] #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/mosql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ def find_ns!(ns)

def fetch_and_delete_dotted(obj, dotted)
pieces = dotted.split(".")
selector = pieces.last
if selector =~ /\[/
number = Integer(selector.match(/\[(.+?)\]/)[1])
pieces = pieces[0..-2] + [selector.gsub!("[#{number}]", "")]
else
number = nil
end

breadcrumbs = []
while pieces.length > 1
key = pieces.shift
Expand All @@ -153,7 +161,12 @@ def fetch_and_delete_dotted(obj, dotted)
return nil unless obj.is_a?(Hash)
end

val = obj.delete(pieces.first)
if number
ary = obj.delete(selector)
val = ary && ary[number]
else
val = obj.delete(selector)
end

breadcrumbs.reverse.each do |obj, key|
obj.delete(key) if obj[key].empty?
Expand Down