Commit 3c30a54

mo khan <mo@mokhan.ca>
2013-07-21 00:56:48
consolidate prepare_insert and prepare_update into a single method
1 parent 9f34767
lib/humble/column.rb
@@ -4,6 +4,24 @@ module Humble
       @column_name = name
     end
 
+    def prepare(item)
+      if primary_key? && has_default_value?
+        prepare_insert(item)
+      else
+        prepare_update(item)
+      end
+    end
+
+    def primary_key?
+      false
+    end
+
+    protected
+
+    attr_reader :column_name
+
+    private
+
     def prepare_insert(item)
       return {} if primary_key?
       value = item.instance_variable_get("@#{column_name}")
@@ -15,13 +33,6 @@ module Humble
       { column_name.to_sym => value }
     end
 
-    def primary_key?
-      false
-    end
-
-    protected
-
-    attr_reader :column_name
   end
 
   class PrimaryKeyColumn < Column
lib/humble/database_table.rb
@@ -20,7 +20,7 @@ module Humble
 
     def persist(connection, item)
       if @primary_key.has_default_value?(item)
-        insert(item, connection[@name])
+        @primary_key.apply(insert(item, connection[@name]) , item)
       else
         update(item, connection[@name])
       end
@@ -39,12 +39,11 @@ module Humble
     end
 
     def insert(item, connection)
-      id = connection.insert(prepare_statement { |column| column.prepare_insert(item) })
-      @primary_key.apply(id, item)
+      connection.insert(prepare_statement { |column| column.prepare(item) })
     end
 
     def update(item, connection)
-      connection.update( prepare_statement { |column| column.prepare_update(item) })
+      connection.update(prepare_statement { |column| column.prepare(item) })
     end
   end
 end
Gemfile.lock
@@ -1,7 +1,7 @@
 PATH
   remote: .
   specs:
-    humble (0.0.1374367897)
+    humble (0.0.1374368118)
       sequel
 
 GEM