Commits

Sean Cribbs committed 0ece6bc

Add update_attributes and update_attribute (Closes #37). Fix minor bug for 1.8.7. Add Gemfile for easy dev.

  • Participants
  • Parent commits ebef99c

Comments (0)

Files changed (7)

+source "http://rubygems.org/"
+
+gem 'yajl-ruby'
+gem 'curb', '>0.6'
+gem 'activemodel', '3.0.0.beta4'
+gem 'rspec', '2.0.0.beta.12'
+gem 'tzinfo'
+gem 'rake'

ripple/lib/ripple/associations/one.rb

     module One
       include Instantiators
 
+      def to_a
+        [self]
+      end
+      
       protected
       def instantiate_target(instantiator, attrs={})
         @target = klass.send(instantiator, attrs)

ripple/lib/ripple/document/persistence.rb

           @new || false
         end
 
+        # Updates a single attribute and then saves the document
+        # NOTE: THIS SKIPS VALIDATIONS! Use with caution.
+        # @return [true,false] whether the document succeeded in saving
+        def update_attribute(attribute, value)
+          send("#{attribute}=", value)
+          save(:validate => false)
+        end
+
+        # Writes new attributes and then saves the document
+        # @return [true,false] whether the document succeeded in saving
+        def update_attributes(attrs)
+          self.attributes = attrs
+          save
+        end
+
         # Saves the document in Riak.
         # @return [true,false] whether the document succeeded in saving
-        def save
+        def save(*args)
           robject.key = key if robject.key != key
           robject.data = attributes_for_persistence
           robject.store(self.class.quorums.slice(:w,:dw))

ripple/lib/ripple/embedded_document/persistence.rb

           end
         end
 
+        def update_attributes(attrs)
+          self.attributes = attrs
+          save
+        end
+
+        def update_attribute(attribute, value)
+          send("#{attribute}=", value)
+          save(:validate => false)
+        end
+
         def save(*args)
           if _root_document
             _root_document.save(*args)
         def _root_document
           @_parent_document.try(:_root_document)
         end
-        
+
         def _parent_document=(value)
           @_parent_document = value
         end

ripple/lib/ripple/validations.rb

       def save!
         (raise Ripple::DocumentInvalid.new(self) unless save) || true
       end
+
+      def update_attributes!(attrs)
+        self.attributes = attrs
+        save!
+      end
     end
   end
 end

ripple/spec/ripple/embedded_document/persistence_spec.rb

     @root.addresses << @addr
     @root.attributes_for_persistence.should == {'_type' => 'User', 'addresses' => [{'_type' => 'Address', 'street' => nil}]}
   end
+
+  it "should modify its attributes and save" do
+    @addr.should_receive(:save).and_return(true)
+    @addr.update_attributes(:street => "4 Folsom Ave")
+    @addr.street.should == "4 Folsom Ave"
+  end
+
+  it "should update a single attribute and save without validations" do
+    @addr.should_receive(:save).with(:validate => false).and_return(true)
+    @addr.update_attribute(:street, "4 Folsom Ave")
+    @addr.street.should == "4 Folsom Ave"
+  end
 end

ripple/spec/ripple/persistence_spec.rb

     @widget.changes.should be_blank
   end
 
+  it "should modify attributes and save a new object" do
+    json = @widget.attributes.merge("_type" => "Widget", "size" => 5).to_json
+    @http.should_receive(:post).with(201, "/riak/", "widgets", an_instance_of(Hash), json, hash_including("Content-Type" => "application/json")).and_return(:code => 201, :headers => {'location' => ["/riak/widgets/new_widget"]})
+    @widget.update_attributes(:size => 5)
+    @widget.key.should == "new_widget"
+    @widget.should_not be_a_new_record
+    @widget.changes.should be_blank
+  end
+
+  it "should modify a single attribute and save a new object" do
+    json = @widget.attributes.merge("_type" => "Widget", "size" => 5).to_json
+    @http.should_receive(:post).with(201, "/riak/", "widgets", an_instance_of(Hash), json, hash_including("Content-Type" => "application/json")).and_return(:code => 201, :headers => {'location' => ["/riak/widgets/new_widget"]})
+    @widget.update_attribute(:size, 5)
+    @widget.key.should == "new_widget"
+    @widget.should_not be_a_new_record
+    @widget.changes.should be_blank
+    @widget.size.should == 5
+  end
+
   it "should instantiate and save a new object to riak" do
     json = @widget.attributes.merge(:size => 10, :_type => 'Widget').to_json
     @http.should_receive(:post).with(201, "/riak/", "widgets", an_instance_of(Hash), json, hash_including("Content-Type" => "application/json")).and_return(:code => 201, :headers => {'location' => ["/riak/widgets/new_widget"]})