Commit e062c55
Changed files (4)
lib/humble/column.rb
@@ -6,7 +6,13 @@ module Humble
def prepare_insert(item)
return {} if primary_key?
- key = @attributes[:name]
+ key = column_name
+ value = item.instance_variable_get("@#{key}")
+ { key.to_sym => value }
+ end
+
+ def prepare_update(item)
+ key = column_name
value = item.instance_variable_get("@#{key}")
{ key.to_sym => value }
end
@@ -14,5 +20,13 @@ module Humble
def primary_key?
@attributes[:primary_key]
end
+
+ def column_name
+ @attributes[:name]
+ end
+
+ def default
+ @attributes[:default]
+ end
end
end
lib/humble/database_table.rb
@@ -6,6 +6,7 @@ module Humble
@columns = []
end
+ # configuration methods
def named(name)
@name = name
end
@@ -23,5 +24,23 @@ module Humble
result.merge(column.prepare_insert(item))
end
end
+
+ def update(item)
+ @columns.inject({}) do |result, column|
+ result.merge(column.prepare_update(item))
+ end
+ end
+
+ # query methods
+
+ def has_default_value?(item)
+ primary_key_column.default == item.id
+ end
+
+ private
+
+ def primary_key_column
+ @columns.find { |x| x.primary_key? }
+ end
end
end
lib/humble/mapping_configuration.rb
@@ -10,13 +10,11 @@ module Humble
end
def save_using(connection, item)
- p @attributes
- p @table
- if item.id < 0
+ 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.insert(item))
+ connection[@table.name].update(@table.update(item))
end
end
Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- humble (0.0.1374362429)
+ humble (0.0.1374363184)
sequel
GEM