diff options
| author | mo khan <mo@mokhan.ca> | 2013-07-20 17:42:18 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-07-20 17:42:18 -0600 |
| commit | 2a9a3ff009253bf39750e0a7daee64eb6d0ee99e (patch) | |
| tree | 0cde39b08881b83473aa8e71c9318266892328c7 | |
| parent | af819506568f94010ef97d589416e79472ea9fbb (diff) | |
move persistence down to database table
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | lib/humble/database_table.rb | 28 | ||||
| -rw-r--r-- | lib/humble/mapping_configuration.rb | 7 |
3 files changed, 21 insertions, 16 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index 1fcb32d..eda2e80 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - humble (0.0.1374363495) + humble (0.0.1374363716) sequel GEM diff --git a/lib/humble/database_table.rb b/lib/humble/database_table.rb index a3e3d63..69531f1 100644 --- a/lib/humble/database_table.rb +++ b/lib/humble/database_table.rb @@ -18,15 +18,12 @@ module Humble @columns << Column.new(:name => name) end - def insert(item) - prepare_statement do |column| - column.prepare_insert(item) - end - end - - def update(item) - prepare_statement do |column| - column.prepare_update(item) + def persist(connection, item) + if has_default_value?(item) + id = connection[@name].insert(insert(item)) + item.instance_variable_set('@id', id) + else + connection[@name].update(update(item)) end end @@ -45,5 +42,18 @@ module Humble result.merge(yield(column)) end end + + def insert(item) + prepare_statement do |column| + column.prepare_insert(item) + end + end + + def update(item) + prepare_statement do |column| + column.prepare_update(item) + end + end + end end diff --git a/lib/humble/mapping_configuration.rb b/lib/humble/mapping_configuration.rb index 6b4987a..82a7c21 100644 --- a/lib/humble/mapping_configuration.rb +++ b/lib/humble/mapping_configuration.rb @@ -10,12 +10,7 @@ module Humble end def save_using(connection, item) - if @table.has_default_value?(item) - id = connection[@table.name].insert(@table.insert(item)) - item.instance_variable_set('@id', id) - else - connection[@table.name].update(@table.update(item)) - end + @table.persist(connection, item) end def is_for?(item) |
