Browse files

extract model interaction code to series of methods so it can be over…

…riden in builders
  • Loading branch information...
1 parent 243f2ef commit 92477f5719e8769e59588560676a3151de6429e8 @timcharper timcharper committed Dec 15, 2011
Showing with 34 additions and 14 deletions.
  1. +34 −14 lib/workbench.rb
View
48 lib/workbench.rb
@@ -1,7 +1,36 @@
-require File.expand_path('../workbench/string_helpers', __FILE__)
+require 'workbench/string_helpers'
+
module Workbench
COUNTERS = Hash.new(0)
+ module InstanceMethods
+ def assign_model_attributes(model, attributes)
+ attributes.each do |k, v|
+ model.send("#{k}=", v)
+ end
+ end
+
+ def save_model(model)
+ if model.respond_to?(:save!) # save should raise if unsuccessful
+ model.save!
+ else
+ model.save
+ end
+ end
+
+ def find_model_by_attributes(klass, attributes)
+ klass.where(attributes).first
+ end
+
+ def new_model(klass)
+ klass.new
+ end
+ end
+
+ def self.extended(klass)
+ klass.send :include, InstanceMethods
+ end
+
# Declare that the next builder method is to use the said class
#
# module Builders
@@ -77,10 +106,8 @@ def method_added(name)
def define_builder_methods(name, klass, counter_klass)
define_method("new_#{name}") do |*args|
attributes = args[0] || { }
- klass.new.tap do |model|
- attributes.each do |k, v|
- model.send("#{k}=", v)
- end
+ new_model(klass).tap do |model|
+ assign_model_attributes(model, attributes)
build_method = method("#{name}_defaults")
p = [model]
p << Workbench.counter(counter_klass) if build_method.arity >= 2 || build_method.arity <= -1
@@ -90,18 +117,12 @@ def define_builder_methods(name, klass, counter_klass)
end
define_method("create_#{name}") do |*args|
- send("new_#{name}", *args).tap do |model|
- if model.respond_to?(:save!) # save should raise if unsuccessful
- model.save!
- else
- model.save
- end
- end
+ send("new_#{name}", *args).tap { |model| save_model(model) }
end
define_method("find_or_create_#{name}") do |*args|
attrs = args[0] || { }
- klass.where(attrs).first || send("create_#{name}", attrs)
+ find_model_by_attributes(klass, attrs) || send("create_#{name}", attrs)
end
end
@@ -110,5 +131,4 @@ def inferred_builder_class_name(name)
$1.to_sym
end
end
-
end

0 comments on commit 92477f5

Please sign in to comment.