Module: Ragabash::Refinements
- Defined in:
- lib/ragabash/refinements.rb
Overview
A set of useful refinements for base classes.
Activate these by including the following in an appropriate lexical scope:
using Ragabash::Refinements
If your Ruby version does not support refinements, this will fall back to monkey-patching.
You may also explicitly activate these refinements via monkey-patching with:
Ragabash::Refinements.monkey_patch! # or
Ragabash::Refinements.activate!
Class Method Summary collapse
-
.monkey_patch! ⇒ Array<Class>
(also: activate!)
Activate the refinements as permanent monkey-patches.
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Determines if the object is (empty? || false).
-
#deep_dup ⇒ Object, self
Recursively duplicates
self
, including non-duplicable objects where necessary. -
#deep_freeze ⇒ self
Deep-freezes
self
. -
#deep_freeze! ⇒ self
Deep-freezes
self
, but skips already-frozen objects. -
#present? ⇒ Boolean
The inverse of #blank?.
-
#safe_copy ⇒ Object, self
(also: #frozen_copy)
Returns
self
if frozen or otherwise a frozen deep-duplicate. -
#try_dup ⇒ Object, self
Attempts to duplicate
self
, or returnsself
on non-duplicable objects.
Class Method Details
.monkey_patch! ⇒ Array<Class> Also known as: activate!
Activate the refinements as permanent monkey-patches.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ragabash/refinements.rb', line 82 def monkey_patch! return @rmods_activated.dup if @rmods.empty? MUTEX.synchronize do @rmods.delete_if do |mod| if mod.is_a?(Array) klass = mod[0] klass.class_eval(&mod[1]) @rmods_activated << klass elsif mod.is_a?(Module) klass = mod.ancestors[1] mod.public_instance_methods(false).each do |m_name| umeth = mod.public_instance_method(m_name) klass.send(:define_method, umeth.name, umeth) end @rmods_activated << klass end end end @rmods_activated.dup end |
Instance Method Details
#blank? ⇒ Boolean
Determines if the object is (empty? || false). (Uses fast_blank#blank_as? for Strings)
Refines: ::Object
, ::NilClass
, ::FalseClass
, ::TrueClass
, ::String
, ::Symbol
, ::Numeric
, ::BigDecimal
, ::Array
, ::Hash
, ::Set
|
# File 'lib/ragabash/refinements.rb', line 57
|
#deep_dup ⇒ Object, self
Recursively duplicates self
, including non-duplicable objects where necessary.
Refines: ::Object
, ::NilClass
, ::FalseClass
, ::TrueClass
, ::Symbol
, ::Numeric
, ::BigDecimal
, ::Array
, ::Hash
, ::Set
|
# File 'lib/ragabash/refinements.rb', line 42
|
#deep_freeze ⇒ self
Deep-freezes self
.
Refines: ::Object
|
# File 'lib/ragabash/refinements.rb', line 21
|
#deep_freeze! ⇒ self
Deep-freezes self
, but skips already-frozen objects.
Refines: ::Object
|
# File 'lib/ragabash/refinements.rb', line 28
|
#present? ⇒ Boolean
The inverse of #blank?
Refines: ::Object
, ::NilClass
, ::FalseClass
, ::TrueClass
, ::String
, ::Symbol
, ::Numeric
, ::BigDecimal
, ::Array
, ::Hash
, ::Set
72 |
# File 'lib/ragabash/refinements.rb', line 72 MUTEX = Mutex.new |
#safe_copy ⇒ Object, self Also known as: frozen_copy
Returns self
if frozen or otherwise a frozen deep-duplicate.
Refines: ::Object
, ::NilClass
, ::FalseClass
, ::TrueClass
, ::String
, ::Symbol
, ::Numeric
, ::BigDecimal
, ::Array
, ::Hash
, ::Set
|
# File 'lib/ragabash/refinements.rb', line 49
|
#try_dup ⇒ Object, self
Attempts to duplicate self
, or returns self
on non-duplicable objects.
Refines: ::Object
, ::NilClass
, ::FalseClass
, ::TrueClass
, ::Symbol
, ::Numeric
, ::BigDecimal
|
# File 'lib/ragabash/refinements.rb', line 35
|