Module: TapIf

Defined in:
lib/tap_if.rb

Instance Method Summary collapse

Instance Method Details

#tap_if(*args, &block) ⇒ Object

Delegates to Object#tap 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()

end

Only update twitter/facebook if the post actually 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



23
24
25
26
# File 'lib/tap_if.rb', line 23

def tap_if(*args, &block)
  args.empty? && self || args.any? && self.respond_to?(args.first) && self.send(*args) ?
    self.tap(&block) : self
end