Commit af81950

mo khan <mo@mokhan.ca>
2013-07-20 23:38:34
extract method to prepare insert/update
1 parent e062c55
lib/humble/database_table.rb
@@ -6,7 +6,6 @@ module Humble
       @columns = []
     end
 
-    # configuration methods
     def named(name)
       @name = name
     end
@@ -20,19 +19,17 @@ module Humble
     end
 
     def insert(item)
-      @columns.inject({}) do |result, column|
-        result.merge(column.prepare_insert(item))
+      prepare_statement do |column|
+        column.prepare_insert(item)
       end
     end
 
     def update(item)
-      @columns.inject({}) do |result, column|
-        result.merge(column.prepare_update(item))
+      prepare_statement do |column|
+        column.prepare_update(item)
       end
     end
 
-    # query methods
-
     def has_default_value?(item)
       primary_key_column.default == item.id
     end
@@ -42,5 +39,11 @@ module Humble
     def primary_key_column
       @columns.find { |x| x.primary_key? }
     end
+
+    def prepare_statement
+      @columns.inject({}) do |result, column|
+        result.merge(yield(column))
+      end
+    end
   end
 end
lib/humble/mapping_configuration.rb
@@ -10,7 +10,7 @@ module Humble
     end
 
     def save_using(connection, item)
-      if  @table.has_default_value?(item)
+      if @table.has_default_value?(item)
         id = connection[@table.name].insert(@table.insert(item))
         item.instance_variable_set('@id', id)
       else
@@ -19,7 +19,7 @@ module Humble
     end
 
     def is_for?(item)
-      item == @attributes[:type] || item.is_a?(@attributes[:type])
+      item == self[:type] || item.is_a?(self[:type])
     end
 
     def [](key)
@@ -29,8 +29,7 @@ module Humble
     private
 
     def mapper
-      @attributes[:mapper] || DefaultDataRowMapper.new(self)
+      self[:mapper] || DefaultDataRowMapper.new(self)
     end
   end
-
 end
Gemfile.lock
@@ -1,7 +1,7 @@
 PATH
   remote: .
   specs:
-    humble (0.0.1374363184)
+    humble (0.0.1374363495)
       sequel
 
 GEM