Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/schrodinger/core_ext.rb
Instance Method Summary collapse
-
#assuming_a(*others, &block) ⇒ Object
if user_or_id.is_a?(User) continue with chain, else continue returning Schrodinger’s Cat.
- #assuming_equals_a(*others, &block) ⇒ Object
-
#if?(varied, *args, &block) ⇒ Boolean
Custom conditionals.
-
#if_a?(*others, &block) ⇒ Boolean
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.
-
#if_equals_a?(*others, &block) ⇒ Boolean
Exact comparison of #== in is_one_of? rather than utilizing #=== in #is_one_kind_of.
- #only_if?(*args, &block) ⇒ Boolean
-
#only_if_a?(*others, &block) ⇒ Boolean
Only if user_or_id.is_a?(User) run chained method normally.
- #only_if_equals_a?(*others, &block) ⇒ Boolean
- #only_unless?(*args, &block) ⇒ Boolean
- #only_unless_a?(*others, &block) ⇒ Boolean
- #only_unless_equals_a?(*others, &block) ⇒ Boolean
-
#present_else_nil ⇒ Object
Utilities ##.
- #significant_else_nil ⇒ Object
- #unless?(*args, &block) ⇒ Boolean
- #unless_a?(*others, &block) ⇒ Boolean
- #unless_equals_a?(*others, &block) ⇒ Boolean
Instance Method Details
#assuming_a(*others, &block) ⇒ Object
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('[email protected]') # if there is no user with ID=999,
res = user_or_id.assuming_a(User).authentications.first.provider
> nil
res.class
> SchrodingersCat
52 53 54 |
# File 'lib/schrodinger/core_ext.rb', line 52 def assuming_a(*others, &block) if_a?(*others.(:chain => true), &block) end |
#assuming_equals_a(*others, &block) ⇒ Object
63 |
# File 'lib/schrodinger/core_ext.rb', line 63 def assuming_equals_a(*others, &block); if_equals_a?(*others.(:chain => true), &block); end |
#if?(varied, *args, &block) ⇒ Boolean
Custom conditionals. ##
68 |
# File 'lib/schrodinger/core_ext.rb', line 68 def if?(varied, *args, &block) if_a?(*args.(:conditions => resolve_conditions(varied)), &block); end |
#if_a?(*others, &block) ⇒ Boolean
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
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/schrodinger/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. 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) ⇒ Boolean
Exact comparison of #== in is_one_of? rather than utilizing #=== in #is_one_kind_of
59 |
# File 'lib/schrodinger/core_ext.rb', line 59 def if_equals_a?(*others, &block); if_a?(*others.(:check => :is_included_in?), &block); end |
#only_if?(*args, &block) ⇒ Boolean
70 |
# File 'lib/schrodinger/core_ext.rb', line 70 def only_if?(*args, &block); if?(*args.(:else => nil), &block); end |
#only_if_a?(*others, &block) ⇒ Boolean
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
33 34 35 |
# File 'lib/schrodinger/core_ext.rb', line 33 def only_if_a?(*others, &block) if_a?(*others.(:else => nil), &block) end |
#only_if_equals_a?(*others, &block) ⇒ Boolean
61 |
# File 'lib/schrodinger/core_ext.rb', line 61 def only_if_equals_a?(*others, &block); if_equals_a?(*others.(:else => nil), &block); end |
#only_unless?(*args, &block) ⇒ Boolean
71 |
# File 'lib/schrodinger/core_ext.rb', line 71 def only_unless?(*args, &block); only_if?(*args.(:opposite => true), &block); end |
#only_unless_a?(*others, &block) ⇒ Boolean
37 38 39 |
# File 'lib/schrodinger/core_ext.rb', line 37 def only_unless_a?(*others, &block) only_if_a?(*others.(:opposite => true), &block) end |
#only_unless_equals_a?(*others, &block) ⇒ Boolean
62 |
# File 'lib/schrodinger/core_ext.rb', line 62 def only_unless_equals_a?(*others, &block); unless_equals_a?(*others.(:else => nil), &block); end |
#present_else_nil ⇒ Object
Utilities ##
77 |
# File 'lib/schrodinger/core_ext.rb', line 77 def present_else_nil; present_else(nil); end |
#significant_else_nil ⇒ Object
78 |
# File 'lib/schrodinger/core_ext.rb', line 78 def significant_else_nil; significant_else(nil); end |
#unless?(*args, &block) ⇒ Boolean
69 |
# File 'lib/schrodinger/core_ext.rb', line 69 def unless?(*args, &block); if?(*args.(:opposite => true), &block); end |
#unless_a?(*others, &block) ⇒ Boolean
22 23 24 |
# File 'lib/schrodinger/core_ext.rb', line 22 def unless_a?(*others, &block) if_a?(*others.(:opposite => true), &block) end |
#unless_equals_a?(*others, &block) ⇒ Boolean
60 |
# File 'lib/schrodinger/core_ext.rb', line 60 def unless_equals_a?(*others, &block); if_equals_a?(*others.(:opposite => true), &block); end |