lib/ncmb/object.rb in ncmb-ruby-client-0.1.1 vs lib/ncmb/object.rb in ncmb-ruby-client-0.1.2

- old
+ new

@@ -10,10 +10,14 @@ def fields @fields end + def ClassName + @name + end + def method_missing(name, value = nil) if name =~ /.*=$/ sym = name.to_s.gsub(/(.*?)=$/, '\1').to_sym @fields[sym] = value else @@ -54,10 +58,32 @@ def path "#{base_path}/#{@fields[:objectId] || '' }" end + def saved? + @fields[:objectId] != nil + end + def post + @fields.each do |key, field| + if field.is_a?(NCMB::Object) + field.save unless field.saved? + @fields[key] = { + __type: "Pointer", + className: field.ClassName, + objectId: field.objectId + } + end + if field.is_a?(Array) && field[0].is_a?(NCMB::Object) + relation = NCMB::Relation.new + field.each do |sub_field| + sub_field.save unless sub_field.saved? + relation << sub_field + end + @fields[key] = relation + end + end result = @@client.post path, @fields @fields.merge!(result) self end alias :save :post