Class: Object

Inherits:
BasicObject
Defined in:
lib/support/active_support_lite/misc.rb,
lib/support/core_ext.rb,
lib/support/active_support_lite/blank.rb,
lib/support/active_support_lite/object.rb

Overview

:nodoc:all

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__define_method(method_name, &block) ⇒ Object



42
43
44
45
46
# File 'lib/support/core_ext.rb', line 42

def self.__define_method( method_name, &block )
  self.class.class_eval do
    define_method method_name, &block
  end
end

Instance Method Details

#__define_singleton_method(method_name, &block) ⇒ Object



48
49
50
51
52
# File 'lib/support/core_ext.rb', line 48

def __define_singleton_method( method_name, &block )
  (class << self; self; end).class_eval do
    define_method method_name, &block
  end
end

#blank?Boolean

An object is blank if it’s false, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are blank.

This simplifies

if !address.nil? && !address.empty?

to

if !address.blank?

:nodoc

Returns:

  • (Boolean)


13
14
15
# File 'lib/support/active_support_lite/blank.rb', line 13

def blank?
  respond_to?(:empty?) ? empty? : !self
end

#extended_byObject

:nodoc



4
5
6
7
# File 'lib/support/active_support_lite/object.rb', line 4

def extended_by #:nodoc
  ancestors = class << self; ancestors end
  ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
end

#present?Boolean

:nodoc

Returns:

  • (Boolean)


19
20
21
# File 'lib/support/active_support_lite/blank.rb', line 19

def present?
  !blank?
end

#returning(value) {|value| ... } ⇒ Object

:nodoc

Yields:

  • (value)


39
40
41
42
# File 'lib/support/active_support_lite/misc.rb', line 39

def returning(value)
  yield(value)
  value
end

#with_methods_on(other) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/support/core_ext.rb', line 55

def with_methods_on(other)
  (class << self; self; end).class_eval do
    # we need some accounting to ensure that everything behaves itself when
    # .with_methods_on is called more than once.
    @_with_methods_on ||= []
    if !@_with_methods_on.include?("method_missing_before_#{other.__id__}")
      alias_method "method_missing_before_#{other.__id__}", :method_missing
    end     
    @_with_methods_on << "method_missing_before_#{other.__id__}"
      
    define_method :method_missing do |method_name, *args|
      if _other.respond_to?(method_name, true)
        _other.__send__( method_name, *args )
      else
        send "method_missing_before_#{other.__id__}", method_name, *args
      end
    end      
  end

  result = yield

  (class << self; self; end).class_eval do
    # heal the damage
    if @_with_methods_on.pop != "method_missing_before_#{other.__id__}"
      raise "there is no god"
    end        
    if !@_with_methods_on.include?("method_missing_before_#{other.__id__}")
      alias_method :method_missing, "method_missing_before_#{other.__id__}"
      undef_method "method_missing_before_#{other.__id__}"
    end     
  end

  result
end