Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/justools/core_ext/object.rb
Instance Method Summary collapse
-
#and_also(&block) ⇒ Object
Examples.
-
#as {|_self| ... } ⇒ Object
Examples.
-
#in?(*args) ⇒ Boolean
(also: #is_included_in?)
Copy of Object#in? from Rails 3.2.1 which also allows list.
-
#one_kind_of?(*klasses) ⇒ Boolean
(also: #is_one_kind_of?)
Similar to is_a? or kind_of? but with an array of possible classes.
-
#one_of?(*args) ⇒ Boolean
(also: #is_when?)
Similar to is_a? or kind_of? but with an array of possible classes.
-
#or_else(&block) ⇒ Object
Examples.
- #out_of?(*others) ⇒ Boolean (also: #is_excluded_from?)
Instance Method Details
#and_also(&block) ⇒ Object
Examples
= User.find_by_id(10).and_also { |u| u. }
28 29 30 |
# File 'lib/justools/core_ext/object.rb', line 28 def and_also(&block) self && yield(self) end |
#as {|_self| ... } ⇒ Object
Examples
{ :a=>1, .., :z=>26 }.as { |h| h.each { |k, v| h[k] = v + h[26 - v] } }
User.find(15).stories.joins(:tasks).as { |s| [s.class.to_s, s.count] }.
as { |klass_name, count| "#{count} stories in #{klass_name}" }
[15, { :includes => { :stories => :tasks } }, 56, 78, 102].
as { |id, opts, *ns| ns.each { |n| puts n }; User.find(id, opts) }
14 15 16 |
# File 'lib/justools/core_ext/object.rb', line 14 def as(&block) yield self end |
#in?(*args) ⇒ Boolean Also known as: is_included_in?
Copy of Object#in? from Rails 3.2.1 which also allows list.
Parameters
-
args- Array or a list of its contents.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/justools/core_ext/object.rb', line 38 def in?(*args) if args.length > 1 args.include? self else another_object = args.first if another_object.respond_to? :include? another_object.include?(self) else raise ArgumentError.new("The single parameter passed to #in? must respond to #include?") end end end |
#one_kind_of?(*klasses) ⇒ Boolean Also known as: is_one_kind_of?
Similar to is_a? or kind_of? but with an array of possible classes. Returns the matching class or a nil.
Parameters
-
klasses- List of Class constants, or an Array of Class constants.
Examples
rec_or_num.one_kind_of?(User, Admin).or_else { User.find(rec_or_num) }
71 72 73 |
# File 'lib/justools/core_ext/object.rb', line 71 def one_kind_of?(*klasses) klasses.flatten_splat.detect { |klass| self.kind_of?(klass) } end |
#one_of?(*args) ⇒ Boolean Also known as: is_when?
Similar to is_a? or kind_of? but with an array of possible classes. Returns the matching class or a nil. Renamed here from is_one_kind_of? because it uses the ‘klass === self’ which isn’t necessarily equivalent to ‘self.is_a?(klass)’.
Parameters
-
args- List of arguments.
Examples
rec_or_num.one_of?(User, Admin).or_else { User.find(rec_or_num) }
88 89 90 |
# File 'lib/justools/core_ext/object.rb', line 88 def one_of?(*args) args.flatten_splat.detect { |arg| arg === self} end |
#or_else(&block) ⇒ Object
Examples
user = User.find_by_id(10).or_else { |res| raise "Got back #{res}" }
21 22 23 |
# File 'lib/justools/core_ext/object.rb', line 21 def or_else(&block) self || yield(self) end |
#out_of?(*others) ⇒ Boolean Also known as: is_excluded_from?
56 57 58 |
# File 'lib/justools/core_ext/object.rb', line 56 def out_of?(*others) others.flatten_splat.exclude?(self) end |