From: Logan Capaldo Date: 2005-09-15T00:37:00+09:00 Subject: Re: KirbyBase [ANN (sort-of)] proof-of-concept KirbyBase ORM --Apple-Mail-2-228640982 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8; delsp=yes; format=flowed On Sep 14, 2005, at 1:50 AM, Hal Fulton wrote: > Hmmmm. Having to inherit *after* the open_database > just Feels Wrong somehow. Would it work the same if you > just did the open_database before any db operations? > > In fact, I'm not sure I'd do inheritance at all. I'd have > to think about it. > > Why do Book and Author have to have an is-a relationship > with KirbyRecord::Base? So you can do Author.find() and > so on? > > BTW, I think to_sym is preferred over intern lately. > > >> class Person < KirbyRecord::Base >> end >> person1 =3D Person.new >> person1.set_name "John Doe" >> person1.set_age 25 >> person1.save >> > > OK, I'm catching on to the reason for the inheritance. Is there > any reason a module wouldn't suffice? I'm not sure it matters > either way, but I tend to include modules more than I actually > inherit. > > So far I like the general idea. > Ok I'm including a patch that lets you connect after the inherit. eg. class Author < KirbyRecord::Base end KirbyRecord::Base.open_database( ) authors =3D Author.find(:all) author =3D Author.new # will also cause the reflection and =20 modifications to the Author class to occurr This patch also incidently removes the reliance on the inherited =20 hook, so it would be enitrely possible to make this a module. I'm =20 still mulling that over, and I want to hear some more thoughts on the =20= subject before I decide. =EF=BF=BC= --Apple-Mail-2-228640982 Content-Type: multipart/mixed; boundary=Apple-Mail-3-228640982 --Apple-Mail-3-228640982 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1
On Sep 14, 2005, = at 1:50 AM, Hal Fulton wrote:

Hmmmm. Having to inherit = *after* the open_database

just Feels Wrong somehow. Would it work the same if = you

just did = the open_database before any db operations?


In fact, = I'm not sure I'd do inheritance at all. I'd have

to think about = it.


Why do Book and Author have to have an is-a = relationship

with KirbyRecord::Base? So you can do Author.find() = and

so = on?


BTW, I think to_sym is preferred over intern = lately.



class = Person < KirbyRecord::Base

end

person1 =3D Person.new

person1.set_name "John Doe"

person1.set_age = 25

person1.save



OK, I'm = catching on to the reason for the inheritance. Is there

any reason a module wouldn't = suffice? I'm not sure it matters

either way, but I tend to include modules more than I = actually

inherit.


So far I like the general = idea.



Ok I'm = including a patch that lets you connect after the inherit.

eg.

class Author < = KirbyRecord::Base
end

KirbyRecord::Base.open_databa= se( )

authors = =3D Author.find(:all)

author =3D Author.new # = will also cause the reflection and modifications to the Author class to = occurr


=A0This patch also = incidently removes the reliance on the inherited hook, so it would be = enitrely possible to make this a module. I'm still mulling that over, = and I want to hear some more thoughts on the subject before I = decide.

= --Apple-Mail-3-228640982 Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-unix-mode=0644; name="kirbyrecord.smartinform.patch" Content-Disposition: attachment; filename=kirbyrecord.smartinform.patch --- kirbyrecord.rb.0 2005-09-13 22:25:40.000000000 -0400 +++ kirbyrecord.rb 2005-09-14 11:27:01.000000000 -0400 @@ -11,47 +11,49 @@ end def initialize + self.class.inform_about_table @state = {} end - def self.inherited(subclass) - subclass.inform_about_table - subclass.class_eval do - subclass.fields.each do |name, type| - define_method("get_#{name}".intern) do - @state[name] - end - - define_method("set_#{name}".intern) do |value| - @state[name] = case type - when :String - value.to_s - when :Integer - value.to_i - when :Float - value.to_f - when :Boolean - if value then true else false end - when :Date - value - when :DateTime - value - end - end - end - end - end - def self.inform_about_table + def self.inform_about_table(force = false) + unless force + return self if @fields + end table = get_database.get_table(table_name) @fields = {} table.field_names.zip( table.field_types ) do |field_name, field_type| @fields[field_name] = field_type end + class_eval do + fields.each do |name, type| + define_method("get_#{name}".intern) do + @state[name] + end + + define_method("set_#{name}".intern) do |value| + @state[name] = case type + when :String + value.to_s + when :Integer + value.to_i + when :Float + value.to_f + when :Boolean + if value then true else false end + when :Date + value + when :DateTime + value + end + end + end + end self end def self.find(card) + inform_about_table db = get_database tbl = db.get_table(table_name) results = if block_given? then tbl.select { |r| yield(r) } else tbl.select end --Apple-Mail-3-228640982 Content-Transfer-Encoding: 7bit Content-Type: text/html; charset=US-ASCII
--Apple-Mail-3-228640982-- --Apple-Mail-2-228640982--