lib/supermodel/base.rb in supermodel-0.0.5 vs lib/supermodel/base.rb in supermodel-0.0.6

- old
+ new

@@ -55,11 +55,11 @@ end # Removes all records and executes # destory callbacks. def destroy_all - records.dup.each {|r| r.destroy } + all.each {|r| r.destroy } end # Removes all records without executing # destroy callbacks. def delete_all @@ -72,17 +72,21 @@ def create(atts = {}) rec = self.new(atts) rec.save && rec end + def find_by_attribute(name, value) #:nodoc: + records.find {|r| r.name == value } + end + def method_missing(method_symbol, *args) #:nodoc: method_name = method_symbol.to_s if method_name =~ /^find_by_(\w+)!/ send("find_by_#{$1}", *args) || raise(UnknownRecord) elsif method_name =~ /^find_by_(\w+)/ - records.find {|r| r.send($1) == args.first } + find_by_attribute($1, args.first) elsif method_name =~ /^find_or_create_by_(\w+)/ send("find_by_#{$1}", *args) || create($1 => args.first) else super end @@ -154,10 +158,12 @@ def exists? !new? end def load(attributes) #:nodoc: - @attributes.merge!(attributes) + attributes.each {|(name, value)| + self.send("#{name}=".to_sym, value) + } end def update_attribute(name, value) self.send("#{name}=".to_sym, value) self.save \ No newline at end of file