Skip to content

Commit

Permalink
Make seed_fu go a bit faster: create a method for setting attributes …
Browse files Browse the repository at this point in the history
…instead of hitting slow method_missing, use something slightly faster for creating the condition hash
  • Loading branch information
Matthew Beale committed Jul 17, 2009
1 parent 7310cc3 commit 15c7b90
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/seed-fu.rb
Expand Up @@ -25,10 +25,6 @@ def set_constraints(*constraints)
end
end

def set_attribute(name, value)
@data[name.to_sym] = value
end

def plant! insert_only=false
record = get
return if !record.new_record? and insert_only
Expand All @@ -41,8 +37,9 @@ def plant! insert_only=false
end

def method_missing(method_name, *args) #:nodoc:
if (match = method_name.to_s.match(/(.*)=$/)) && args.size == 1
set_attribute(match[1], args.first)
if args.size == 1 and (match = method_name.to_s.match(/(.*)=$/))
self.class.class_eval "def #{method_name} arg; @data[:#{match[1]}] = arg; end"
send(method_name, args[0])
else
super
end
Expand All @@ -61,7 +58,7 @@ def get
end

def condition_hash
@data.reject{|a,v| !@constraints.include?(a)}
@constraints.inject({}) {|a,c| a[c] = @data[c]; a }
end
end
end
Expand All @@ -87,4 +84,4 @@ def self.seed_many(*constraints)
end
end
end
end
end

0 comments on commit 15c7b90

Please sign in to comment.