Class: Humble::DatabaseTable
- Inherits:
-
Object
- Object
- Humble::DatabaseTable
- Defined in:
- lib/humble/database_table.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_column(name) ⇒ Object
- #destroy(connection, entity) ⇒ Object
-
#initialize ⇒ DatabaseTable
constructor
A new instance of DatabaseTable.
- #named(name) ⇒ Object
- #persist(connection, item) ⇒ Object
- #primary_key(name, default: 0) ⇒ Object
Constructor Details
#initialize ⇒ DatabaseTable
Returns a new instance of DatabaseTable.
5 6 7 |
# File 'lib/humble/database_table.rb', line 5 def initialize @columns = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/humble/database_table.rb', line 3 def name @name end |
Instance Method Details
#add_column(name) ⇒ Object
17 18 19 |
# File 'lib/humble/database_table.rb', line 17 def add_column(name) @columns << Column.new(name) end |
#destroy(connection, entity) ⇒ Object
29 30 31 |
# File 'lib/humble/database_table.rb', line 29 def destroy(connection, entity) @primary_key.destroy(connection[@name], entity) end |
#named(name) ⇒ Object
9 10 11 |
# File 'lib/humble/database_table.rb', line 9 def named(name) @name = name end |
#persist(connection, item) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/humble/database_table.rb', line 21 def persist(connection, item) if @primary_key.has_default_value?(item) @primary_key.apply(insert(item, connection[@name]) , item) else update(item, connection[@name]) end end |
#primary_key(name, default: 0) ⇒ Object
13 14 15 |
# File 'lib/humble/database_table.rb', line 13 def primary_key(name, default: 0) @primary_key = PrimaryKeyColumn.new(name, default) end |