Class: KSTable
- Includes:
- DRbUndumped
- Defined in:
- lib/kansas/Table.rb,
lib/kansas/TableClass.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#rollback_buffer ⇒ Object
Returns the value of attribute rollback_buffer.
-
#rollback_hash ⇒ Object
Returns the value of attribute rollback_hash.
-
#row ⇒ Object
Returns the value of attribute row.
Class Method Summary collapse
- .all_fields ⇒ Object
- .belongs_to(name, foreign_table, local_field, foreign_field = nil) ⇒ Object
- .database ⇒ Object
- .field(localName, remoteName = nil, conversion = nil) ⇒ Object
- .fields ⇒ Object
- .is_a_kstable? ⇒ Boolean
- .primaries ⇒ Object
-
.primary(field) ⇒ Object
A table can have more than one primary field?.
- .relations ⇒ Object
- .table_name ⇒ Object
- .to_many(name, foreign_table, foreign_field, local_field = nil) ⇒ Object
- .to_one(name, local_field, foreign_table, foreign_field = nil) ⇒ Object
Instance Method Summary collapse
- #changed ⇒ Object
-
#delete ⇒ Object
TODO: Add a cascade_delete that deletes the row plus any to_many relations to the row.
-
#initialize ⇒ KSTable
constructor
A new instance of KSTable.
- #inspect ⇒ Object
- #key ⇒ Object
- #load(row, context = nil, read_only = false) ⇒ Object
- #nullify_self ⇒ Object
- #pending_deletion? ⇒ Boolean
- #read_only=(cond) ⇒ Object
- #read_only? ⇒ Boolean (also: #read_only)
- #reset_pending_deletion ⇒ Object
-
#rollback ⇒ Object
Unwind the changes.
- #serialized? ⇒ Boolean
- #set_pending_deletion ⇒ Object
- #set_serialized ⇒ Object
- #table_name ⇒ Object
Constructor Details
#initialize ⇒ KSTable
Returns a new instance of KSTable.
36 37 38 39 40 41 42 43 |
# File 'lib/kansas/Table.rb', line 36 def initialize @row = {} @rollback_buffer = [] @rollback_hash = {} @serialized = false @pending_deletion = false @read_only = false end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
5 6 7 |
# File 'lib/kansas/Table.rb', line 5 def context @context end |
#rollback_buffer ⇒ Object
Returns the value of attribute rollback_buffer.
5 6 7 |
# File 'lib/kansas/Table.rb', line 5 def rollback_buffer @rollback_buffer end |
#rollback_hash ⇒ Object
Returns the value of attribute rollback_hash.
5 6 7 |
# File 'lib/kansas/Table.rb', line 5 def rollback_hash @rollback_hash end |
#row ⇒ Object
Returns the value of attribute row.
5 6 7 |
# File 'lib/kansas/Table.rb', line 5 def row @row end |
Class Method Details
.all_fields ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/kansas/TableClass.rb', line 112 def KSTable.all_fields database.query do |dbh| dbh.columns(table_name).each do |descr| fieldName = descr['name'] field(fieldName, fieldName, conversion(descr['type_name'])) if descr['primary'] primary fieldName end end end end |
.belongs_to(name, foreign_table, local_field, foreign_field = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/kansas/TableClass.rb', line 64 def KSTable.belongs_to(name, foreign_table, local_field, foreign_field = nil) foreign_table = foreign_table.to_s if Symbol === foreign_table if foreign_table.class.to_s == 'String' if KSDatabase.partial_to_complete_map.has_key?(foreign_table) foreign_table = KSDatabase.const_get KSDatabase.partial_to_complete_map[foreign_table] else foreign_table = nil end end # Need to throw an exception if foregin_table is bad. addRelation(name.to_s, KSBelongsTo.new(self, foreign_table, local_field.to_s, foreign_field.to_s)) class_eval <<-EOS def #{name} self.class.relations['#{name}'].get(self) end def #{name}=(val) self.class.relations['#{name}'].set(self, val) end EOS end |
.database ⇒ Object
132 133 134 |
# File 'lib/kansas/TableClass.rb', line 132 def KSTable.database class_eval 'Database' end |
.field(localName, remoteName = nil, conversion = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/kansas/TableClass.rb', line 7 def KSTable.field(localName, remoteName=nil, conversion=nil) remoteName = localName unless remoteName conversionCall = conversion ? ".#{conversion}" : '' addField(localName, remoteName) class_eval <<-EOS def #{localName} @row['#{remoteName}']#{conversionCall} end def #{localName}=(val) unless pending_deletion? or read_only? @rollback_buffer.push ['#{remoteName}', @row['#{remoteName}']] @rollback_hash['#{remoteName}'] ||= [] @rollback_hash['#{remoteName}'].push @row['#{remoteName}'] @row['#{remoteName}'] = val changed end end EOS end |
.fields ⇒ Object
136 137 138 |
# File 'lib/kansas/TableClass.rb', line 136 def KSTable.fields @fields end |
.is_a_kstable? ⇒ Boolean
3 4 5 |
# File 'lib/kansas/TableClass.rb', line 3 def KSTable.is_a_kstable? true end |
.primaries ⇒ Object
124 125 126 |
# File 'lib/kansas/TableClass.rb', line 124 def KSTable.primaries @primaries end |
.primary(field) ⇒ Object
A table can have more than one primary field?
32 33 34 35 36 37 38 |
# File 'lib/kansas/TableClass.rb', line 32 def KSTable.primary(field) if defined?(@primaries) && @primaries @primaries << field unless @primaries.include?(field) else @primaries = [field] end end |
.relations ⇒ Object
140 141 142 |
# File 'lib/kansas/TableClass.rb', line 140 def KSTable.relations @relations end |
.table_name ⇒ Object
128 129 130 |
# File 'lib/kansas/TableClass.rb', line 128 def KSTable.table_name class_eval 'Name' end |
.to_many(name, foreign_table, foreign_field, local_field = nil) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/kansas/TableClass.rb', line 88 def KSTable.to_many(name, foreign_table, foreign_field, local_field = nil) foreign_table = foreign_table.to_s if Symbol === foreign_table if foreign_table.class.to_s == 'String' if KSDatabase.partial_to_complete_map.has_key?(foreign_table) foreign_table = KSDatabase.const_get KSDatabase.partial_to_complete_map[foreign_table] else foreign_table = nil end end # Need to throw an exception if foregin_table is bad. addRelation(name.to_s, KSToMany.new(self, foreign_table, foreign_field.to_s, local_field.to_s)) class_eval <<-EOS def #{name} self.class.relations['#{name}'].get(self) end def #{name}_count self.class.relations['#{name}'].count(self) end EOS end |
.to_one(name, local_field, foreign_table, foreign_field = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kansas/TableClass.rb', line 40 def KSTable.to_one(name, local_field, foreign_table, foreign_field = nil) foreign_table = foreign_table.to_s if Symbol === foreign_table if foreign_table.class.to_s == 'String' if KSDatabase.partial_to_complete_map.has_key?(foreign_table) foreign_table = KSDatabase.const_get KSDatabase.partial_to_complete_map[foreign_table] else foreign_table = nil end end # Need to throw an exception if foregin_table is bad. addRelation(name.to_s, KSToOne.new(self, local_field.to_s, foreign_table, foreign_field.to_s)) class_eval <<-EOS def #{name} self.class.relations['#{name}'].get(self) end def #{name}=(val) self.class.relations['#{name}'].set(self, val) end EOS end |
Instance Method Details
#changed ⇒ Object
54 55 56 |
# File 'lib/kansas/Table.rb', line 54 def changed @context.changed(self) if defined?(@context) && @context end |
#delete ⇒ Object
TODO: Add a cascade_delete that deletes the row plus any to_many relations to the row.
66 67 68 69 70 71 72 73 74 |
# File 'lib/kansas/Table.rb', line 66 def delete # TODO: Add a cascade_delete that deletes the row plus any to_many relations to the row. if @context.autocommit? @context.delete_one(self) else set_pending_deletion changed nullify_self end end |
#inspect ⇒ Object
62 63 64 |
# File 'lib/kansas/Table.rb', line 62 def inspect table_name + @row.inspect end |
#key ⇒ Object
50 51 52 |
# File 'lib/kansas/Table.rb', line 50 def key result = self.class.primaries.collect{|f| @row[f.to_s]} end |
#load(row, context = nil, read_only = false) ⇒ Object
45 46 47 48 |
# File 'lib/kansas/Table.rb', line 45 def load(row, context = nil, read_only = false) @row, @context, @read_only = row, context, read_only self end |
#nullify_self ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/kansas/Table.rb', line 76 def nullify_self @rollback_buffer.push self.dup @row.each_key do |key| @rollback_hash[key] ||= [] @rollback_hash[key].push @row[key] end @row = {} end |
#pending_deletion? ⇒ Boolean
7 8 9 |
# File 'lib/kansas/Table.rb', line 7 def pending_deletion? @deletion end |
#read_only=(cond) ⇒ Object
16 17 18 |
# File 'lib/kansas/Table.rb', line 16 def read_only=(cond) @read_only = cond ? true : false end |
#read_only? ⇒ Boolean Also known as: read_only
11 12 13 |
# File 'lib/kansas/Table.rb', line 11 def read_only? @read_only end |
#reset_pending_deletion ⇒ Object
28 29 30 |
# File 'lib/kansas/Table.rb', line 28 def reset_pending_deletion @deletion = false end |
#rollback ⇒ Object
Unwind the changes.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/kansas/Table.rb', line 87 def rollback @rollback_buffer.reverse.each do |rbval| if Array === rbval @row[rbval[0]] = rbval[1] else @row = rbval.row reset_pending_deletion end end @rollback_buffer.clear @rollback_hash.each_key do |key| @rollback_hash[key].clear end end |
#serialized? ⇒ Boolean
20 21 22 |
# File 'lib/kansas/Table.rb', line 20 def serialized? @serialized end |
#set_pending_deletion ⇒ Object
24 25 26 |
# File 'lib/kansas/Table.rb', line 24 def set_pending_deletion @deletion = true end |
#set_serialized ⇒ Object
32 33 34 |
# File 'lib/kansas/Table.rb', line 32 def set_serialized @serialized = true end |
#table_name ⇒ Object
58 59 60 |
# File 'lib/kansas/Table.rb', line 58 def table_name self.class.table_name end |