Commit 238aa41
Changed files (5)
lib/humble/column.rb
@@ -0,0 +1,18 @@
+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
+end
lib/humble/database_table.rb
@@ -0,0 +1,27 @@
+module Humble
+ 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
+end
lib/humble/mapping_configuration.rb
@@ -1,47 +1,4 @@
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
@@ -71,32 +28,4 @@ module Humble
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/mapping_configuration_builder.rb
@@ -0,0 +1,30 @@
+module Humble
+ 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.rb
@@ -6,6 +6,9 @@ require "humble/database_mapping"
require "humble/default_data_row_mapper"
require "humble/result_set"
require "humble/mapping_configuration"
+require "humble/mapping_configuration_builder"
+require "humble/database_table"
+require "humble/column"
module Humble