"findOrCreateBy"

View: New views
10 Messages — Rating Filter:   Alert me  

"findOrCreateBy"

by Robert Fischer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm adding "findOrCreateBy" in GORM Labs (does a find, and failing that, instantiates a new one).
Anyone have a nicer nomenclature for that?

~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: "findOrCreateBy"

by nycsailor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Do we need a new method for this or is this too much.  Seems like not
that much to me.

def item = Item.findByName('someName') ?: new Item()

Certainly, this would be more flexible.

def item = Item.findByName('someName') ?: new Item(name:'default', property:'something')

On Fri, 2009-07-03 at 15:41 -0400, Robert Fischer wrote:

> I'm adding "findOrCreateBy" in GORM Labs (does a find, and failing that, instantiates a new one).
> Anyone have a nicer nomenclature for that?
>
> ~~ Robert Fischer, Smokejumper IT Consulting.
> Enfranchised Mind Blog http://EnfranchisedMind.com/blog
>
> Check out my book, "Grails Persistence with GORM and GSQL"!
> http://www.smokejumperit.com/redirect.html
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: "findOrCreateBy"

by Robert Fischer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm not looking for that flexibility.  I'm looking to eliminate this repeated code:

def nameArg = ...
Item.findByName(nameArg) ?: new Item(name:nameArg)

In that idiom, I repeat the class twice, the attribute twice, the argument twice, and it looks
scarier than it should.  And since I use this idiom all over the place, I'd like to fix that.

Don't take this personally, but I'm not looking for feedback on whether other people think it's a
good idea or not.  As far as APIs go, I figure that if you don't like it, you don't have to use it.
  There's no harm being done to the code on your side: a method existing where one didn't before
won't break your code.  And since this is going into GORM Labs and not core Grails, and since I find
it useful and there's no harm being done, I don't feel the need to vet it by the community.

I'm really just wondering if someone else has a better name, because I'd like to create something
that others might find useful and use now and again, too.  And a good name helps adoption quite a
bit.  :)

~~ Robert.

Scott Burch wrote:

> Do we need a new method for this or is this too much.  Seems like not
> that much to me.
>
> def item = Item.findByName('someName') ?: new Item()
>
> Certainly, this would be more flexible.
>
> def item = Item.findByName('someName') ?: new Item(name:'default', property:'something')
>
> On Fri, 2009-07-03 at 15:41 -0400, Robert Fischer wrote:
>> I'm adding "findOrCreateBy" in GORM Labs (does a find, and failing that, instantiates a new one).
>> Anyone have a nicer nomenclature for that?
>>
>> ~~ Robert Fischer, Smokejumper IT Consulting.
>> Enfranchised Mind Blog http://EnfranchisedMind.com/blog
>>
>> Check out my book, "Grails Persistence with GORM and GSQL"!
>> http://www.smokejumperit.com/redirect.html
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>

--
~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: "findOrCreateBy"

by nycsailor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Robert,

Of course no offense :)  

As you said, it breaks nothing.  My concern is bloat.  I have been
working with Rails recently (only because I'm still looking for another
Grails project), and it suffers badly from bloat without adding any good
functionality.

My understanding was that GORM Labs was created as a testing ground for
new Gorm functionality and not necessarily a place for convenience
methods.  

To be honest, I have not looked deeply at Gorm Labs because of it's
transient nature.

Thank you for the clarification.

Scott

On Fri, 2009-07-03 at 17:31 -0400, Robert Fischer wrote:

