lib/mosql/schema.rb in mosql-0.4.0 vs lib/mosql/schema.rb in mosql-0.4.1

- old
+ new

@@ -157,14 +157,28 @@ end val end - def fetch_special_source(obj, source) + def fetch_exists(obj, dotted) + pieces = dotted.split(".") + while pieces.length > 1 + key = pieces.shift + obj = obj[key] + return false unless obj.is_a?(Hash) + end + obj.has_key?(pieces.first) + end + + def fetch_special_source(obj, source, original) case source when "$timestamp" Sequel.function(:now) + when /^\$exists (.+)/ + # We need to look in the cloned original object, not in the version that + # has had some fields deleted. + fetch_exists(original, $1) else raise SchemaError.new("Unknown source: #{source}") end end @@ -186,18 +200,23 @@ end def transform(ns, obj, schema=nil) schema ||= find_ns!(ns) - obj = obj.dup + original = obj + + # Do a deep clone, because we're potentially going to be + # mutating embedded objects. + obj = BSON.deserialize(BSON.serialize(obj)) + row = [] schema[:columns].each do |col| source = col[:source] type = col[:type] if source.start_with?("$") - v = fetch_special_source(obj, source) + v = fetch_special_source(obj, source, original) else v = fetch_and_delete_dotted(obj, source) case v when Hash v = JSON.dump(Hash[v.map { |k,v| [k, transform_primitive(v)] }])