Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/ruby/commons/core_ext/object/duplicable.rb,
lib/ruby/commons/core_ext/object/try.rb,
lib/ruby/commons/core_ext/object/path.rb,
lib/ruby/commons/core_ext/object/blank.rb,
lib/ruby/commons/core_ext/object/freeze.rb,
lib/ruby/commons/core_ext/object/deep_dup.rb
Overview
Most objects are cloneable, but not all. For example you can’t dup nil
:
nil.dup # => TypeError: can't dup NilClass
Classes may signal their instances are not duplicable removing dup
/clone
or raising exceptions from them. So, to dup an arbitrary object you normally use an optimistic approach and are ready to catch an exception, say:
arbitrary_object.dup rescue object
Rails dups objects in a few critical spots where they are not that arbitrary. That rescue is very expensive (like 40 times slower than a predicate), and it is often triggered.
That’s why we hardcode the following cases and check mp_duplicable?
instead of using that rescue idiom.
Instance Method Summary collapse
-
#mp_blank? ⇒ true, false
An object is blank if it’s false, empty, or a whitespace string.
-
#mp_deep_dup ⇒ Object
Returns a deep copy of object if it’s duplicable.
-
#mp_deep_freeze ⇒ Object
TODO.
-
#mp_duplicable? ⇒ Boolean
Can you safely dup this object?.
-
#mp_path(path = '', default = nil, delimiter = '.') ⇒ Object
Gets a value from an Object using a dot separated path.
-
#mp_presence ⇒ Object
Returns the receiver if it’s present otherwise returns
nil
. -
#mp_present? ⇒ true, false
An object is present if it’s not blank.
-
#mp_try(*a, &b) ⇒ Object
Invokes the public method whose name goes as first argument just like
public_send
does, except that if the receiver does not respond to it the call returnsnil
rather than raising an exception. -
#mp_try!(*a, &b) ⇒ Object
Same as #mp_try, but will raise a NoMethodError exception if the receiver is not
nil
and does not implement the tried method.
Instance Method Details
#mp_blank? ⇒ true, false
An object is blank if it’s false, empty, or a whitespace string. For example, ”, ‘ ’, nil
, [], and {} are all blank.
This simplifies
address.nil? || address.empty?
to
address.mp_blank?
15 16 17 |
# File 'lib/ruby/commons/core_ext/object/blank.rb', line 15 def mp_blank? respond_to?(:empty?) ? !!empty? : !self end |
#mp_deep_dup ⇒ Object
Returns a deep copy of object if it’s duplicable. If it’s not duplicable, returns self
.
object = Object.new
dup = object.mp_deep_dup
dup.instance_variable_set(:@a, 1)
object.instance_variable_defined?(:@a) # => false
dup.instance_variable_defined?(:@a) # => true
14 15 16 |
# File 'lib/ruby/commons/core_ext/object/deep_dup.rb', line 14 def mp_deep_dup mp_duplicable? ? dup : self end |
#mp_deep_freeze ⇒ Object
TODO
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ruby/commons/core_ext/object/freeze.rb', line 4 def mp_deep_freeze proc_val = Proc.new {} proc_hsh = Proc.new do |k, v| proc_val.call(v) end proc_val = Proc.new do |v| case v when Hash v.each_value(&proc_hsh) when Array v.each(&proc_val) else # Do nothing .. end v.freeze end proc_val.call(self) end |
#mp_duplicable? ⇒ Boolean
Can you safely dup this object?
False for nil
, false
, true
, symbol, number objects; true otherwise.
24 25 26 |
# File 'lib/ruby/commons/core_ext/object/duplicable.rb', line 24 def mp_duplicable? true end |
#mp_path(path = '', default = nil, delimiter = '.') ⇒ Object
Gets a value from an Object using a dot separated path.
{ a: 1, b: { c: 3, d: [1, 2, 3] } }.mp_path('b.d') # => [1, 2, 3]
{ a: 1, b: { c: 3, d: [1, 2, 3] } }.mp_path('b.d.1') # => 2
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ruby/commons/core_ext/object/path.rb', line 7 def mp_path(path = '', default = nil, delimiter = '.') value = self path.to_s.split(delimiter).each do |key| index = key.to_i unless value.respond_to?(:[]) value = nil break end if key == index.to_s value = value[index] elsif value.respond_to?(:key?) value = value.key?(key) ? value[key] : value[key.to_sym] else value = nil end break if value.nil? end value || default end |
#mp_presence ⇒ Object
Returns the receiver if it’s present otherwise returns nil
. object.mp_presence
is equivalent to
object.mp_present? ? object : nil
For example, something like
state = params[:state] if params[:state].mp_present?
country = params[:country] if params[:country].mp_present?
region = state || country || 'US'
becomes
region = params[:state].mp_presence || params[:country].mp_presence || 'US'
44 45 46 |
# File 'lib/ruby/commons/core_ext/object/blank.rb', line 44 def mp_presence self if mp_present? end |
#mp_present? ⇒ true, false
An object is present if it’s not blank.
23 24 25 |
# File 'lib/ruby/commons/core_ext/object/blank.rb', line 23 def mp_present? !mp_blank? end |
#mp_try(*a, &b) ⇒ Object
Invokes the public method whose name goes as first argument just like public_send
does, except that if the receiver does not respond to it the call returns nil
rather than raising an exception.
This method is defined to be able to write
@person.mp_try(:name)
instead of
@person.name if @person
mp_try
calls can be chained:
@person.mp_try(:spouse).mp_try(:name)
instead of
@person.spouse.name if @person && @person.spouse
mp_try
will also return nil
if the receiver does not respond to the method:
@person.mp_try(:non_existing_method) #=> nil
instead of
@person.non_existing_method if @person.respond_to?(:non_existing_method) #=> nil
mp_try
returns nil
when called on nil
regardless of whether it responds to the method:
nil.mp_try(:to_i) # => nil, rather than 0
Arguments and blocks are forwarded to the method if invoked:
@posts.mp_try(:each_slice, 2) do |a, b|
...
end
The number of arguments in the signature must match. If the object responds to the method the call is attempted and ArgumentError
is still raised in case of argument mismatch.
If mp_try
is called without arguments it yields the receiver to a given block unless it is nil
:
@person.mp_try do |p|
...
end
You can also call mp_try with a block without accepting an argument, and the block will be instance_eval’ed instead:
@person.mp_try { upcase.truncate(50) }
Please also note that mp_try
is defined on Object
. Therefore, it won’t work with instances of classes that do not have Object
among their ancestors, like direct subclasses of BasicObject
. For example, using mp_try
with SimpleDelegator
will delegate mp_try
to the target instead of calling it on the delegator itself.
63 64 65 |
# File 'lib/ruby/commons/core_ext/object/try.rb', line 63 def mp_try(*a, &b) mp_try!(*a, &b) if a.empty? || respond_to?(a.first) end |
#mp_try!(*a, &b) ⇒ Object
Same as #mp_try, but will raise a NoMethodError exception if the receiver is not nil
and does not implement the tried method.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ruby/commons/core_ext/object/try.rb', line 70 def mp_try!(*a, &b) if a.empty? && block_given? if b.arity.zero? instance_eval(&b) else yield self end else public_send(*a, &b) end end |