Commit f7b7e84
Changed files (7)
lib/humble/column.rb
@@ -15,7 +15,7 @@ module Humble
@column_name == column_name
end
- def apply(value, entity)
+ def apply(value, entity, session)
entity.public_send("#{@column_name}=", value)
end
@@ -51,14 +51,18 @@ module Humble
@type = type
end
- def apply(value, entity)
- child_entity = @type.new
- column = column_name.to_s.gsub(/_id/, '')
- entity.public_send("#{column}=", child_entity)
+ def apply(value, entity, session)
+ entity.public_send("#{attribute_name}=", session.find(@type, value))
end
def prepare(entity)
{ column_name.to_sym => '' }
end
+
+ private
+
+ def attribute_name
+ column_name.to_s.gsub(/_id/, '')
+ end
end
end
lib/humble/configuration.rb
@@ -2,13 +2,14 @@ module Humble
class Configuration
attr_reader :connection_string
- def initialize(connection_string)
- @mappings = []
+ def initialize(connection_string, table_builder = MappingConfigurationBuilder.new)
+ @mapping_configurations = []
@connection_string = connection_string
+ @table_builder = table_builder
end
def add(mapping)
- @mappings.push(prepare(mapping))
+ @mapping_configurations.push(MappingConfiguration.new(@table_builder.build(mapping), self))
end
def build_session_factory
@@ -16,16 +17,9 @@ module Humble
end
def mapping_for(item)
- @mappings.find do |mapping|
+ @mapping_configurations.find do |mapping|
mapping.matches?(item)
end
end
-
- private
-
- def prepare(mapping, builder = MappingConfigurationBuilder.new)
- mapping.run(builder)
- builder.build
- end
end
end
lib/humble/database_table.rb
@@ -24,13 +24,9 @@ module Humble
add(Column.new(name))
end
- def find_all_using(connection)
- ResultSet.new(connection[self.name], self)
- end
-
def persist(connection, item)
if @primary_key.has_default_value?(item)
- @primary_key.apply(insert(item, connection[@name]) , item)
+ @primary_key.apply(insert(item, connection[@name]) , item, nil)
else
update(item, connection[@name])
end
@@ -40,20 +36,12 @@ module Humble
@primary_key.destroy(connection[@name], entity)
end
- def map_from(row)
- entity = type.new
- row.each do |key, value|
- column_for(key).apply(value, entity)
- end
- entity
- end
-
- private
-
def column_for(key)
@columns.find { |x| x.matches?(key) }
end
+ private
+
def prepare_statement_for(item)
@columns.inject({}) do |result, column|
result.merge(column.prepare(item))
lib/humble/mapping_configuration.rb
@@ -1,11 +1,12 @@
module Humble
class MappingConfiguration
- def initialize(table)
+ def initialize(table, configuration)
@table = table
+ @configuration = configuration
end
- def find_all_using(connection)
- @table.find_all_using(connection)
+ def find_all_using(session, connection = session.create_connection)
+ ResultSet.new(connection[@table.name], DefaultMapper.new(@table, session))
end
def save_using(connection, entity)
@@ -19,5 +20,22 @@ module Humble
def matches?(item)
@table.type == item || item.is_a?(@table.type)
end
+
+ private
+
+ class DefaultMapper
+ def initialize(table, session)
+ @table = table
+ @session = session
+ end
+
+ def map_from(row)
+ @table.type.new.tap do |entity|
+ row.each do |key, value|
+ @table.column_for(key).apply(value, entity, @session)
+ end
+ end
+ end
+ end
end
end
lib/humble/mapping_configuration_builder.rb
@@ -1,9 +1,5 @@
module Humble
class MappingConfigurationBuilder
- def initialize(table = DatabaseTable.new)
- @table = table
- end
-
def table(name)
@table.named(name)
end
@@ -24,8 +20,10 @@ module Humble
@table.add(BelongsTo.new(foreign_key, type))
end
- def build
- MappingConfiguration.new(@table)
+ def build(mapping)
+ @table = DatabaseTable.new
+ mapping.run(self)
+ @table
end
end
end
lib/humble/session.rb
@@ -1,8 +1,10 @@
+require 'debugger'
+
module Humble
class Session
- def initialize(connection_factory, mapper_registry)
- @connection_factory = connection_factory
- @mapper_registry = mapper_registry
+ def initialize(session_factory, configuration)
+ @session_factory = session_factory
+ @configuration = configuration
end
def begin_transaction(&block)
@@ -20,7 +22,7 @@ module Humble
end
def find_all(clazz)
- mapping_for(clazz).find_all_using(create_connection)
+ mapping_for(clazz).find_all_using(self)
end
def delete(entity)
@@ -32,16 +34,14 @@ module Humble
@connection = nil
end
- private
-
- attr_reader :connection_factory, :mapper_registry
-
def create_connection
- @connection ||= connection_factory.create_connection
+ @connection ||= @session_factory.create_connection
end
+ private
+
def mapping_for(entity)
- mapper_registry.mapping_for(entity)
+ @configuration.mapping_for(entity)
end
end
end
humble.gemspec
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake"
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'sqlite3'
+ spec.add_development_dependency 'debugger'
end