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
- #__define_singleton_method(method_name, &block) ⇒ Object
-
#blank? ⇒ Boolean
An object is blank if it’s false, empty, or a whitespace string.
-
#extended_by ⇒ Object
:nodoc.
-
#present? ⇒ Boolean
:nodoc.
-
#returning(value) {|value| ... } ⇒ Object
:nodoc.
- #with_methods_on(other) ⇒ Object
Class Method Details
.__define_method(method_name, &block) ⇒ Object
44 45 46 47 48 |
# File 'lib/support/core_ext.rb', line 44 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
50 51 52 53 54 |
# File 'lib/support/core_ext.rb', line 50 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
13 14 15 |
# File 'lib/support/active_support_lite/blank.rb', line 13 def blank? respond_to?(:empty?) ? empty? : !self end |
#extended_by ⇒ Object
: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
19 20 21 |
# File 'lib/support/active_support_lite/blank.rb', line 19 def present? !blank? end |
#returning(value) {|value| ... } ⇒ Object
:nodoc
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
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 89 90 |
# File 'lib/support/core_ext.rb', line 57 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 |