./lib/active_object/base.rb in activeobject-0.0.3 vs ./lib/active_object/base.rb in activeobject-0.0.4

- old
+ new

@@ -286,11 +286,11 @@ object end end - def initialize(attributes = nil) + def initialize(attributes = {}) self.id = UUID.new.generate @new_record = true assign_attributes(attributes) object = yield self if block_given? object @@ -325,10 +325,18 @@ def delete self.class.delete(id) unless new_record? @destroyed = true end + # 更新对象属性 + # <tt>options</tt>是需要更新的一组Hash + def update_attributes(options={}) + options.each do |key,value| + self.send("#{key}=",value) if self.respond_to?("#{key}=") + end + end + def destroy delete end # 从数据库中重新加载对象的属性 @@ -375,13 +383,14 @@ def attribute_present?(attribute) value = read_attribute(attribute) !value.blank? end - def assign_attributes(attributes=nil) - attributes.each do |key,value| - self.send "#{key}=",value - end unless attributes.nil? + def assign_attributes(attrs={}) + return if attrs.nil? || attrs.size==0 + attrs.each do |key,value| + self.send("#{key}=",value) if self.respond_to?("#{key}=") + end unless attrs.empty? end def create_or_update result = new_record? ? create : update @destroyed = false