> I'm not looking for that flexibility.  I'm looking to eliminate this repeated code:
>
> def nameArg = ...
> Item.findByName(nameArg) ?: new Item(name:nameArg)
>
> In that idiom, I repeat the class twice, the attribute twice, the argument twice, and it looks
> scarier than it should.  And since I use this idiom all over the place, I'd like to fix that.
>
> Don't take this personally, but I'm not looking for feedback on whether other people think it's a
> good idea or not.  As far as APIs go, I figure that if you don't like it, you don't have to use it.
>   There's no harm being done to the code on your side: a method existing where one didn't before
> won't break your code.  And since this is going into GORM Labs and not core Grails, and since I find
> it useful and there's no harm being done, I don't feel the need to vet it by the community.
>
> I'm really just wondering if someone else has a better name, because I'd like to create something
> that others might find useful and use now and again, too.  And a good name helps adoption quite a
> bit.  :)
>
> ~~ Robert.
>
> Scott Burch wrote:
> > Do we need a new method for this or is this too much.  Seems like not
> > that much to me.
> >
> > def item = Item.findByName('someName') ?: new Item()
> >
> > Certainly, this would be more flexible.
> >
> > def item = Item.findByName('someName') ?: new Item(name:'default', property:'something')
> >
> > On Fri, 2009-07-03 at 15:41 -0400, Robert Fischer wrote:
> >> I'm adding "findOrCreateBy" in GORM Labs (does a find, and failing that, instantiates a new one).
> >> Anyone have a nicer nomenclature for that?
> >>
> >> ~~ Robert Fischer, Smokejumper IT Consulting.
> >> Enfranchised Mind Blog http://EnfranchisedMind.com/blog
> >>
> >> Check out my book, "Grails Persistence with GORM and GSQL"!
> >> http://www.smokejumperit.com/redirect.html
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe from this list, please visit:
> >>
> >>     http://xircles.codehaus.org/manage_email
> >>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
> >
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: "findOrCreateBy"

by nycsailor :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I also want to say to everyone that I have been working for years in
corporations with closed source.  I am still learning my way around
open-source etiquette. There seems to be a lot to learn.

Feel free to let me know if I am  committing any terrible atrocities :)

On Fri, 2009-07-03 at 17:31 -0400, Robert Fischer wrote:

> I'm not looking for that flexibility.  I'm looking to eliminate this repeated code:
>
> def nameArg = ...
> Item.findByName(nameArg) ?: new Item(name:nameArg)
>
> In that idiom, I repeat the class twice, the attribute twice, the argument twice, and it looks
> scarier than it should.  And since I use this idiom all over the place, I'd like to fix that.
>
> Don't take this personally, but I'm not looking for feedback on whether other people think it's a
> good idea or not.  As far as APIs go, I figure that if you don't like it, you don't have to use it.
>   There's no harm being done to the code on your side: a method existing where one didn't before
> won't break your code.  And since this is going into GORM Labs and not core Grails, and since I find
> it useful and there's no harm being done, I don't feel the need to vet it by the community.
>
> I'm really just wondering if someone else has a better name, because I'd like to create something
> that others might find useful and use now and again, too.  And a good name helps adoption quite a
> bit.  :)
>
> ~~ Robert.
>
> Scott Burch wrote:
> > Do we need a new method for this or is this too much.  Seems like not
> > that much to me.
> >
> > def item = Item.findByName('someName') ?: new Item()
> >
> > Certainly, this would be more flexible.
> >
> > def item = Item.findByName('someName') ?: new Item(name:'default', property:'something')
> >
> > On Fri, 2009-07-03 at 15:41 -0400, Robert Fischer wrote:
> >> I'm adding "findOrCreateBy" in GORM Labs (does a find, and failing that, instantiates a new one).
> >> Anyone have a nicer nomenclature for that?
> >>
> >> ~~ Robert Fischer, Smokejumper IT Consulting.
> >> Enfranchised Mind Blog http://EnfranchisedMind.com/blog
> >>
> >> Check out my book, "Grails Persistence with GORM and GSQL"!
> >> http://www.smokejumperit.com/redirect.html
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe from this list, please visit:
> >>
> >>     http://xircles.codehaus.org/manage_email
> >>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
> >
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: "findOrCreateBy"

by Robert Fischer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Scott Burch wrote:
> My understanding was that GORM Labs was created as a testing ground for
> new Gorm functionality and not necessarily a place for convenience
> methods.  
>

GORM Labs is created by me (not by anyone associated with the Grails core) as basically a place to
drop in API extensions that seem like a good idea at the time.

