Commit 98a626f
Changed files (3)
lib
lib/humble/database_table.rb
@@ -1,5 +1,7 @@
module Humble
class DatabaseTable
+ include Enumerable
+
attr_reader :name
attr_accessor :type
@@ -24,11 +26,9 @@ module Humble
add(Column.new(name))
end
- def persist(connection, item)
- if @primary_key.has_default_value?(item)
- @primary_key.apply(insert(item, connection[@name]) , item, nil)
- else
- update(item, connection[@name])
+ def each
+ @columns.each do |column|
+ yield column
end
end
@@ -40,20 +40,10 @@ module Humble
@columns.find { |x| x.matches?(key) }
end
- private
-
def prepare_statement_for(item)
@columns.inject({}) do |result, column|
result.merge(column.prepare(item))
end
end
-
- def insert(item, dataset)
- dataset.insert(prepare_statement_for(item))
- end
-
- def update(item, dataset)
- dataset.update(prepare_statement_for(item))
- end
end
end
lib/humble/mapping_configuration.rb
@@ -9,8 +9,12 @@ module Humble
ResultSet.new(connection[@table.name], DefaultMapper.new(@table, session))
end
- def save_using(connection, entity)
- @table.persist(connection, entity)
+ def save_using(session, entity)
+ if primary_key.has_default_value?(entity)
+ primary_key.apply(insert(entity, session.create_connection[@table.name]) , entity, nil)
+ else
+ update(entity, session.create_connection[@table.name])
+ end
end
def delete_using(connection, entity)
@@ -23,6 +27,20 @@ module Humble
private
+ def primary_key
+ @primary_key ||= @table.find do |column|
+ column.primary_key?
+ end
+ end
+
+ def insert(item, dataset)
+ dataset.insert(@table.prepare_statement_for(item))
+ end
+
+ def update(item, dataset)
+ dataset.update(@table.prepare_statement_for(item))
+ end
+
class DefaultMapper
def initialize(table, session)
@table = table
lib/humble/session.rb
@@ -14,7 +14,7 @@ module Humble
end
def save(entity)
- mapping_for(entity).save_using(create_connection, entity)
+ mapping_for(entity).save_using(self, entity)
end
def find(clazz, id)