class Object
Public Instance Methods
assuming_a(*others, &block)
click to toggle source
if user_or_id.is_a?(User) continue with chain, else continue returning Schrodinger’s Cat. Ex: 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('noone@kibblr.com') # if there is no user with ID=999, res = user_or_id.assuming_a(User).authentications.first.provider
> nil¶ ↑
res.class
> SchrodingersCat
¶ ↑
# File lib/schrodingers_cat/core_ext.rb, line 52 def assuming_a(*others, &block) if_a?(*others.merge_options(:chain => true), &block) end
assuming_equals_a(*others, &block)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 63 def assuming_equals_a(*others, &block); if_equals_a?(*others.merge_options(:chain => true), &block); end
if?(varied, *args, &block)
click to toggle source
Custom conditionals. ##
# File lib/schrodingers_cat/core_ext.rb, line 68 def if?(varied, *args, &block) if_a?(*args.merge_options(:conditions => resolve_conditions(varied)), &block); end
if_a?(*others) { |self| ... }
click to toggle source
if user_or_id.is_a?(User) then do id, else just return user_or_id Ex: 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¶ ↑
# File lib/schrodingers_cat/core_ext.rb, line 9 def if_a?(*others, &block) others.flatten_splat! # decide whether im using these or not. already using merge_options and shit. opts = others.extract_options! check_conditions = opts.key?(:conditions) ? opts.delete(:conditions) : send((opts.delete(:check) || :is_one_kind_of?), *others) if opts.delete(:opposite) ? !check_conditions : check_conditions block_given? ? yield(self) : self else # :else => self is the default, for when i dont do any operations on original object. opts.merge!(:else => self) unless opts.key?(:else) || opts[:chain] # if opts[:chain] is passed, then there is never an :else. return opts[:else] if block_given? && opts.key?(:else) SchrodingersCat.new(self, opts) end end
if_equals_a?(*others, &block)
click to toggle source
Exact comparison of == in is_one_of? rather than utilizing === in is_one_kind_of
# File lib/schrodingers_cat/core_ext.rb, line 59 def if_equals_a?(*others, &block); if_a?(*others.merge_options(:conditions => in?(others)), &block); end
only_if?(*args, &block)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 70 def only_if?(*args, &block); if?(*args.merge_options(:else => nil), &block); end
only_if_a?(*others, &block)
click to toggle source
Only if user_or_id.is_a?(User) run chained method normally. Otherwise, return nil on chained method call. Ex: 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¶ ↑
# File lib/schrodingers_cat/core_ext.rb, line 33 def only_if_a?(*others, &block) if_a?(*others.merge_options(:else => nil), &block) end
only_if_equals_a?(*others, &block)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 61 def only_if_equals_a?(*others, &block); if_equals_a?(*others.merge_options(:else => nil), &block); end
only_unless?(*args, &block)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 71 def only_unless?(*args, &block); only_if?(*args.merge_options(:opposite => true), &block); end
only_unless_a?(*others, &block)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 37 def only_unless_a?(*others, &block) only_if_a?(*others.merge_options(:opposite => true), &block) end
only_unless_equals_a?(*others, &block)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 62 def only_unless_equals_a?(*others, &block); unless_equals_a?(*others.merge_options(:else => nil), &block); end
present_else_nil()
click to toggle source
Utilities ##
# File lib/schrodingers_cat/core_ext.rb, line 77 def present_else_nil; present_else(nil); end
significant_else_nil()
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 78 def significant_else_nil; significant_else(nil); end
unless?(*args, &block)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 69 def unless?(*args, &block); if?(*args.merge_options(:opposite => true), &block); end
unless_a?(*others, &block)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 22 def unless_a?(*others, &block) if_a?(*others.merge_options!(:opposite => true), &block) end
unless_equals_a?(*others, &block)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 60 def unless_equals_a?(*others, &block); if_equals_a?(*others.merge_options(:opposite => true), &block); end
Protected Instance Methods
present_else(other)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 81 def present_else(other); if?(:present?, :else => other) { self }; end
significant_else(other)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 82 def significant_else(other); if?(:significant?, :else => other) { self }; end
Private Instance Methods
resolve_conditions(varied)
click to toggle source
# File lib/schrodingers_cat/core_ext.rb, line 85 def resolve_conditions(varied) varied.is_a?(Symbol) ? __send__(varied) : varied.is_a?(Array) ? __send__(*varied) : varied.is_a?(String) ? varied =~ /^\./ ? __send__(:eval, "self#{varied}") : eval(varied) : !!varied end