I wrote this little script yesterday and was very happy with it and wanted to share. Hopefully someone else out there finds it useful. Usually when I write classes (Prototype 1.6) I use what I think is a pretty common method for defining defaults and allowing those defaults to be overridden. It looks something like this: http://pastie.caboo.se/108865 (Section 1) This simply and quick solution usually does the job. But notice how the "defaults" object has an object within it called "feedback". This object holds 3 items called "tryAgain", "correct", and "incorrect". What if you want to override the "correct" item but leave the "tryAgain" and "incorrect" intact? The standard "Object.extend" method isn''t going to help us here. Setting only the "feedback.correct" will in fact leave you with no "feedback.tryAgain" or "feedback.incorrect" properties at all. They''ll be overridden and lost. http://pastie.caboo.se/108865 (Section 2) My solution was to write a mixin that would recursively look at the passed in properties and only replace the exact nodes specified. For example, now we can override the "correct" item AND leave the "tryAgain" and "incorrect" intact! http://pastie.caboo.se/108865 (Section 3) I tried to write this mixin in such may that it could be used on just about any Class. I have not extensively tested it but it''s holding up so far in all of my cases. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Richard Quadling
2007-Oct-19 15:05 UTC
Re: Prototype - Default Options Mixin (for 1.6+ Classes)
On 19/10/2007, Bart Lewis <bartlewis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I wrote this little script yesterday and was very happy with it and > wanted to share. Hopefully someone else out there finds it useful. > > Usually when I write classes (Prototype 1.6) I use what I think is a > pretty common method for defining defaults and allowing those defaults > to be overridden. It looks something like this: > > http://pastie.caboo.se/108865 > (Section 1) > > This simply and quick solution usually does the job. But notice how > the "defaults" object has an object within it called "feedback". This > object holds 3 items called "tryAgain", "correct", and "incorrect". > What if you want to override the "correct" item but leave the > "tryAgain" and "incorrect" intact? The standard "Object.extend" method > isn''t going to help us here. Setting only the "feedback.correct" will > in fact leave you with no "feedback.tryAgain" or "feedback.incorrect" > properties at all. They''ll be overridden and lost. > > http://pastie.caboo.se/108865 > (Section 2) > > My solution was to write a mixin that would recursively look at the > passed in properties and only replace the exact nodes specified. For > example, now we can override the "correct" item AND leave the > "tryAgain" and "incorrect" intact! > > http://pastie.caboo.se/108865 > (Section 3) > > I tried to write this mixin in such may that it could be used on just > about any Class. I have not extensively tested it but it''s holding up > so far in all of my cases.The contra question to this would be how do I NOT merge my subclass options but override them. Maybe only merge some and not others? -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Good point. I haven''t yet ran into a need for that in my scripts thus I haven''t considered that case. -B On Oct 19, 8:05 am, "Richard Quadling" <rquadl...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 19/10/2007, Bart Lewis <bartle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > I wrote this little script yesterday and was very happy with it and > > wanted to share. Hopefully someone else out there finds it useful. > > > Usually when I write classes (Prototype 1.6) I use what I think is a > > pretty common method for defining defaults and allowing those defaults > > to be overridden. It looks something like this: > > >http://pastie.caboo.se/108865 > > (Section 1) > > > This simply and quick solution usually does the job. But notice how > > the "defaults" object has an object within it called "feedback". This > > object holds 3 items called "tryAgain", "correct", and "incorrect". > > What if you want to override the "correct" item but leave the > > "tryAgain" and "incorrect" intact? The standard "Object.extend" method > > isn''t going to help us here. Setting only the "feedback.correct" will > > in fact leave you with no "feedback.tryAgain" or "feedback.incorrect" > > properties at all. They''ll be overridden and lost. > > >http://pastie.caboo.se/108865 > > (Section 2) > > > My solution was to write a mixin that would recursively look at the > > passed in properties and only replace the exact nodes specified. For > > example, now we can override the "correct" item AND leave the > > "tryAgain" and "incorrect" intact! > > >http://pastie.caboo.se/108865 > > (Section 3) > > > I tried to write this mixin in such may that it could be used on just > > about any Class. I have not extensively tested it but it''s holding up > > so far in all of my cases. > > The contra question to this would be how do I NOT merge my subclass > options but override them. Maybe only merge some and not others? > > -- > ----- > Richard Quadling > Zend Certified Engineer :http://zend.com/zce.php?c=ZEND002498&r=213474731 > "Standing on the shoulders of some very clever giants!"--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Richard Quadling
2007-Oct-22 10:05 UTC
Re: Prototype - Default Options Mixin (for 1.6+ Classes)
On 19/10/2007, Bart Lewis <bartlewis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Good point. I haven''t yet ran into a need for that in my scripts thus > I haven''t considered that case. > > -B > > On Oct 19, 8:05 am, "Richard Quadling" <rquadl...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > wrote: > > On 19/10/2007, Bart Lewis <bartle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I wrote this little script yesterday and was very happy with it and > > > wanted to share. Hopefully someone else out there finds it useful. > > > > > Usually when I write classes (Prototype 1.6) I use what I think is a > > > pretty common method for defining defaults and allowing those defaults > > > to be overridden. It looks something like this: > > > > >http://pastie.caboo.se/108865 > > > (Section 1) > > > > > This simply and quick solution usually does the job. But notice how > > > the "defaults" object has an object within it called "feedback". This > > > object holds 3 items called "tryAgain", "correct", and "incorrect". > > > What if you want to override the "correct" item but leave the > > > "tryAgain" and "incorrect" intact? The standard "Object.extend" method > > > isn''t going to help us here. Setting only the "feedback.correct" will > > > in fact leave you with no "feedback.tryAgain" or "feedback.incorrect" > > > properties at all. They''ll be overridden and lost. > > > > >http://pastie.caboo.se/108865 > > > (Section 2) > > > > > My solution was to write a mixin that would recursively look at the > > > passed in properties and only replace the exact nodes specified. For > > > example, now we can override the "correct" item AND leave the > > > "tryAgain" and "incorrect" intact! > > > > >http://pastie.caboo.se/108865 > > > (Section 3) > > > > > I tried to write this mixin in such may that it could be used on just > > > about any Class. I have not extensively tested it but it''s holding up > > > so far in all of my cases. > > > > The contra question to this would be how do I NOT merge my subclass > > options but override them. Maybe only merge some and not others?I can''t think of a sensible solution without creating a hack. The expected behaviour would be to replace on inheritence. -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---