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

Instance Method Summary collapse

Class Method Details

.monkey_patch!Array<Class> Also known as: activate!

Activate the refinements as permanent monkey-patches.

Returns:

  • (Array<Class>)

    a list of all patched classes.


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

Returns:

  • (Boolean)

    true if (empty? || false), otherwise false


# File 'lib/ragabash/refinements.rb', line 57

#deep_dupObject, self

Recursively duplicates self, including non-duplicable objects where necessary.

Refines: ::Object, ::NilClass, ::FalseClass, ::TrueClass, ::Symbol, ::Numeric, ::BigDecimal, ::Array, ::Hash, ::Set

Returns:

  • (Object, self)

# File 'lib/ragabash/refinements.rb', line 42

#deep_freezeself

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

Returns:

  • (Boolean)

    true if !blank?, othewise false


72
# File 'lib/ragabash/refinements.rb', line 72

MUTEX = Mutex.new

#safe_copyObject, 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

Returns:

  • (Object, self)

# File 'lib/ragabash/refinements.rb', line 49

#try_dupObject, self

Attempts to duplicate self, or returns self on non-duplicable objects.

Refines: ::Object, ::NilClass, ::FalseClass, ::TrueClass, ::Symbol, ::Numeric, ::BigDecimal

Returns:

  • (Object, self)

# File 'lib/ragabash/refinements.rb', line 35