diff options
| author | mo khan <mo@mokhan.ca> | 2013-07-20 18:04:12 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-07-20 18:04:12 -0600 |
| commit | 255ebedfc89cebb7c98257c528ae4961eb89f6cd (patch) | |
| tree | deae9bed8d56ed77f07325fda271a6e7fc27850e | |
| parent | e0837096c9c8011846bb35a5e7c969a657a51e8a (diff) | |
inline insertion
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | lib/humble/database_table.rb | 13 |
2 files changed, 6 insertions, 9 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index 4a3f84e..e923263 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - humble (0.0.1374364685) + humble (0.0.1374365008) sequel GEM diff --git a/lib/humble/database_table.rb b/lib/humble/database_table.rb index 69531f1..ca70a12 100644 --- a/lib/humble/database_table.rb +++ b/lib/humble/database_table.rb @@ -20,19 +20,18 @@ module Humble def persist(connection, item) if has_default_value?(item) - id = connection[@name].insert(insert(item)) - item.instance_variable_set('@id', id) + insert(item, connection[@name]) else connection[@name].update(update(item)) end end + private + def has_default_value?(item) primary_key_column.default == item.id end - private - def primary_key_column @columns.find { |x| x.primary_key? } end @@ -43,10 +42,8 @@ module Humble end end - def insert(item) - prepare_statement do |column| - column.prepare_insert(item) - end + def insert(item, connection) + item.instance_variable_set('@id', connection.insert(prepare_statement { |column| column.prepare_insert(item) })) end def update(item) |
