Sha256: 264a0bf3f6b77e02ea396bf9181d4de6046b35fff9fa37e44fc87b3eb23de638

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

class Object

  def array?
    is_a?(Array)
  end

  unless defined?(Rails)
    def blank?
      object = self
      object = object.strip if respond_to?(:strip)
      respond_to?(:empty?) ? !!object.empty? : !object
    end
  end

  def boolean?
    [false, true, nil, 0, 1].include?(self)
  end

  def false?
    self == false
  end

  def falsey?
    [false, nil, 0].include?(self)
  end

  def float?
    is_a?(Float)
  end

  def hash?
    is_a?(Hash)
  end

  def integer?
    is_a?(Integer)
  end

  def numeral?
    !to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
  end

  def numeric?
    is_a?(Numeric)
  end

  def palindrome?
    to_s == to_s.reverse
  end

  unless defined?(Rails)
    def present?
      !blank?
    end
  end

  def range?
    is_a?(Range)
  end

  def salvage(placeholder="---")
    blank? ? placeholder : self
  end

  def string?
    is_a?(String)
  end

  def time?
    is_a?(Time)
  end

  def true?
    self == true
  end

  def truthy?
    !falsey?
  end

  unless defined?(Rails)
    def try(*a, &b)
      try!(*a, &b) if a.empty? || respond_to?(a.first)
    end
  end

  unless defined?(Rails)
    def try!(*a, &b)
      if a.empty? && block_given?
        b.arity.zero? ? instance_eval(&b) : yield(self)
      else
        public_send(*a, &b)
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_object-2.2.5 lib/active_object/object.rb
active_object-2.2.4 lib/active_object/object.rb