Schrodinger’s Cat

Usage:

gem 'schrodingers-cat'
# or with GitHub for latest:
gem 'schrodinger', :git => "git://github.com/caleon/schrodinger.git"

Use case:

Able to avoid common patterns like:

user_or_id.is_a?(User) ? user_or_id.id : user_or_id

in favor of

user_or_id.if_a?(User).id # or
user_or_id.if_a?(User, :else => user_or_id).id
user_or_id.only_if_a?(User) || user_or_id

Note:

These can’t be overriden as they are not methods but low-level operators… ):

define_method(:"&&") {|obj| false }; define_method(:"||") {|obj| obj }

which means we can’t reliably do something like

record.assuming_a(User).authentications.first || raise "You didn't supply a user with authentications!"

because if sc.is_a?(SchrodingersCat), we ideally want:

sc || false     # => false
true && sc      # => false

but instead, as || and && are low-level operators for short-circuiting, we get:

sc || false     # => sc
true && sc      # => sc

However, though normally nil is the only object which should respond to #nil? with a true, so does SchrodingersCat whose MODEL_OBJECT is nil.

Examples:

Example 1:

if user_or_id.is_a?(User) then do #id, else just return user_or_id

    user_or_id = User.first
    user_or_id.if_a?(User).id
=>  1
    user_or_id = User.first.id
    user_or_id.if_a?(User).id
=>  1

Example 2:

Only if user_or_id.is_a?(User) run chained method normally. Otherwise, return nil on chained method call.

    possible_match = "hello".match(/ello/)
    possible_match.only_if_a?(MatchData)[0]
=>  "ello"
    possible_match = "hello".match(/superman/)
    possible_match.only_if_a?(MatchData)[0]
=>  nil

Example 3:

If user_or_id.is_a?(User) continue with chain, else continue returning Schrodinger’s Cat.

    user_or_id = User.first
  	res = user_or_id.assuming_a(User).authentications.first.provider # assuming an authentications record exists.
=>  "facebook"
  	res.class
=>  String
    user_or_id = User.find_by_email('[email protected]') # if there is no user with that email,
  	res = user_or_id.assuming_a(User).authentications.first.provider
=>  nil
  	res.class
=>  SchrodingersCat

Additional information

Contributors

We have a short list of valued contributors. Check them all at:

github.com/caleon/schrodinger/contributors

Maintainers

License

MIT License. Copyright 2011-2012 caleon.