Forum: Ruby KirbyBase, Table Default Values not being assigned

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adam A. (Guest)
on 2008-11-15 06:04
Hi Ive been using KirbyBase and got it up and working and been fine with
basic stuff such as inserting and selecting. I create my own record
class as well to retrieve and pass data to the table.

The problem is with using default values. I cant seem to get the db to
substitiute any blank (nil) values in my custom record class with the
default values I specified when creating the table. When i check the
table in a text editor after an insert the values are kb_nil - hence its
not substituting like it should during the insert operation. Hence when
I do a select on the table in question i get nil values back rather than
the default values.

I experimented in irb and when i dont create a custom record class it
seems to work but i cant find anything in the docs that suggest i should
be doing anything different if i use my own custom record class.

heres is some code for you to use in irb which makes use of a custom
record class called task (im making a todo list) which replicates the
problem


require 'kirbybase'

class Task
attr_accessor(:recno, :title)

def Task.kb_create(recno, title)
Task.new do |x|
x.recno = recno
x.title = title
end
end

def initialize(&block)
instance_eval(&block)
end
end

db = KirbyBase.new
tbl = db.create_table(:deleteme, :title, {:DataType=>:String,
:Default=>'No Title'}) do |t|
t.record_class = Task
end

a_task = Task.new {|r| r.title = nil}
tbl.insert(a_task)
tbl.select

============================================
the result of the above select
---------------------------------------------
irb(main):029:0> tbl.select
=> [#<Task:0x4ec71e8 @title=nil, @recno=1>]

title should not be nil. It should be "No Title".
finally

here is the table file after insert

000001|000000|Task|recno:Integer|title:String:Default->No Title
1|kb_nil


can anyone tell me where im going wrong on this????
Adam A. (Guest)
on 2008-11-18 09:02
im still banging my head against a wall on this one. I have no idea what
im doing wrong...are there any other kirbybase users out there who can
test the code ive supplied and see if they get the same error???
Pit C. (Guest)
on 2008-11-18 10:27
(Received via mailing list)
Adam, from quickly browsing through the KirbyBase code, it looks like
the normal default value mechanism doesn't work if you use custom
record classes. A default value for a column is used only if the data
passed to #insert doesn't contain a value for the column. An instance
of your custom record class always has accessors for all columns, so
the default values are never used. I think you have to handle default
values in your custom class.

Regards,
Pit
Adam A. (Guest)
on 2008-11-25 13:48
Thanks Pit for that, ive decided to scrap the class and just work with
hashses and structs.
This topic is locked and can not be replied to.