Sha256: 6ab8327529fc47bebbb917c432525c18237014d345819fe70592a3fea4426ba7

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

module ActiveObject::Object
  FALSE_VALUES = [false, 0, '0', 'false', 'FALSE', 'f', 'F'].freeze
  TRUE_VALUES = [true, 1, '1', 'true', 'TRUE', 't', 'T'].freeze

  def array?
    is_a?(Array)
  end

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

  def boolean?
    TRUE_VALUES.include?(self) || FALSE_VALUES.include?(self)
  end

  def false?
    self == false
  end

  def falsey?
    nil? || FALSE_VALUES.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

  def present?
    !blank?
  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?
    TRUE_VALUES.include?(self)
  end

  def try(*obj, &block)
    try!(*obj, yield(block)) if obj.empty? || respond_to?(obj.first)
  end

  def try!(*obj, &block)
    if obj.empty? && block_given?
      block.arity.zero? ? instance_eval(yield(block)) : yield(self)
    else
      public_send(*obj, yield(block))
    end
  end

end

Object.include(ActiveObject::Object) if ActiveObject::Settings.config.autoload_object

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_object-4.0.2 lib/active_object/object.rb
active_object-4.0.1 lib/active_object/object.rb