Git Product home page Git Product logo

tap_if's Introduction

#Tap If and Tap Unless Build Status

Install

In your Gemfile:
gem 'tap_if'

Then run:
$ bundle install

Before you use it:
require 'tap_if'
require 'tap_unless'

##Usage

Yields to the block if the caller is truthy or given the method name + args evaluate to a truthy value.
Useful for clarity - always return the caller but only execute the block when the condition passes.

# Update the user's account token if the user is an admin of the account.

User.find(user_id).tap_if(:admin?, account) do |user|
  user.update_token(account)
end

# Only update twitter/facebook if the post actually updates/publishes.

def publish
  (post.pending? && post.update_attributes(:published => true)).tap_if do
    the_update = "New blog post: #{post.title[0..100]}... #{post.link}"

    Twitter.update(the_update)
    Facebook.update(the_update)
  end
end

# Only add a user to an account if the user is not a member.

AccountUser.where(:account_id => account.id, :user_id => user.id).tap_if(:empty?) do |user|
  account.users << user
end

# OR

AccountUser.where(:account_id => account.id, :user_id => user.id).tap_unless(:any?) do |user|
  account.users << user
end

##The Motivation

I found myself assigning a variable and doing something to it if a condition passed. This pattern leaves dangling ifs on the end of lines and is simply less clear than code that will execute if the line is reached.

# I found myself doing (a contrived version):

user = User.where(:email => email).first
user.update_attributes(:status => "active") if user.present? && user.admin?

#But now I can do:

User.where(:email => email).first.tap_if(:admin?) do |user|
  user.update_attributes(:status => "active")
end

# In essence we always update the user assuming we get there.

License

License: MIT-LICENSE (LICENSE.md)

tap_if's People

Contributors

bnorton avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.