Commit 72d8705
Changed files (7)
lib
spec
integration
fixtures
lib/humble/database_mapping.rb
@@ -1,57 +1,22 @@
module Humble
- class DefaultDataRowMapper
- def initialize(configuration)
- @configuration = configuration
- end
-
- def map_from(row)
- @configuration[:type].new(row)
- end
- end
-
- class MappingConfiguration
- def initialize(attributes = {})
- @attributes = {}
- @attributes[:mapper] = DefaultDataRowMapper.new(self)
- end
-
- def table(name)
- @attributes[:table] = name
- end
-
- def type(name)
- @attributes[:type] = name
- end
-
- def primary_key(name)
- @attributes[:primary_key] = name
- end
-
- def column(name)
- @attributes[:column] = name
- end
-
- def [](key)
- @attributes[key]
- end
- end
-
class DatabaseMapping
- def initialize
- @configuration = MappingConfiguration.new
- run(@configuration)
+ def initialize(builder = MappingConfigurationBuilder.new)
+ run(builder)
+ @configuration = builder.build
end
+ def run; end
+
def is_for?(item)
item == configuration[:type] || item.is_a?(configuration[:type])
end
def save_using(connection, item)
- connection[configuration[:table]].insert(:name => item.name)
+ configuration.save_using(connection, item)
end
- def find_all_using(connection, clazz)
- Results.new(connection[configuration[:table]], configuration[:mapper])
+ def find_all_using(connection)
+ configuration.find_all_using(connection)
end
private
lib/humble/default_data_row_mapper.rb
@@ -0,0 +1,11 @@
+module Humble
+ class DefaultDataRowMapper
+ def initialize(configuration)
+ @configuration = configuration
+ end
+
+ def map_from(row)
+ @configuration[:type].new(row)
+ end
+ end
+end
lib/humble/mapping_configuration.rb
@@ -0,0 +1,98 @@
+module Humble
+ class Column
+ def initialize(attributes)
+ @attributes = attributes
+ end
+
+ def prepare_insert(item)
+ return {} if primary_key?
+ key = @attributes[:name]
+ value = item.instance_variable_get("@#{key}")
+ { key.to_sym => value }
+ end
+
+ def primary_key?
+ @attributes[:primary_key]
+ end
+ end
+
+ class DatabaseTable
+ attr_reader :name
+
+ def initialize
+ @columns = []
+ end
+
+ def named(name)
+ @name = name
+ end
+
+ def primary_key(name, default: 0)
+ @columns << Column.new(:name => name, :default => default, :primary_key => true)
+ end
+
+ def add_column(name)
+ @columns << Column.new(:name => name)
+ end
+
+ def insert(item)
+ @columns.inject({}) do |result, column|
+ result.merge(column.prepare_insert(item))
+ end
+ end
+ end
+
+ class MappingConfiguration
+ def initialize(attributes, table)
+ @attributes = attributes
+ @table = table
+ end
+
+ def find_all_using(connection)
+ ResultSet.new(connection[@table.name], mapper)
+ end
+
+ def save_using(connection, item)
+ connection[@table.name].insert(@table.insert(item))
+ end
+
+ def [](key)
+ @attributes[key]
+ end
+
+ private
+
+ def mapper
+ @attributes[:mapper] || DefaultDataRowMapper.new(self)
+ end
+ end
+
+ class MappingConfigurationBuilder
+ def initialize(attributes = {}, table = DatabaseTable.new)
+ @attributes = attributes
+ @table = table
+ end
+
+ def table(name)
+ @attributes[:table] = name
+ @table.named(name)
+ end
+
+ def type(name)
+ @attributes[:type] = name
+ end
+
+ def primary_key(name, default: 0)
+ @attributes[:primary_key] = name
+ @table.primary_key(name, default: default)
+ end
+
+ def column(name)
+ @table.add_column(name)
+ end
+
+ def build
+ MappingConfiguration.new(@attributes, @table)
+ end
+ end
+end
lib/humble/result_set.rb
@@ -0,0 +1,16 @@
+module Humble
+ class ResultSet
+ include Enumerable
+
+ def initialize(rows, mapper)
+ @rows = rows
+ @mapper = mapper
+ end
+
+ def each(&block)
+ @rows.each do |row|
+ block.call(@mapper.map_from(row))
+ end
+ end
+ end
+end
lib/humble/session.rb
@@ -16,7 +16,7 @@ module Humble
end
def find_all(clazz)
- mapping_for(clazz).find_all_using(create_connection, clazz)
+ mapping_for(clazz).find_all_using(create_connection)
end
private
lib/humble.rb
@@ -3,6 +3,9 @@ require "humble/session_factory"
require "humble/session"
require "humble/configuration"
require "humble/database_mapping"
+require "humble/default_data_row_mapper"
+require "humble/result_set"
+require "humble/mapping_configuration"
module Humble
spec/integration/fixtures/movie_mapping.rb
@@ -1,7 +1,8 @@
class Movie
- attr_reader :name
+ attr_reader :id, :name
def initialize(attributes)
+ @id = attributes[:name] || -1
@name = attributes[:name]
end
end
@@ -10,23 +11,7 @@ class MovieMapping < Humble::DatabaseMapping
def run(map)
map.table :movies
map.type Movie
- map.primary_key :id
+ map.primary_key(:id, default: -1)
map.column :name
end
end
-
-class Results
- include Enumerable
-
- def initialize(rows, mapper)
- @rows = rows
- @mapper = mapper
- end
-
- def each(&block)
- @rows.each do |row|
- block.call(@mapper.map_from(row))
- end
- end
-end
-