ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: foonlyboy@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92564] [Ruby trunk Feature#15829] Object#then_if(condition){}
Date: Sun, 05 May 2019 20:30:37 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77926.20190505203037.95b3d895c22543ee@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15829.20190505182618@ruby-lang.org

Issue #15829 has been updated by foonlyboy (Eike Dierks).


A more complex example:

``` ruby
    scope :blacklisted_at, -> (seller=nil, flag=true) {
      then_if(! flag.nil?) {
        joins(:disk_seller_maps)
        .then_if(seller) {|q|
          q.where(disk_seller_map:{seller_id:seller})
        }
        .then_if(! flag) {|q|
          all.where.not(id: q)
        }
      }
    }

```

Not really sure if this does what I mean,
but it looks a lot more concise now.


Before that all that cases would have expanded to 6 cases,
but now it looks a lot more concise.

- then_if(! flag.nil?) shields from flag.nil?
- then_if(seller) only applies if a seller was given
- then_if(! flag) negates the query

This brings the code down from 2x3=6 variants,
to only two code blocks (the second one being trivial in this case)

















----------------------------------------
Feature #15829: Object#then_if(condition){}
https://bugs.ruby-lang.org/issues/15829#change-77926

* Author: foonlyboy (Eike Dierks)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I'd like to propose some sugar to Object#then

There should be Object#then_if(condition, &block)
 
The block should only be applied when the condition is true,
otherwise the object should be returned without applying the block.

Rationale:

I frequently use Object#then with Rails to extend queries like this:

```
foo.then {|query|
  if(condition)
   query.where(zip:zap)
  else
   query
  end
}
```

by using the proposed Object#then_if the example could be simplified to:

```
foo.then_if(condition) {|query|
   query.where(zip:zap)
}
```

I believe that this also applies to a lot of other use cases,
i.e. only applying some transformation if some condition is true,
but otherwise leaving the result untouched.















-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2019-05-05 20:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15829.20190505182618@ruby-lang.org>
2019-05-05 18:26 ` [ruby-core:92559] [Ruby trunk Feature#15829] Object#then_if(condition){} foonlyboy
2019-05-05 18:35 ` [ruby-core:92560] " foonlyboy
2019-05-05 18:48 ` [ruby-core:92561] " foonlyboy
2019-05-05 20:30 ` foonlyboy [this message]
2019-05-05 20:51 ` [ruby-core:92565] " sawadatsuyoshi
2019-05-11  7:13 ` [ruby-core:92620] " nobu
2019-05-11 12:11 ` [ruby-core:92621] " sawadatsuyoshi
2019-07-29  6:58 ` [ruby-core:93967] [Ruby master " ko1
2019-12-15  8:22 ` [ruby-core:96245] " masafumi.o1988

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-77926.20190505203037.95b3d895c22543ee@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).