It's actually not very transient, and I've dropped the "Labs" name and scary wording for similar
plugins (e.g. Sublog) because I think it's driving more people away than is fair.  The warning in
the description was mainly because I'm not giving any guaranties that any particular API in there is
a great idea.  And if it turns out to be *really* stupid, there's a possibility it'll get axed.
Nothing's been axed so far, but I want to reserve the right to axe the API on a major release.

The one thing I will say is that more than Autobase and more than the new Sublog and more than any
other plugin or open source software I've written, GORM Labs is supported through active work on my
billable projects.  This is at least partially because extending GORM is a lot easier than working
in the Autobase niche, so when I estimate hours to implement functionality, my clients tend to be
willing to throw the money at it for the win down the line.  It's also at least partially due to my
clients having a strong confidence in my GORM-wrangling skills: so far, I've only been stumped with
implementing "hasOne" (think "belongsTo" with the id on the other side of the
relationship)—everything else has gone well.  BTW, have I mentioned I'm offering retainer services[1]?

[1] http://smokejumperit.com/grails-retainer/

~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: "findOrCreateBy"

by Moe-25 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thats a good idea, the GORM labs and when it comes to the name of the method I think it sounds easy enough. 
On whether or not its necessary (although Robert don't want any input :P) I do think it would be good standard method for grails gorm, I am sure most folks have this code somewhere in their apps and should therefore be a standard one.

def item = Item.findByName('someName') ?: new Item(name:'default', property:'something').save(flush:true)

for example wouldn't work. It will return either true or false.

I hope you will provide two maps, one to find by, and one with the "extra" properties to create by and the flush option :)

Moe

Re: "findOrCreateBy"

by Robert Fischer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's true -- I could jack the map argument of findBy.  I'll probably go with that.

~~ Robert.

Moe wrote:

> Thats a good idea, the GORM labs and when it comes to the name of the
> method I think it sounds easy enough.
> On whether or not its necessary (although Robert don't want any input
> :P) I do think it would be good standard method for grails gorm, I am
> sure most folks have this code somewhere in their apps and should
> therefore be a standard one.
>
> def item = Item.findByName('someName') ?: new Item(name:'default',
> property:'something').save(flush:true)
>
> for example wouldn't work. It will return either true or false.
>
> I hope you will provide two maps, one to find by, and one with the
> "extra" properties to create by and the flush option :)
>
> Moe

--
~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: "findOrCreateBy"

by Jeff Brown-14 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Jul 4, 2009 at 12:09 PM, Moe<mohamed5432154321@...> wrote:

> Thats a good idea, the GORM labs and when it comes to the name of the method
> I think it sounds easy enough.
> On whether or not its necessary (although Robert don't want any input :P) I
> do think it would be good standard method for grails gorm, I am sure most
> folks have this code somewhere in their apps and should therefore be a
> standard one.
> def item = Item.findByName('someName') ?: new Item(name:'default',
> property:'something').save(flush:true)
>
> for example wouldn't work. It will return either true or false.

I don't think that will return a boolean.  I think that will return
null or an Item.  It will return an Item if findByName returns an item
or if save() is successful, otherwise return null.  Right?




Jeff
--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: "findOrCreateBy"

by Moe-25 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Jul 5, 2009 at 12:39 AM, Jeff Brown <jeff@...> wrote:
On Sat, Jul 4, 2009 at 12:09 PM, Moe<mohamed5432154321@...> wrote:
> Thats a good idea, the GORM labs and when it comes to the name of the method
> I think it sounds easy enough.
> On whether or not its necessary (although Robert don't want any input :P) I
> do think it would be good standard method for grails gorm, I am sure most
> folks have this code somewhere in their apps and should therefore be a
> standard one.
> def item = Item.findByName('someName') ?: new Item(name:'default',
> property:'something').save(flush:true)
>
> for example wouldn't work. It will return either true or false.

I don't think that will return a boolean.  I think that will return
null or an Item.  It will return an Item if findByName returns an item
or if save() is successful, otherwise return null.  Right?

 You are absolutely right! Poor testing from my side :O .. I need to pay more attention!

Moe